Posts tagged with vhosts:

Default VHost in Nginx

6 May 2009  •  Comments

I recently started using nginx as my main web server.  While it's a little tricky at first to figure out (documentation is all Russian), there are a few wiki's that help a lot.  One of the things I couldn't figure out was how it determines the "default" vhost.  In Apache this is the first host found in the Vhost configs (or alphabetically in sites-enabled/*) however this didn't seem to be the case with Nginx.

To make matters worse the way Nginx determines where to send traffic is significantly different than say Apache so some very weird things were happening with a couple of my hosts.  So after a bit of reading I found the missing link.   Logically enough it's the word default, the where is the weird part:

When defining a vhost it looks something like this:

server {
    listen 80;
    server_name server1.example.com;
    location / {
        root /path/to/server1.example.com/web/;
        expires 1d;
    }

To make this the default put "default" on the listen directive:

server {
    listen 80 default;
    server_name server1.example.com;
    location / {
        root /path/to/server1.example.com/web/;
        expires 1d;
    }

No more guessing or leaving it to nginx to determine the host based on URI.

Copyright © 2004 - 2013 Nomad.Works | Made in Ottawa