How to make nginx to listen to server_name:port

NginxConfig

Nginx Problem Overview


In my nginx conf file, I have :

  listen       80;
    server_name  $hostname;

however if I do netstat I see that it is listening on 0.0.0.0:80

what I want to happen, is the nginx to listen to $hostname:80 , is there a way to configure it to do that?

I tried different settings with no success so far. Appreciate your help.

Nginx Solutions


Solution 1 - Nginx

The server_namedocs directive is used to identify virtual hosts, they're not used to set the binding.

netstat tells you that nginx listens on 0.0.0.0:80 which means that it will accept connections from any IP.

If you want to change the IP nginx binds on, you have to change the listendocs rule.
So, if you want to set nginx to bind to localhost, you'd change that to:

listen 127.0.0.1:80;

In this way, requests that are not coming from localhost are discarded (they don't even hit nginx).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSerenadeView Question on Stackoverflow
Solution 1 - NginxAlessandro VendruscoloView Answer on Stackoverflow