Welcome! Log In Create A New Profile

Advanced

Re: server_name and listen behaviour

February 12, 2013 11:58PM
On Feb 13, 2013, at 8:49 , ivan babrou wrote:

> On 12 February 2013 23:06, Igor Sysoev <igor@sysoev.ru> wrote:
> 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)" ?
>
> No, there's no such thing.

Then it should work with just reload.

> > 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.
>
> This is what netstat says:
>
> callisto ~ # netstat -ntl
> Active Internet connections (only servers)
> Proto Recv-Q Send-Q Local Address Foreign Address State
> tcp 0 0 127.0.0.1:21213 0.0.0.0:* LISTEN
> tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
> tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
> tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN
> tcp6 0 0 ::1:21213 :::* LISTEN
> tcp6 0 0 :::22 :::* LISTEN
> tcp6 0 0 :::5432 :::* LISTEN
>
> Here I only see 0.0.0.0:80 and 127.0.0.1:80 looks like a subset for this. So why can't nginx use the same socket?

If there is "listen *:80", nginx uses this single socket to accept all connections to 80 port and
then learn local address with getsockaddr(), see Ruslan email for details.


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

_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

server_name and listen behaviour

ivan babrou 956 February 12, 2013 12:12PM

Re: server_name and listen behaviour

Igor Sysoev 719 February 12, 2013 02:08PM

Re: server_name and listen behaviour

ivan babrou 482 February 12, 2013 11:52PM

Re: server_name and listen behaviour

Igor Sysoev 719 February 12, 2013 11:58PM

Re: server_name and listen behaviour

ivan babrou 558 February 13, 2013 12:08AM

Re: server_name and listen behaviour

Igor Sysoev 728 February 13, 2013 12:20AM

Re: server_name and listen behaviour

Ruslan Ermilov 435 February 12, 2013 02:16PM

Re: server_name and listen behaviour

Igor Sysoev 726 February 12, 2013 02:34PM

Re: server_name and listen behaviour

ivan babrou 487 February 12, 2013 11:58PM

Re: server_name and listen behaviour

Ruslan Ermilov 611 February 13, 2013 03:20AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 168
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready