1. 程式人生 > >Nginx配置參數優化註解

Nginx配置參數優化註解

nginx

### For more information on configuration, see:
###   * Official English Documentation: 
### auther:luo.m
##nginx運行用戶
user nginx;
##pid路徑
pid /run/nginx.pid;
##nginx工作的線程數,auto根據本機cpu顆粒數自動適配.
worker_processes auto;
##一個nginx進程打開的最多文件描述符數目,與ulimit -n(系統每個進程可打開的文件數)的值保持一致.
worker_rlimit_nofile 65535;
##events模塊中包含nginx中所有處理連接的設置.
events {
##一個worker進程同時打開的最大連接數,由系統的可用socket連接數限制.
worker_connections 51200;
##當nginx接到新連接的請求時,會盡可能的接受更多的連接
multi_accept on;
##Linux2.6+,性能最好的多路IO復用客戶端線程的輪詢方法.
use epoll;
} 
##HTTP模塊控制著nginx之http處理的所有核心特性.
http {
##加載MIME類型
    include /etc/nginx/mime.types;
##設置文件使用的默認的MIME-type
    default_type text/html;
##設置頭文件中的默認的字符集
    charset UTF-8;
##關閉錯誤頁面nginx版本號顯示,於安全有益.
    server_tokens off;
##sendfile減少拷貝文件過程,實現高效數據傳輸
    sendfile on;
##當使用sendfile函數時,tcp_nopush才起作用,一個數據包裏發送所有頭文件,而不一個接一個的發送
##它是Nagle算法的進一步增強,即阻塞數據包發送.
##Tcp_nopush選項會讓nginx嘗試在一個packet內發送其HTTP響應,而不是分幀傳送##對優化吞吐率很有用處
    tcp_nopush on;
##禁用Nagle算法,益於大量數據的通信性能.
    tcp_nodelay on;
##關閉日誌,減少IO
    #	access_log off;
    #	log_format main ‘[$time_local] ‘ ‘$request_uri ‘ ‘$status $upstream_addr $upstream_status ‘ ‘$upstream_response_time $request_time‘;
log_format cache ‘[$time_local] ‘ ‘$request_uri ‘ ‘$status $upstream_addr $upstream_status ‘ ‘$upstream_response_time $request_time‘;
    #	access_log /var/log/nginx/access.log main;
##設置錯誤日誌路徑和文件名
    error_log /var/log/nginx/error.log;
##當被代理的後端服務器的響應狀態碼大於等於300時,將響應轉發給nginx由error_page指令來處理.
    proxy_intercept_errors on;
##自定義錯誤頁面	
    error_page 404 http://static.yunjiweidian.com/error/404.html;
    error_page 500 502 503 504 http://static.yunjiweidian.com/error/soHot.html;
##keep-alive連接的超時時間,server會在此時間之後關閉連接
    keepalive_timeout 13;
##client的請求可以轉換成keep-alive連接的數量,影響性能測試,默認100
#	keepalive_requests 65535;
##允許server在client停止響應以後關閉連接,釋放分配給該連接的內存
    reset_timedout_connection on;
##如果client停止讀取數據,在此時間以後釋放該連接,默認是60秒
    send_timeout 6;
##設置用於保存各種key(比如當前連接數)的共享內存的參數
    limit_conn_zone $binary_remote_addr zone=addr:24m ;
##為給定的key設置最大連接數,即一個IP地址最多同時打開連接數
    limit_conn addr 51200;
##采用gzip壓縮的形式發送數據
    gzip on;
##為指定的客戶端禁用gzip功能
    gzip_disable "msie6";
##壓縮所有的請求
	gzip_proxied any;
##設置對數據啟用壓縮的最少字節數,少於1000字節不壓縮
	gzip_min_length 1000;
##壓縮級別
	gzip_comp_level 6;
##設置需要壓縮的數據格式
	gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript image/jpeg image/gif image/png image/jpg application/x-javascript image/svg+xml application/x-font-ttf application/vnd.ms-fontobject;
##緩存最大數目,以及緩存的時間	
	open_file_cache max=100000 inactive=20s;
##更新間隔時間	
	open_file_cache_valid 30s;
##inactive參數時間內文件的最少使用次數,如果超過這個數字,緩存一直存在
	open_file_cache_min_uses 2;
##搜索文件時是否緩存錯誤信息,再次給配置中添加文件
	open_file_cache_errors on;#	
	proxy_next_upstream 	off;
##確定在何種情況下請求將轉發到下一個服務器。轉發請求只發生在沒有數據傳遞到客戶端的過程中##[error|timeout|invalid_header|http_500|http_502|http_503|http_504|http_404|off]##表示只負載一臺機器,如果超時則返回,不去輪詢其他機器	
	proxy_next_upstream_tries 1;
##nginx上傳文件大小限制,默認值1M
	client_max_body_size 	10m;	
	client_body_buffer_size	128k;
##如果client對於body的請求超過這個時間,則發送"request timed out"響應,默認60秒##防範慢查詢攻擊
	client_body_timeout 	10;	
	client_header_timeout 	10;
#nginx跟後端服務器連接超時時間(代理連接超時)
	proxy_connect_timeout 	13;	
	proxy_send_timeout 	21;
#後端服務器數據回傳時間(代理發送超時)
	proxy_read_timeout 	34; 
#連接成功後,後端服務器響應時間(代理接收超時)
	proxy_buffer_size 	64k;
#設置代理服務器(nginx)保存用戶頭信息的緩沖區大小
	proxy_buffers 		4 64k;
#proxy_buffers緩沖區,網頁平均在32k以下
	proxy_busy_buffers_size 128k;
#高負荷下緩沖大小(proxy_buffers*2)
	proxy_temp_file_write_size 256k;
#設定緩存文件夾大小,大於這個值,將從upstream服務器傳
#當文件超過該參數設置的大小時,nginx會先將文件寫入臨時目錄(缺省為nginx安裝目下/proxy_temp目錄),註意權限問題
        server_names_hash_bucket_size	128;	
        client_header_buffer_size 64k;	
        large_client_header_buffers 4 128k;
##cache聲明
        proxy_temp_path /etc/nginx/tmp;	
        proxy_cache_path /etc/nginx/neicun levels=1:2 keys_zone=my_cache:512m inactive=30 max_size=4g;
    include /etc/nginx/conf.d/*.conf;
}


本文出自 “運維” 博客,請務必保留此出處http://dropak.blog.51cto.com/12206256/1976320

Nginx配置參數優化註解