Question
Nginx rule doesn't work when listening to specific hostname
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?
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.
×
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:
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.