How to make nginx to listen to server_name:port

2019-02-07 22:44发布

问题:

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.

回答1:

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



标签: nginx config