Home > View Post

Nginx Configuration File


posted May 04, 2008 11:13 ago by gersham


A friend was asking for configuration instructions for Nginx so I thought I'd post it up here for reference (for myself even).

Nginx is the 6th most popular webserver on the Internet and serves up over a million domains. (Wikipedia).

It's easy to configure and a heck of a lot more light weight than Apache. For most web hosting and proxying requirements it's a pretty good choice. I do most of my Rails hosting through an Nginx proxy to a cluster of mongrel servers.

First you'll need to download and install Nginx, it's available within the package management system of Ubuntu and other distributions.

On Ubuntu there's a directory created by the default install called /etc/nginx/sites-enabled/ which you can place Nginx config files in. If you don't have this include config line in your /etc/nginx/nginx.conf you'll need to create it.

For instance, for this site I use the following in the contents of /etc/nginx/sites-enabled/meharg.com



upstream meharg.com_mongrel {
        server 127.0.0.1:3000;
        server 127.0.0.1:3001;
        server 127.0.0.1:3002;
}

upstream meharg.com_apache_svn {
        server 127.0.0.1:2999;
}

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

        access_log  /var/log/nginx/meharg.com.access.log;

        #
        # Deny .SVN files
        #

        location ~ /\.svn {
                deny  all;
        }

        #
        # Serve static files directly
        #

        location ~ ^/(images|javascripts|stylesheets)/ {
                root /var/www/apps/meharg.com/public;
                #access_log        off;
                expires           30d;
        }

        # 
        # All the rest to the mongrel cluster
        #

        location / {

                proxy_set_header   Host             $host;
                proxy_set_header   X-Real-IP        $remote_addr;
                proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

                proxy_pass  http://meharg.com_mongrel;
        }
}