# What I found out about nginx

**URL:** <https://www.ruby-forum.com/t/what-i-found-out-about-nginx/113286>\
**Category:** Deployment\
**Created:** [August 29, 2007, 3:20pm UTC](https://www.ruby-forum.com/t/what-i-found-out-about-nginx/113286 "2007-08-29T15:20:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dxk3355](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@dxk3355](https://www.ruby-forum.com/u/dxk3355)\
**Post date:** [August 29, 2007, 3:20pm UTC](https://www.ruby-forum.com/t/what-i-found-out-about-nginx/113286/1 "2007-08-29T15:20:51Z")

</div>

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](http://domain1.com), [www.domain1.com](http://www.domain1.com), [domain2.com](http://domain2.com),  
[www.domain2.com](http://www.domain2.com) all point to the machine where nginx is running):

http {  
server {  
listen 80;  
server\_name [www.domain1.com](http://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](http://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](http://www.domain2.com) or [domain2.com](http://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](http://domain1.com:80);  
server\_name [domain1.com](http://domain1.com) alias [www.domain1.com](http://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](http://domain2.com). It  
seems that listen [somedomain.com:80](http://somedomain.com:80); doesn’t do anything, but it was a  
nice try 🙂

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

---

<div class="post-metadata">

**Author:** ![dxk3355](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@dxk3355](https://www.ruby-forum.com/u/dxk3355)\
**Post date:** [August 29, 2007, 3:32pm UTC](https://www.ruby-forum.com/t/what-i-found-out-about-nginx/113286/2 "2007-08-29T15:32:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dxk3355](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@dxk3355](https://www.ruby-forum.com/u/dxk3355)\
**Post date:** [August 29, 2007, 5:45pm UTC](https://www.ruby-forum.com/t/what-i-found-out-about-nginx/113286/3 "2007-08-29T17:45:03Z")

</div>

Le mercredi 29 aoÃ»t 2007 Ã 15:20 +0200, D. Krmpotic a Ã©crit :

> - if you go to [domain2.com](http://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](http://www.domain2.com) or [domain2.com](http://domain2.com)

Hello D. Krmpotic,

With this config, Nginx doesn’t know “[domain1.com](http://domain1.com)” and “[domain2.com](http://domain2.com)” and  
redirects to the first vhost found.

http {

server {  
listen 80;  
server\_name [www.domain1.com](http://www.domain1.com) [domain1.com](http://domain1.com);

```
[...]

```

}  
}

http {

server {  
listen 80;  
server\_name [www.domain2.com](http://www.domain2.com) [domain2.com](http://domain2.com);

```
[...]

```

}  
}

and now it works !

Best,

J.

---

<div class="post-metadata">

**Author:** ![dxk3355](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@dxk3355](https://www.ruby-forum.com/u/dxk3355)\
**Post date:** [August 29, 2007, 6:52pm UTC](https://www.ruby-forum.com/t/what-i-found-out-about-nginx/113286/4 "2007-08-29T18:52:43Z")

</div>

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](http://domain2.com), it just goes to the first vhosts entry (for [domain1.com](http://domain1.com)).

JÃ©rÃ©my DIERX wrote:

> Le mercredi 29 aoÃ»t 2007 Ã 15:20 +0200, D. Krmpotic a Ã©crit :
> 
> >

---

<div class="post-metadata">

**Author:** ![dxk3355](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@dxk3355](https://www.ruby-forum.com/u/dxk3355)\
**Post date:** [August 30, 2007, 3:07pm UTC](https://www.ruby-forum.com/t/what-i-found-out-about-nginx/113286/5 "2007-08-30T15:07:26Z")

</div>

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](http://www.brightbox.co.uk) - UK Rails Xen Hosting

---

<div class="post-metadata">

**Author:** ![dxk3355](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@dxk3355](https://www.ruby-forum.com/u/dxk3355)\
**Post date:** [August 31, 2007, 12:40am UTC](https://www.ruby-forum.com/t/what-i-found-out-about-nginx/113286/6 "2007-08-31T00:40:38Z")

</div>

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](http://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](http://www.brightbox.co.uk) - UK Rails Xen Hosting
