Question

Nginx rule doesn't work when listening to specific hostname

  • Posted August 28, 2014

I am learning to use digital ocean. I have the following NGINX rule created:

server { 
	listen asimplereader.com:80; 
	location ~ ^/ { 
		proxy_pass http://localhost:8081; 
		proxy_http_version 1.1; 
		proxy_set_header Upgrade $http_upgrade; 
		proxy_set_header Connection 'upgrade'; 
		proxy_set_header Host $host; 
		proxy_cache_bypass $http_upgrade; 
	} 
}

When I modify my hosts file to point asimplereader.com to my digital ocean VM (at 162.243.199.7) NGinx doesn’t apply the rule and the page 404s. If I change the rule to listen to *:80 instead of asimplereader.com:80, then the rule starts working.

Why is it not allowing me to use the hostname?

Subscribe
Share

fschwiet Yeah, but if you check my comment/the answer given you’ll see its the server_name property that allows multiple ‘virtual hosts’, not the listen property. The listen property just binds the software to listen on a particular IP & port.

If you want to host multiple sites, just modify the the server_name directive and making one of them a default_server.

Listen directive do nothing for virtual hosting but listen on a specific interface.

Since every droplet will only have 3 network interface max (ipv4, ipv6 & loopback interface), listen direct don’t have much use, unless you want to control which website to face ipv4 & ipv6.

MarkF, I’m listening by hostname so that I can host multiple sites (each with their own hostname) on the same virtual machine.

Why do you need it to listen using the hostname? I’m not sure if thats correct syntax, if it is then maybe your droplet can’t resolve that or something?

I would change that block to this:

server { 
    listen 1.2.3.4:80; 
    server_name asimplereader.com;
    location ~ ^/ { 
        proxy_pass http://localhost:8081; 
        proxy_http_version 1.1; 
        proxy_set_header Upgrade $http_upgrade; 
        proxy_set_header Connection 'upgrade'; 
        proxy_set_header Host $host; 
        proxy_cache_bypass $http_upgrade; 
    } 
}

Does that do anything for you? Note - replace 1.2.3.4 with your IP…

This comment has been deleted

Note that in every case I am accessing the site at http://asimplereader.com


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

You need a server_name directive. You don’t have one.

There are three things that can go in a listen directive - an IP address, a port, and a path. Host names do not go there. That’s why listen *:80 works.

Try this:

server { 
    listen 80;
    server_name asimplereader.com;
    location ~ ^/ { 
        proxy_pass http://localhost:8081; 
        proxy_http_version 1.1; 
        proxy_set_header Upgrade $http_upgrade; 
        proxy_set_header Connection 'upgrade'; 
        proxy_set_header Host $host; 
        proxy_cache_bypass $http_upgrade; 
    } 
}

You need a server_name directive. You don’t have one.

There are three things that can go in a listen directive - an IP address, a port, and a path. Host names do not go there. That’s why listen *:80 works.

Try this:

server { 
    listen 80;
    server_name asimplereader.com;
    location ~ ^/ { 
        proxy_pass http://localhost:8081; 
        proxy_http_version 1.1; 
        proxy_set_header Upgrade $http_upgrade; 
        proxy_set_header Connection 'upgrade'; 
        proxy_set_header Host $host; 
        proxy_cache_bypass $http_upgrade; 
    } 
}

This comment has been deleted

This comment has been deleted

This comment has been deleted

This comment has been deleted

This comment has been deleted

This comment has been deleted

This comment has been deleted

This comment has been deleted