Search code examples
nginxvagrantvirtualhostvagrantfile

Nginx named virtual Host Not working in Vagrant


I started with the default nginx configurations. Modified the default configuration in /etc/nginx/sites-available/default.Here is the modification.

server {
        listen 80;
        listen [::]:80;

        root /var/www/html;
        index index.html index.htm index.nginx-debian.html;

        server_name mysite.com www.mysite.com;

        location / {
                try_files $uri $uri/ =404;
        }
}

The base configuration in /etc/nginx/nginx.conf is left untouched.

I am running this whole setup in a vagrant box with the following configurations.(Omitted other lines for the sake of brevity.)

  web.vm.network :forwarded_port, guest: 80, host: 8001
  web.vm.network "private_network", ip: "192.168.56.105"

Now if I hit 192.168.56.105 on browser I see the html page being rendered, however when I type mysite.com I get the following error message.

Error The requested URL can not be retrieved

Have added the following entry in my /etc/hosts

192.165.56.105 mysite.com

Interestingly when I try the same configuration on a nginx server installed directly on a ubuntu machine it works. I am suspecting if its a problem with my Vagrant config. Could anyone please point out as to where the error could be?


Solution

  • you have a typo in your /etc/hosts file, the IP is 192.168...

    192.168.56.105 mysite.com
    

    the line

    web.vm.network :forwarded_port, guest: 80, host: 8001
    

    is useless when working with a static IP as you'll reach the server VM directly using its IP. You dont really need to setup an additional network interface