1. 程式人生 > >【轉載】nginx 配置檔案詳解

【轉載】nginx 配置檔案詳解

user  www www;#使用哪個使用者啟動nginx 前面是使用者,後面是組
worker_processes 4;#nginx工作的程序數量

#[ debug | info | notice | warn | error | crit ]   錯誤日誌的級別及位置
error_log  /var/htdocs/logs/nginx_error.log  crit;

pid /usr/local/nginx/nginx.pid;#程序檔案
worker_rlimit_nofile 51200;#一個nginx程序開啟的最多檔案描述符數目,理論值應該是最多開啟檔案數(ulimit -n)與nginx程序數相除,但是nginx分配請求並不是那麼均勻,所以最好與ulimit -n的值保持一致。

#工作模式及連線數上限
events
{
     # use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];
     use epoll;  #使用epoll(linux2.6的高效能方式)
     worker_connections 51200; #每個程序最大連線數(最大連線=連線數x程序數)
}

#設定http伺服器
http
{
     include       mime.types;#副檔名與檔案型別對映表
     default_type  application/octet-stream;#預設檔案型別
     #日誌檔案格式
     log_format  main  ‘$remote_addr – $remote_user [$time_local] $request ‘
                  ‘"$status" $body_bytes_sent "$http_referer" ‘
                  ‘"$http_user_agent" "$http_x_forwarded_for"’;

    log_format download  ‘$remote_addr – $remote_user [$time_local] ‘
                             ‘"$request" $status $bytes_sent ‘
                             ‘"$http_referer" "$http_user_agent" ‘
                             ‘"$http_range" "$sent_http_content_range"’;

     charset  gb2312,utf-8;#預設編碼    
     server_names_hash_bucket_size 128;#伺服器名字的hash表大小    
     sendfile on;     #開啟高效檔案傳輸模式
     #以下兩個選項用於防止網路阻塞
     tcp_nopush     on;
     tcp_nodelay on;

     keepalive_timeout 300; #超時時間

     #FastCGI是為了改善網站的效能--減少資源佔用,提高訪問速度.有關fastCGI的詳細資料請參閱:http://www.fastcgi.com
     fastcgi_connect_timeout 300;
     fastcgi_send_timeout 300;
     fastcgi_read_timeout 300;
     fastcgi_buffer_size 128k;
     fastcgi_buffers 4 256k;
     fastcgi_busy_buffers_size 256k;
     fastcgi_temp_file_write_size 256k;
     fastcgi_temp_path /dev/shm;

     gzip on;     #開啟gzip壓縮
     gzip_min_length  1k;      #最小壓縮檔案大小
     gzip_buffers     4 8k;     #壓縮緩衝區
     gzip_http_version 1.1;      #壓縮版本(預設1.1,前端為squid2.5使用1.0)
    #壓縮型別,預設就已經包含text/html 所以下面就不用再寫了,當然寫上去的話,也不會有問題,但是會有一個warn
     gzip_types       text/plain application/x-javascript text/css text/html text/javascript application/xml;
     #錯誤頁面
     error_page 404 http://www.freeopens.com;
     error_page 403 http://www.freeopens.com;

     client_max_body_size 20m;      #上傳檔案大小限制
     #設定請求緩
     client_header_buffer_size 16k;
     large_client_header_buffers 4 64k;
     #設定負載均衡的伺服器列表
     #如果在同一臺機器上,單獨起4組獨立的php-cgi程序(每組8個子程序),效能應該不如1組php-cgi程序(32個子程序),因為1組程序,eaccelerator的PHP二進位制檔案快取是共享的,1組程序命中率較高。
     #不過好處是,碰到某組的php假死的話,其他埠就可以接管了,我實測下來似乎發生502錯誤的概率降低了很多,或者說我這樣配置以後還沒有遇到
     upstream mysvr {
             #weigth引數表示權值,權值越高被分配到的機率越大
             #本機上的Squid開啟3128埠
             server 192.168.8.1:3128 weight=5;
             server 192.168.8.2:80   weight=1;
             server 192.168.8.3:80   weight=6;
     }
     #虛擬主機的配置
     server
     {
             listen       80;
             server_name  www.freeopens.com;
             index index.html Index.html index.htm index.php;
             root  /var/htdocs/freeopens;
             if (-d $request_filename)
             {
                    rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
             }
             #設定本虛擬主機的訪問日誌
             access_log  logs/www.freeopens.com.access.log  main;

             location ~ .*\.php?$
             {
                  include fcgi.conf;
                  fastcgi_pass  127.0.0.1:9000;
                  fastcgi_index index.php;
             }
             #如果訪問 /img/*, /js/*, /css/* 資源,則直接取本地檔案,不通過squid
             #如果這些檔案較多,不推薦這種方式,因為通過squid的快取效果更好
             location ~ ^/(img|js|css)/  {
                     root    /var/htdocs/freeopens;
                     expires 24h;
             }

             #對 "/" 啟用負載均衡
             location / {
                     proxy_pass      http://127.0.0.1;
                     proxy_redirect          off;
                     proxy_set_header        Host $host;
                     proxy_set_header        X-Real-IP $remote_addr;
                     proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                     client_max_body_size    10m;
                     client_body_buffer_size 128k;
                     proxy_connect_timeout   90;
                     proxy_send_timeout      90;
                     proxy_read_timeout      90;
                     proxy_buffer_size       4k;
                     proxy_buffers           4 32k;
                     proxy_busy_buffers_size 64k;

                     proxy_temp_file_write_size 64k;
             }

             #設定檢視Nginx狀態的地址
             location /NginxStatus {
                     stub_status             on;
                     access_log              on;
                     auth_basic              "NginxStatus";
                     auth_basic_user_file  conf/htpasswd;
             }
     }
}