本文爭對nginx-1.22.0
nginx的核心配置文件是nginx.conf.
1、基本配置
(1)、worker_processes 定義工作進程的數量
最佳值取決於許多因素,包括(但不限於)CPU內核的數量、存儲數據的硬盤驅動器的數量以及負載模式。當有人懷疑時,將其設置爲可用CPU內核的數量將是一個好的開始(值“auto”將嘗試自動檢測它)。
(2)、error_log 配置日誌記錄
可以在同一配置級別【debug、info、notice、warn、error、crit、alert或emerg。】上指定多個日誌。如果在主配置級別上未明確定義將日誌寫入文件,則將使用默認文件。
第一個參數定義將存儲日誌的文件。特殊值stderr選擇標準錯誤文件。可以通過指定“syslog:”前綴來配置對syslog的日誌記錄。記錄到循環內存緩衝區可以通過指定“memory:”前綴和緩衝區大小進行配置,通常用於調試(1.7.11)。
第二個參數決定日誌記錄的級別,可以是以下參數之一:debug、info、notice、warn、error、crit、alert或emerg。以上日誌級別按嚴重性增加的順序列出。設置某個日誌級別將導致記錄指定和更嚴重日誌級別的所有消息。例如,默認級別錯誤將導致記錄錯誤、crit、alert和emerg消息。注:默認級別error
(3)、pid 進程id配置
2、events
提供配置文件上下文,其中指定了影響連接處理的指令。
2.1 、worker_connections 設置work進程(worker_processes)可以同時打開的最大連接數。 正式環境可以放大(65535)
這個數字包括所有連接(例如與代理服務器的連接等),而不僅僅是與客戶端的連接。另一個考慮因素是,同時連接的實際數量不能超過當前打開文件的最大數量限制,該限制可以由worker_rlimit_nofile更改。
總連接數=worker_processes * worker_connections
2.2 multi_accept
如果禁用multi_accept,工作進程將一次接受一個新連接。否則,工作進程將一次接受所有新連接。
如果使用kqueue連接處理方法,則忽略該指令,因爲它會報告等待接受的新連接數。
2.3 use epoll
設置用於複用客戶端線程的輪詢方法。具體參考epoll原理
更改工作進程的最大打開文件數(RLIMIT_NOFILE)限制。用於在不重新啓動主進程的情況下增加限制。
4、http配置
提供指定HTTP服務器指令的配置文件上下文。
4.1 include mime.types;
配置支持的多媒體類型,在conf/mime.types文件下查看支持的類型
4.2 default_type
定義響應的默認MIME類型。可以使用types指令設置文件擴展名到MIME類型的映射。 默認是application/octet-stream; 字節流類型,能兼容絕大多數的資源文件.
4.3 log_format
定義日誌格式,相關內容查看文檔
4.4 access_log 訪問日誌
簡單使用方式 access_log 日誌路徑 使用的日誌模板(log_format);
4.5 sendfile 高效文件傳輸模式
在此配置中,使用SF_NODISKIO標誌調用sendfile(),這將導致它不會在磁盤I/O上阻塞,而是報告數據不在內存中。然後,nginx通過讀取一個字節來啓動異步數據加載。在第一次讀取時,FreeBSD內核將文件的前128K字節加載到內存中,儘管下一次讀取將只加載16K塊中的數據。這可以使用read_ahead指令進行更改。
限制單個sendfile()調用中可以傳輸的數據量。如果沒有限制,一個快速連接可能會完全佔用工作進程。 默認2m.
4.7 tcp_nopush
sendfile可以開啓高效的文件傳輸模式,tcp_nopush開啓可以確保在發送到客戶端之前數據包已經充分“填滿”, 這大大減少了網絡開銷,並加快了文件發送的速度
4.8 tcp_nodelay 該指令必須在keep-alive連接開啓的情況下才生效,來提高網絡包傳輸的'實時性'
啓用或禁用TCP_NODELAY選項的使用。當連接轉換爲保持活動狀態時,將啓用該選項。此外,它在SSL連接、無緩衝代理和WebSocket代理上啓用。
當tcp_nopush到達最後一個可能因爲沒有“填滿”而暫停的數據包時,Nginx會忽略tcp_nopush參數, 然後,tcp_nodelay強制套接字發送數據。由此可知,TCP_NOPUSH可以與TCP_NODELAY一起設置,它比單獨配置TCP_NODELAY具有更強的性能。所以我們可以使用如下配置來優化Nginx靜態資源的處理
注意:4.5~4.8全部合理的開啓和設置,能有效的解決靜態資源訪問處理的問題,提升效率.
4.9 keepalive_timeout 長連接超時時間設置
第一個參數設置了一個超時,在此期間,keep-alive客戶端連接將在服務器端保持打開狀態。零值將禁用保持活動狀態的客戶端連接。可選的第二個參數在“Keep Alive:timeout=time”響應頭字段中設置一個值。兩個參數可能不同。
Mozilla和Konqueror可以識別“Keep Alive:timeout=time”標題字段。MSIE會在大約60秒內關閉保持活動連接。
5、server配置
5.1 listen 配置監聽端口
5.2 server_name 服務名稱
5.3 charset 字符集配置
5.4 access_log 和4.4 access_log類似 如果在server中給了會覆蓋4.4中的
5.5 location
語法 location [ = | ~ | ~* | ^~ |@ ] uri{...}
位置 server,location
uri變量是待匹配的請求字符串,可以不包含正則表達式,也可以包含正則表達式,那麼nginx服務器在搜索匹配location的時候,是先使用不包含正則表達式進行匹配,找到一個匹配度最高的一個,然後在通過包含正則表達式的進行匹配,如果能匹配到直接訪問,匹配不到,就使用剛纔匹配度最高的那個location來處理請求。'
5.5.1 示例一 配置請求訪問nginx安裝目錄下的靜態資源
location / {
root html;
index index.html index.htm;
}
匹配訪問ip地址加監聽端口加/的請求,請求會重定向到nginx root(根目錄)的html文件夾,訪問index.html或者index.htm頁面.
5.5.2 示例二 精準匹配
location = /50x.html {
root html;
}
訪問ip地址加監聽端口加/50x.html並轉發
5.5.3示例三 配置禁止訪問的文件
#location ~ /\.ht {
# deny all;
#}
5.5.3示例四 配置https訪問
# HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #}
完整配置文件內容如下:
#user nobody; worker_processes 1; error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; worker_rlimit_nofile 65535; events { use epoll; worker_connections 65535; } 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"'; #access_log logs/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; #keepalive_timeout 0; keepalive_timeout 65; gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }