fschwiet
By:
fschwiet

Nginx rule doesn't work when listening to specific hostname

August 28, 2014 6.8k views

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?

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

  • 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..

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

  • If you want to host multiple sites, just modify the the servername directive and making one of them a defaultserver.

    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.

  • 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.

19 Answers

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; 
    } 
}
  • Weird, listen <hostname>:<port> was working on a local virtual machine, provisioned via the same scripts as my digital ocean environment. server_name seems to be working, though it seems to have revealed another issue (which is probably my bug).

Have another answer? Share your knowledge.