How to setup Nginx

Install(in ubuntu)

sudo apt-get install nginx

Main configuration

# update default configuration
sudo nano /etc/nginx/nginx.conf

#
# /etc/nginx/nginx.conf
#

# user and group to run as
user       www-data www-data;

# number of nginx workers
# the same as the core numbers
worker_processes  2;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

worker_rlimit_nofile 8192;
events {
    # Number of worker connections. 2048 is a good default
    worker_connections  2048;
    use epoll;
}

http {
    upstream frontends {
        server 127.0.0.1:8888;            
        #server 127.0.0.1:8891;
        #server 127.0.0.1:8892;
        #server 127.0.0.1:8893;
        #server 127.0.0.1:8894;
    }

    server_tokens on;

    # pull in mime-types.
    include  mime.types;

    # set a default type for the rare situation that
    # nothing matches from the mimie-type include
    default_type application/octet-stream;

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

    gzip on;
    gzip_min_length  1024;
    gzip_buffers     16 8k;   
    gzip_http_version 1.1; 
    gzip_comp_level  2;
    gzip_types       text/plain text/css application/x-javascript text/xml 
                        application/xml application/xml+rss text/javascript;
    gzip_vary on;

    sendfile     on;
    tcp_nopush   on;
    tcp_nodelay  on;

    keepalive_timeout  60;

    client_max_body_size    10m;
    client_body_buffer_size 256k;

    # proxy settings
    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_set_header        X-Scheme        $scheme;
    proxy_connect_timeout   90;
    proxy_send_timeout      90;
    proxy_read_timeout      90;
    proxy_buffers           32 8k;

    include /etc/nginx/sites-enabled/*;
}

A site's configuration(For example:poweredsites.org)

sudo nano /etc/nginx/sites-enabled/poweredsites.conf

#
# /etc/nginx/sites-enabled/poweredsites.conf
# 
server {
    listen   80;
    server_name  static.poweredsites.org;
    access_log  /var/log/nginx/poweredsites.static.log;

    location / {
        root /mnt/ebs/sites/poweredsites/poweredsites/static/;
        if ($query_string) {
            expires max;
        }
    }
}

server {
    listen   80;
    server_name  poweredsites.org;
    access_log  /var/log/nginx/poweredsites.access.log;

    location / {     
        proxy_pass http://frontends;
    }
}

server {
    listen   80;
    server_name  *.poweredsites.org;
    access_log  /var/log/nginx/poweredsites.access.log;

    # permanent redirect www.poweredsites.org to poweredsites.org
    if ($host ~* ^www\.(.*)$) {
        set $domain $1;
        rewrite ^(.*)$ http://$domain$1 permanent;
    }

    location / {     
        proxy_pass http://frontends;
    }
}
Felinx Lee   August 27, 2010
Comments blog comments powered by Disqus