Nginx 配置文件nginx.conf的完整配置說明

[javascript] view plaincopy

  1. #用戶 用戶組  

  2. user www www;  

  3. #工作進程,根據硬件調整,有人說幾核cpu,就配幾個,我覺得可以多一點  

  4. worker_processes 5;  

  5. #錯誤日誌  

  6. error_log logs/error.log;  

  7. #pid文件位置  

  8. pid logs/nginx.pid;  

  9. worker_rlimit_nofile 8192;  

  10.   

  11. events {  

  12. #工作進程的最大連接數量,根據硬件調整,和前面工作進程配合起來用,儘量大,但是別把cpu跑到100%就行  

  13. worker_connections 4096;  

  14. }  

  15.   

  16. http {  

  17. include conf/mime.types;  

  18. #反向代理配置,可以打開proxy.conf看看  

  19. include /etc/nginx/proxy.conf;  

  20. #fastcgi配置,可以打開fastcgi.conf看看  

  21. include /etc/nginx/fastcgi.conf;  

  22.   

  23. default_type application/octet-stream;  

  24. #日誌的格式  

  25. log_format main '$remote_addr - $remote_user [$time_local] $status '  

  26. '"$request" $body_bytes_sent "$http_referer" '  

  27. '"$http_user_agent" "$http_x_forwarded_for"';  

  28. #訪問日誌  

  29. access_log logs/access.log main;  

  30. sendfile on;  

  31. tcp_nopush on;  

  32. #根據實際情況調整,如果server很多,就調大一點  

  33. server_names_hash_bucket_size 128; # this seems to be required for some vhosts  

  34.  

  35. #這個例子是fastcgi的例子,如果用fastcgi就要仔細看  

  36. server { # php/fastcgi  

  37. listen 80;  

  38. #域名,可以有多個  

  39. server_name domain1.com www.domain1.com;  

  40. #訪問日誌,和上面的級別不一樣,應該是下級的覆蓋上級的  

  41. access_log logs/domain1.access.log main;  

  42. root html;  

  43.   

  44. location / {  

  45. index index.html index.htm index.php;  

  46. }  

  47.  

  48. #所有php後綴的,都通過fastcgi發送到1025端口上  

  49. #上面include的fastcgi.conf在此應該是有作用,如果你不include,那麼就把fastcgi.conf的配置項放在這個下面。  

  50. location ~ .php$ {  

  51. fastcgi_pass 127.0.0.1:1025;  

  52. }  

  53. }  

  54.  

  55. #這個是反向代理的例子  

  56. server { # simple reverse-proxy  

  57. listen 80;  

  58. server_name domain2.com www.domain2.com;  

  59. access_log logs/domain2.access.log main;  

  60.  

  61. #靜態文件,nginx自己處理  

  62. location ~ ^/(p_w_picpaths|javascript|js|css|flash|media|static)/ {  

  63. root /var/www/virtual/big.server.com/htdocs;  

  64. #過期30天,靜態文件不怎麼更新,過期可以設大一點,如果頻繁更新,則可以設置得小一點。  

  65. expires 30d;  

  66. }  

  67.  

  68. #把請求轉發給後臺web服務器,反向代理和fastcgi的區別是,反向代理後面是web服務器,fastcgi後臺是fasstcgi監聽進程,當然,協議也不一樣。  

  69. location / {  

  70. proxy_pass http://127.0.0.1:8080;  

  71. }  

  72. }  

  73.  

  74. #upstream的負載均衡,weight是權重,可以根據機器配置定義權重。據說nginx可以根據後臺響應時間調整。後臺需要多個web服務器。  

  75. upstream big_server_com {  

  76. server 127.0.0.3:8000 weight=5;  

  77. server 127.0.0.3:8001 weight=5;  

  78. server 192.168.0.1:8000;  

  79. server 192.168.0.1:8001;  

  80. }  

  81.   

  82. server {  

  83. listen 80;  

  84. server_name big.server.com;  

  85. access_log logs/big.server.access.log main;  

  86.   

  87. location / {  

  88. proxy_pass http://big_server_com;  

  89. }  

  90. }  

  91. }   

  92.   

  93.   

  94.   

  95.     user    www www;           #運行NGINX所使用的用戶和組  

  96.   worker_processes     4;    #nginx進程數,建議按照cpu數目來指定,一般爲它的倍數,每個進程消耗約10M內存  

  97.   error_log       /data/logs/nginx/error.log  crit;  

  98.   pid             /elain/apps/nginx/nginx.pid;  

  99.   worker_rlimit_nofile  65535;   #nginx能打開文件的最大句柄數,最好與ulimit -n的值保持一致,使用ulimit -SHn 65535 設置  

  100.   events {  

  101.   use epoll;          #使用epoll的I/O模型  

  102.   connections 20000;  #每個進程允許的最多連接數  

  103.   worker_connections 65535;   #該值受系統進程最大打開文件數限制,需要使用命令ulimit -n 查看當前設置  

  104.   maxclients=65535*2  

  105.   }  

  106.   http {  

  107.   include mime.types;           #mine.types內定義各文件類型映像  

  108.   types {  

  109.   text/html  html;  

  110.   p_w_picpath/gif  gif;  

  111.   p_w_picpath/jpeg jpg;  

  112.   p_w_picpath/png  png;  

  113.   }  

  114.   default_type application/octet-stream;  #設置默認類型是二進制流,若未設置時,比如未加載PHP時,是不予解析,用瀏覽器訪問則出現下載窗口  

  115.   server_names_hash_bucket_size 128;    #不能帶單位!配置個主機時必須設置該值,否則無法運行Nginx或測試時不通過,該設置與server_names_hash_max_size 共同控制保存服務器名的HASH表,hash bucket size總是等於hash表的大小,並且是一路處理器緩存大小的倍數。若hash bucket size等於一路處理器緩存的大小,那麼在查找鍵的時候,最壞的情況下在內存中查找的次數爲2。第一次是確定存儲單元的地址,第二次是在存儲單元中查找鍵 值。若報出hash max size 或 hash bucket size的提示,則我們需要增加server_names_hash_max_size的值。  

  116.   client_header_buffer_size 128k;    #客戶端請求頭部的緩衝區大小,根據系統分頁大小設置,分頁大小可用命令getconf PAGESIZE取得  

  117.   large_client_header_buffers 4 128k;  #4爲個數,128k爲大小,默認是4k。申請4個128k。當http 的URI太長或者request header過大時會報414 Request URI too large或400 bad request,這是很有可能是cookie中寫入的值太大造成的,因爲header中的其他參數的size一般比較固定,只有cookie可能被寫入較 大的數據,這時可以調大上述兩個值,相應的瀏覽器中cookie的字節數上限會增大。  

  118.   client_max_body_size 8m;   #HTTP請求的BODY最大限制值,若超出此值,報413 Request Entity Too Large  

  119.   open_file_cache max=65535 inactive=20s;  #max指定緩存數量,建議和打開文件數一致,inactive是指經過多長時間文件沒被請求後刪除緩存。  

  120.   open_file_cache_valid 30s;  #指多長時間檢查一次緩存的有效信息  

  121.   open_file_cache_min_uses 1; #open_file_cache指令中的inactive參數時間內文件的最少使用次數,如果超過這個數字,文件描述符一直是在緩存中打開的,如上例, 如果有一個文件在inactive時間內一次沒被使用,它將被移除。  

  122.   server_tokens off;          #關閉錯誤時Nginx版本顯示  

  123.   #提高文件傳輸性能  

  124.   sendfile on;                #打開系統函數sendfile()支持  

  125.   tcp_nopush on;              #打開linux下TCP_CORK,sendfile打開時纔有效,作減少報文段的數量之用  

  126.   keepalive_timeout 60;       #keepalive超時時間  

  127.   tcp_nodelay on;             #打開TCP_NODELAY在包含了keepalive纔有效  

  128.   fastcgi_connect_timeout 300; #指定連接到後端FastCGI的超時時間  

  129.   fastcgi_send_timeout 300;    #向FastCGI傳送請求的超時時間,這個值是指已經完成兩次握手後向FastCGI傳送請求的超時時間。  

  130.   fastcgi_read_timeout 300;    #接收FastCGI應答的超時時間,這個值是指已經完成兩次握手後接收FastCGI應答的超時時間。  

  131.   fastcgi_buffer_size 64k;     #這裏可以設置爲fastcgi_buffers指令指定的緩衝區大小  

  132.   fastcgi_buffers 16 16k;      #指定本地需要用多少和多大的緩衝區來緩衝FastCGI的應答  

  133.   fastcgi_busy_buffers_size 128k;  #建議爲fastcgi_buffers的兩倍  

  134.   fastcgi_temp_file_write_size 128k;   #在寫入fastcgi_temp_path時將用多大的數據塊,默認值是fastcgi_buffers的兩倍,設置上述數值設置太小時若負載上來時可能報 502 Bad Gateway  

  135.   fastcgi_cache dingtm     #開啓FastCGI緩存並且爲其制定一個名稱,有效降低CPU負載,並且防止502錯誤  

  136.   fastcgi_cache_valid 200 302 1h;  #指定應答代碼緩存時間爲1小時  

  137.   fastcgi_cache_valid 301 1d;      #1天  

  138.   fastcgi_cache_valid any 1m;      #其它爲1分鐘  

  139.   fastcgi_cache_min_uses 1;        #緩存在fastcgi_cache_path指令inactive參數值時間內的最少使用次數                f  

  140.   gzip on;                    #打開GZIP壓縮,實時壓縮輸出數據流  

  141.   gzip_min_length  1k;        #從Content-Length中數值獲取驗證,小於1K會越壓越大  

  142.   gzip_buffers  4 16k;        #以16K爲單位4倍的申請內存做壓縮結果流緩存  

  143.   gzip_http_version 1.1;  

  144.   gzip_comp_level 3;          #壓縮比率1-9,1壓縮比最小處理速度最快,9壓縮比最大但處理最慢且耗CPU  

  145.   gzip_types      text/plain application/x-javascript text/css application/xml;  #壓縮類型  

  146.   include   vhosts/*.conf;      #虛擬主機  

  147.   }  

  148.   #虛擬主機  

  149.   server {  

  150.   listen 80;  

  151.   server_name  <a href="http://www.elain.org/">www.elain.org</a>;     #多域名用空格隔開  

  152.   index index.php index.html index.shtml;  

  153.   root  /elain/data/htdocs/elain;  

  154.   #limit_conn connlimit 20;     #限制一個IP只能最多隻能發起20個連接,超過報 503 Service unavailable,可防止惡意連接  

  155.   access_log /elain/logs/nginx/access_www.elain.org.log access;  

  156.   error_log  /elain/logs/nginx/error_www.elain.org.log;  

  157.   location / {  

  158.   ssi on;                 #WEB文檔根目錄打開SSI支持  

  159.   ssi_types text/html;  

  160.   ssi_silent_errors off;  #處理SSI出錯時不提示  

  161.   }  

  162.   location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ {  

  163.   access_log    off;  

  164.   expires       30d;  

  165.   }  

  166.   location ~ .*.(js|css)?$ {  

  167.   expires      1h;  

  168.   add_header Cache_Control private;  

  169.   }  

  170.   location ~ /.ht {  

  171.   deny all;  

  172.   }  

  173.   location /NginxStatus {           #設定查看Nginx狀態的地址  

  174.   stub_status on;  

  175.   access_log off;  

  176.   auth_basic “NginxStatus”;     #標識  

  177.   auth_basic_user_file conf/.htpasswd;   #網頁加密,提示登錄框,輸入用戶名和密碼可查看  

  178.   }  

  179.   location ~ .*.(php|php5)?$ {                           #匹配文件後綴php, php5  

  180.   #fastcgi_pass  unix:/tmp/php-cgi.sock;  #SOCKET方式轉交fastcgi處理  

  181.   fastcgi_pass  127.0.0.1:9000;           #9000端口方式fastcgi  

  182.   fastcgi_index index.php;  

  183.   include fastcgi_params;                 #包含fastcgi配置  

  184.   #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;  

  185.   }  

  186.   }  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章