Example of nginx.conf

Download example of nginx config file : nginx.conf

[codesyntax lang=”apache”]

# nobody nobody but you ...
user  nobody;

# max concurrent requests = worker_processes * worker_connections
worker_processes  8;

# 4 cpus (double core)
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

# max opened files of each process
worker_rlimit_nofile 51200;

# only open for dev environment
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

# pid number for nginx
pid        logs/nginx.pid;

events {
    # for large I/O
    use epoll;
    # max connections for each process
    worker_connections  51200;
}

# handle http requests
http {
    # map of files and mime-types
    include       mime.types;

    # default file type of http (can be text/html)
    default_type  application/octet-stream;

    # format of log file
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    # access log
    #access_log  logs/access.log  main;

    # timeout of keepalive connection
    keepalive_timeout  650;

    # efficient file transfer mode, using both sendfile and TCP_CORK, only for unix
    sendfile        on;
    tcp_nopush      on;

    # start gzip
    gzip  on;
    # min size of file
    gzip_min_length  1000;
    # buffer size
    gzip_buffers     4 8k;
    # file types which need gzip
    gzip_types       text/* text/css application/javascript application/x-javascript;
    # compression ratio (1 for least, 9 for most)
    gzip_comp_level  9;
    # whether allow compression for proxy request
    gzip_proxied     any;
    # header of "Vary: Accept-Encoding"
    gzip_vary        on;
    # gzip version (use 1.0 for squid)
    gzip_http_version 1.1

    # output buffer size
    output_buffers   4 32k;

    # size of output package
    postpone_output  1460;

    # for big http header (especially large cookies)
    client_header_buffer_size 128k;
    large_client_header_buffers 4 256k;

    # default charset
    charset utf-8;

    ########################################################################

    # deny access of default
    server {
        listen 80;
        server_name null;
        location / {
            root /dev/null;
            log_not_found off;
        }
    }

    # for magento-1.4.2-stable ################################################
    server {
        listen       80;
        server_name  www.mglocal.com;

        root /data/vhosts/mglocal/public_html;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location / {
            index  index.php index.html index.htm;
            if (-f $request_filename) {
                expires 30d;
                break;
            }
            if (-d $request_filename) {
                break;
            }
            if (!-e $request_filename) {
                # redirect all requests to magento
                rewrite ^(.+)$ /index.php last;
            }
        }

        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

            # type of project you are running
            fastcgi_param  MAGE_RUN_TYPE website;
            # the code you set in backend
            fastcgi_param  MAGE_RUN_CODE ft;

            include        fastcgi.conf;
        }

        # any access to /app/etc is forbidden
        location /app/etc {
            deny all;
        }

        access_log  /data/weblog/www.mglocal.com.access.log;
        error_log  /data/weblog/www.mglocal.com.error.log;
    }
    ###########################################################################

    # upsteam polling for www.kimbs.cn ########################################
    upstream www.kimbs.cn  {
	    server	127.0.0.1:8000;
	    server	127.0.0.1:8001;
    }

    server {
        listen	80;
        server_name  www.kimbs.cn;
        location / {
            # proxy
            proxy_pass	http://www.kimbs.cn;
            proxy_set_header	Host	$host;
            proxy_set_header	X-Real-IP	$remote_addr;
            proxy_set_header	X-Forwarded-For	$proxy_add_x_forwarded_for;
        }
        access_log  /data/weblog/www.kimbs.cn.access.log  access;
        error_log  /data/weblog/www.kimbs.cn.error.log  crit;
    }

    # 1st server
    server {
        listen       8000;
        server_name  127.0.0.1;
        root /data/vhosts/kbs/public_html;
        error_page   500 502 503 504  /50x.html;

        location = /50x.html {
            root   html;
        }

        location / {
            index  index.php index.html index.htm;
            if (-f $request_filename) {
                break;
            }
            if (-d $request_filename) {
                break;
            }
            rewrite ^(.+)$ /index.php last;
        }

        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi.conf;
        }

        access_log  /data/weblog/www.kimbs.cn.access.log  access;
        error_log  /data/weblog/www.kimbs.cn.error.log  crit;
    }

    # 2nd server
    server {
        listen       8001;
        server_name  127.0.0.1;
        root /data/vhosts/kbs2/public_html;
        error_page   500 502 503 504  /50x.html;

        location = /50x.html {
            root   html;
        }

        location / {
            index  index.php index.html index.htm;
            if (-f $request_filename) {
                break;
            }
            if (-d $request_filename) {
                break;
            }
            rewrite ^(.+)$ /index.php last;
        }

        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi.conf;
        }

        access_log  /data/weblog/www.kimbs.cn.access.log  access;
        error_log  /data/weblog/www.kimbs.cn.error.log  crit;
    }
    ###########################################################################

    # for drupal ##############################################################
    server {
        listen 80;
        server_name myproj.local;

        access_log /data/weblog/myproj.local.access.log access;
        error_log /data/weblog/myproj.local.error.log crit;

        root /data/vhosts/myproj/public_html;

        # prevent '413 Request Entity Too Large'
        client_max_body_size 32m;

        # rewrite
        location / {
            index index.php index.html index.htm;
            if (!-e $request_filename) {
                rewrite ^/(.*)$ /index.php?q=$1 last;
            }
        }

        # hide protected files
        location ~* .(engine|inc|info|install|module|profile|po|sh|.*sql|theme|tpl(.php)?|xtmpl)$|^(code-style.pl|Entries.*|Repository|Root|Tag|Template)$ {
            deny all;
        }

        # hide backup_migrate files
        location ~* ^/files/backup_migrate {
            deny all;
        }

        # serve static files directly
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$ {
            access_log  off;
            expires     30d;
        }

        # js and css files
        location ~ .*\.(js|css|xml)?$ {
            expires     1h;
        }

        # serve php
        location ~ \.php$ {
            fastcgi_pass                   127.0.0.1:9000;
            fastcgi_index                  index.php;
            fastcgi_param                  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_split_path_info        ^(.+\.php)(.*)$;
            fastcgi_param PATH_INFO        $fastcgi_path_info;

            # prevent timeout
            fastcgi_send_timeout           1800;
            fastcgi_read_timeout           1800;
            fastcgi_connect_timeout        1800;
            fastcgi_buffers                8 128k;

            include                        fastcgi.conf;
            include                        fastcgi_params;
        }
    }
    ###########################################################################

}

[/codesyntax]

Posted in Nginx | Tagged , , , | Leave a comment