Question

Nginx rule doesn't work when listening to specific hostname

  • Posted on August 28, 2014
  • fschwietAsked by fschwiet

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?

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

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

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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