Can't communicate with nginx from Vagrant host machine

7,940

Solution 1

So I have edited the /etc/hosts file to point to 127.0.0.1 instead of the address defined in the vagrantfile and I can now access the website from the host machine.

Solution 2

It's possible I'm going mad, but I think your app is really on http://192.168.100.10:80 (and that should work from both host and guest, or testapp.com:80 from the host alone) rather than port 8080.

The 8080 reference refers to the port forward from the mac host, where localhost:8080 should work as it points to 192.168.100.10:80 on the guest.

Share:
7,940

Related videos on Youtube

alexbilbie
Author by

alexbilbie

Updated on September 18, 2022

Comments

  • alexbilbie
    alexbilbie over 1 year

    I am using Ansible to compile and configure Nginx inside a Vagrant box.

    If I run curl http://localhost/ from inside the box I get the expected response (phpinfo()).

    If I access the URL http://testapp:8080/ from my Mac host machine then I can't connect.

    My configurations is as follows:

    HOST: /etc/hosts

    192.168.100.10 testapp

    HOST: 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
    

    GUEST: /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/*;
    }
    

    GUEST: /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;
        }
    }
    

    Can anyone spot something obvious that I'm missing?

    Many thanks

    • Steve Bennett
      Steve Bennett over 10 years
      Anything in nginx logs? Would tunnelling as a workaround solve your problem? What about doing the port forwarding using nginx?
  • Falcon Momot
    Falcon Momot over 10 years
    It's worth mentioning that localhost is not an especially good test to be running, as it leads to precisely this type of (non-)problem.