server_name and listen behaviour

Igor Sysoev igor at sysoev.ru
Tue Feb 12 19:06:25 UTC 2013


On Feb 12, 2013, at 21:10 , ivan babrou wrote:

> Hi, I have a question. It's better to describe with example
> 
> I point one.local and two.local to 127.0.0.1 and create following servers in config for nginx:
> 
> server {
>   listen 80;
>   server_name one.local;
> 
>   location / {
>     return 404;
>   }
> }
> 
> server {
>   listen two.local:80;
>   server_name two.local;
> 
>   location / {
>     return 403;
>   }
> }
> 
> If I request one.local then 403 is returned. If i change listen for one.local to one.local:80 then 404 correctly returned, but only after restart, reload doesn't help (that's probably bug too).

Is there in error_log error something like "listen(127.0.0.1:80) failed (98: Address already in use)" ?

> I expect to get 404 if i request one.local even if I listen on all addresses. Am I right?


No. Your first configuration is

server {
   listen *:80;
   server_name  one.local;
}

server {
  listen 127.0.0.1:80;
  server_name  two.local;
}

A client connects to 127.0.0.1:80, so nginx uses the second server to processes request.
When you change listen in the first server to 127.0.0.1:80, nginx has two servers for this
listen port and chooses server using "Host" header.


--
Igor Sysoev
http://nginx.com/support.html



More information about the nginx-devel mailing list