Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make nginx to listen to server_name:port

Tags:

nginx

config

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.

like image 974
Serenade Avatar asked Nov 29 '12 20:11

Serenade


People also ask

Can Nginx listen on any port?

By default, the Nginx HTTP server listens for inbound connections and connects to port 80, which is the default web port. However, the TLS configuration, which is not supported in Nginx by default, listens to port 443 for secure connections.

Is server_name required in Nginx?

If no server_name is defined in a server block then nginx uses the empty name as the server name. nginx versions up to 0.8. 48 used the machine's hostname as the server name in this case. If a server name is defined as “ $hostname ” (0.9.

What does server_name _ mean in Nginx?

Here, the server name is set to an empty string that will match requests without the “Host” header field, and a special nginx's non-standard code 444 is returned that closes the connection. On the other side, server_name _; defines an invalid server names which never intersect with any real name.


1 Answers

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

like image 97
Alessandro Vendruscolo Avatar answered Sep 24 '22 09:09

Alessandro Vendruscolo