What I found out about nginx

Hi!

I played around with it a little bit and found out the following:

server_name in a vhost entry doesn’t really guarantee that only requests
coming from specified domain will get served by that vhost.

For example (supposing that domain1.com, www.domain1.com, domain2.com,
www.domain2.com all point to the machine where nginx is running):

http {
server {
listen 80;
server_name www.domain1.com;

    location / {
        index index.html;
        root  /var/www/domain1;
    }
}

server {
    listen          80;
    server_name     www.domain2.com;

    location / {
        index index.html;
        root  /var/www/domain2;
    }
}

}

  • if you go to domain2.com (without www), you’ll land in
    /var/www/domain1 (one!)
  • If you delete a vhost for domain2, then you’ll land in
    /var/www/domain1 when going to www.domain2.com or domain2.com

So it seems that if there is no exact match, it just takes the first
vhost. Maybe this is well known, but it’s still confusing. I think this
is not ideal implementation. I tried to do the following:

http {
server {
listen domain1.com:80;
server_name domain1.com alias www.domain1.com;

    location / {
        index index.html;
        root  /var/www/domain1;
    }
}

server {
    listen          domain2.com:80;
    server_name     domain2.com alias www.domain2.com;

    location / {
        index index.html;
        root  /var/www/domain2;
    }
}

}

That of course solves the “www” problem, but I still don’t agree that it
just picks one vhost… it should display an error!

The second problem remains, so if I delete the vhosts entry for domain2,
I still land in /var/www/domain1 when going to (www.)domain2.com. It
seems that listen somedomain.com:80; doesn’t do anything, but it was a
nice try :slight_smile:

Another important issue is that when sending HUP (kill 0) signal to
nginx for reloading configuration (= saying /etc/init.d/nginx reload)
and there is a syntactical error in the conf file, you won’t get any
error message. It just won’t reload the new script.

If you stop and then try to start the server, you will get something
like:
16326#0: directive “server” has no opening “{” in
/usr/local/nginx/conf/nginx.conf:67

Maybe there is a way to check the conf file like in apache. The perfect
solution would be if reload signal would let you know if there were any
issues with the file…

That’s it… hope it helps someone and also that you reply with your
thoughts about those two issues… Overall I like nginx very much, but I
just want to learn exactly how it works.

Thank you,
David

Something else: reload (/etc/init.d/nginx reload) doesn’t seem to work
well everytime (in addition to not telling you the errors)… I’m not
100% sure about this but I won’t use it anymore for now… I’ll just
restart the server.

Le mercredi 29 août 2007 à 15:20 +0200, D. Krmpotic a écrit :

  • if you go to domain2.com (without www), you’ll land in
    /var/www/domain1 (one!)
  • If you delete a vhost for domain2, then you’ll land in
    /var/www/domain1 when going to www.domain2.com or domain2.com

Hello D. Krmpotic,

With this config, Nginx doesn’t know “domain1.com” and “domain2.com” and
redirects to the first vhost found.

http {

server {
listen 80;
server_name www.domain1.com domain1.com;

[...]

}
}

http {

server {
listen 80;
server_name www.domain2.com domain2.com;

[...]

}
}

and now it works !

Best,

J.

Jérémy thanx, but I think you missed “alias” lines in second example and
the point of the first example was that when it doesn’t find
domain2.com, it just goes to the first vhosts entry (for domain1.com).

Jérémy DIERX wrote:

Le mercredi 29 août 2007 à 15:20 +0200, D. Krmpotic a écrit :

Hi David,

in the first case, this is how both Apache and Lighttpd behave. The
first defined settings act like a catch-all. It seems expected to me.

John.


http://www.brightbox.co.uk - UK Rails Xen Hosting

Thank you… yes I guess they do… I wasn’t sure how nginx behaved, so I
played with it. I hoped it wouldn’t do it that way, but what can we do.
Everything’s nice. I also found that this simplifies some things:

server_name domain1.com *.domain1.com;

Thank you all,
David

John L. wrote:

Hi David,

in the first case, this is how both Apache and Lighttpd behave. The
first defined settings act like a catch-all. It seems expected to me.

John.


http://www.brightbox.co.uk - UK Rails Xen Hosting