server_name and listen behaviour
Ruslan Ermilov
ru at nginx.com
Wed Feb 13 08:18:16 UTC 2013
On Wed, Feb 13, 2013 at 08:56:56AM +0400, ivan babrou wrote:
> On 12 February 2013 23:14, Ruslan Ermilov <ru at nginx.com> wrote:
>
> > On Tue, Feb 12, 2013 at 09:10:17PM +0400, 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).
> > > I expect to get 404 if i request one.local even if I listen on all
> > > addresses. Am I right?
> >
> > 127.0.0.1:80 and *:80 are two different socket addresses,
> > and when you connect to 127.0.0.1:80, the first one will
> > be used (two.local in your case). This is not specific
> > to nginx, it's how sockets work.
> >
> > nginx by default optimizes such a config by creating only
> > one listening socket [1], and pessimizes by using getsockaddr()
> > to differentiate between multiple sockaddrs. This optimization
> > can be turned off by using "bind" parameter (or some other
> > parameters, see [1] for details). Nevertheless, 127.0.0.1:80
> > and *:80 are still two different addresses, and 127.0.0.1:80
> > is more specific than *:80.
>
>
> See below, I only see one listening address in netstat.
>
> As to "reload not working", I cannot reproduce this:
[...]
> I have listen *:80 before reload, then change it to listen one.local:80 and
> reaload. nginx/1.2.6
>
> Before reload:
>
> callisto ~ # netstat -ntl | fgrep ':80'
> tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
>
> After reload:
>
> callisto ~ # netstat -ntl | fgrep ':80'
> tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
>
> After restart:
>
> callisto ~ # netstat -ntl | fgrep ':80'
> tcp 0 0 127.0.0.1:80 0.0.0.0:* LISTEN
>
> Did you have one.local and two local when you check this?
Just for the record. Here's how it works on BSD-like systems:
: $ ps 36455
: PID TT STAT TIME COMMAND
: 36455 s004 S+ 0:00.00 nginx: master process objs/nginx -p . -c x.conf
:
: $ cat x.conf
: [...]
: http {
: server {
: listen *:8000;
: server_name one;
: return 200 "$server_name\n";
: }
: server {
: listen 127.0.0.1:8000;
: server_name two;
: return 200 "$server_name\n";
: }
: }
:
: $ echo 'GET http://one/' | nc 127.0.0.1 8000
: two
:
: $ echo 'GET http://two/' | nc 127.0.0.1 8000
: two
:
: $ netstat -anL | grep 8000
: 0/0/128 *.8000
:
: $ vim x.conf
: [...]
:
: $ cat x.conf
: [...]
: http {
: server {
: listen 127.0.0.1:8000;
: server_name one;
: return 200 "$server_name\n";
: }
: server {
: listen 127.0.0.1:8000;
: server_name two;
: return 200 "$server_name\n";
: }
: }
:
: $ kill -HUP 36455
:
: $ netstat -anL | grep 8000
: 0/0/128 127.0.0.1.8000
: $ echo 'GET http://one/' | nc 127.0.0.1 8000
: one
: $ echo 'GET http://two/' | nc 127.0.0.1 8000
: two
But as Igor already mentioned, trying this on Linux fails at
the "reload" step because Linux cannot create two sockets for
*:8000 and 127.0.0.1:8000:
2013/02/13 08:15:57 [notice] 17532#0: signal 1 (SIGHUP) received, reconfiguring
2013/02/13 08:15:57 [notice] 17532#0: reconfiguring
2013/02/13 08:15:57 [emerg] 17532#0: bind() to 127.0.0.1:8000 failed (98: Address already in use)
2013/02/13 08:15:57 [notice] 17532#0: try again to bind() after 500ms
2013/02/13 08:15:57 [emerg] 17532#0: bind() to 127.0.0.1:8000 failed (98: Address already in use)
2013/02/13 08:15:57 [notice] 17532#0: try again to bind() after 500ms
2013/02/13 08:15:57 [emerg] 17532#0: bind() to 127.0.0.1:8000 failed (98: Address already in use)
2013/02/13 08:15:57 [notice] 17532#0: try again to bind() after 500ms
2013/02/13 08:15:57 [emerg] 17532#0: bind() to 127.0.0.1:8000 failed (98: Address already in use)
2013/02/13 08:15:57 [notice] 17532#0: try again to bind() after 500ms
2013/02/13 08:15:57 [emerg] 17532#0: bind() to 127.0.0.1:8000 failed (98: Address already in use)
2013/02/13 08:15:57 [notice] 17532#0: try again to bind() after 500ms
2013/02/13 08:15:57 [emerg] 17532#0: still could not bind()
So nginx rolls back to previous configuration [1].
[1] http://nginx.org/libxslt/en/docs/control.html#reconfiguration
More information about the nginx-devel
mailing list