无法从 Vagrant 主机与 nginx 通信

无法从 Vagrant 主机与 nginx 通信

我正在使用 Ansible 在 Vagrant 框内编译和配置 Nginx。

如果我curl http://localhost/从盒子里面跑出来,我会得到预期的反应(phpinfo())。

如果我从 Mac 主机访问该 URL http://testapp:8080/,则无法连接。

我的配置如下:

主机:/etc/hosts

192.168.100.10 testapp

主机:vagrantfile

Vagrant.configure("2") do |config|

  config.vm.define "web" do |web_config|
    web_config.vm.box = "raring64"
    web_config.vm.box_url = "https://dl.dropboxusercontent.com/s/{{redacted}}/raring64.box"
    web_config.vm.network "forwarded_port", guest: 80, host: 8080
    web_config.vm.network "private_network", ip: "192.168.100.10"

    web_config.vm.provision :ansible do |ansible|
      ansible.playbook = "devops/webserver.yml"
      ansible.hosts = "webservers"
      ansible.inventory_file = "devops/hosts"
      ansible.verbosity = "vv"
      ansible.verbose = "true"
    end

    web_config.vm.provider "virtualbox" do |v|
      v.customize ["modifyvm", :id, "--memory", "256"]
    end
  end
end

访客:/etc/nginx-1.5.6/nginx.conf

user www-data www-data;
worker_processes  1;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
  worker_connections  768;
  multi_accept on;
}

http {

  include       /etc/nginx-1.5.6/mime.types;
  default_type  application/octet-stream;

  access_log    /var/log/nginx/access.log;

  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;

  keepalive_timeout  65;

  server_tokens off;

  gzip  off;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_proxied any;
  gzip_vary off;
  gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript application/json;
  gzip_min_length  1000;
  gzip_disable     "MSIE [1-6]\.";

  include /etc/nginx-1.5.6/sites-enabled/*;
}

访客:/etc/nginx-1.5.6/sites-enabled/testapp.conf

upstream phpbackend {
    server unix:/var/run/php-fpm-www.sock;
}

server {
    server_name  testapp;
    listen 0.0.0.0:80;
    root  /web/testapp/public;
    index index.php;

    access_log  /var/log/nginx/webvg.access.log;
    error_log /var/log/nginx/webvg.error.log debug;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass phpbackend;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /web/testapp/public/$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_param CACHE_HOST 127.0.0.1;
        fastcgi_param CACHE_PORT 6379;
        fastcgi_param NEO_HOST   127.0.0.1;
        fastcgi_param NEO_PORT   7474;
        fastcgi_param SERVER_ENV dev;
    }

    location ~ ^/(php_status|php_ping)$ {
        fastcgi_pass phpbackend;
        fastcgi_param SCRIPT_FILENAME /web/testapp/public/$fastcgi_script_name;
        include fastcgi_params;
        allow 127.0.0.1;
        deny all;
    }

    location /nginx_status {
        allow 127.0.0.1;
        deny all;
        access_log off;
    }

    location ~ /\.git {
        deny all;
    }
}

有人能发现我遗漏的一些明显的东西吗?

非常感谢

答案1

我可能疯了,但我认为你的应用程序确实处于打开状态http://192.168.100.10:80(并且应该可以在主机和客户机上运行,​​或者testapp.com:80单独在主机上运行),而不是端口 8080。

8080 参考指的是从 mac 主机转发的端口,它应该按照在客户机上localhost:8080指向的方式工作。192.168.100.10:80

答案2

因此,我编辑了 /etc/hosts 文件以指向127.0.0.1vagrantfile 中定义的地址,现在我可以从主机访问该网站。

相关内容