Nginx的設定檔nginx.conf配置詳解,nginxnginx.conf
Nginx的設定檔nginx.conf配置詳解如下:
usernginxnginx;
Nginx使用者及組:使用者組。window下不指定
worker_processes8;
背景工作處理序:數目。根據硬體調整,通常等於CPU數量或者2倍於CPU。
error_loglogs/error.log;
error_loglogs/error.lognotice;
error_loglogs/error.loginfo;
錯誤記錄檔:存放路徑。
pidlogs/nginx.pid;
pid(進程標識符):存放路徑。
worker_rlimit_nofile204800;
指定進程可以開啟的最大描述符:數目。
這個指令是指當一個nginx進程開啟的最多檔案描述符數目,理論值應該是最多開啟檔案數(ulimit-n)與nginx進程數相除,但是nginx分配請求並不是那麼均勻,所以最好與ulimit-n的值保持一致。
現在在linux2.6核心下開啟檔案開啟數為65535,worker_rlimit_nofile就相應應該填寫65535。
這是因為nginx調度時分配請求到進程並不是那麼的均衡,所以假如填寫10240,總並發量達到3-4萬時就有進程可能超過10240了,這時會返回502錯誤。
events
{
useepoll;
使用epoll的I/O模型。linux建議epoll,FreeBSD建議採用kqueue,window下不指定。
補充說明:
與apache相類,nginx針對不同的作業系統,有不同的事件模型
A)標準事件模型
Select、poll屬於標準事件模型,如果當前系統不存在更有效方法,nginx會選擇select或poll
B)高效事件模型
Kqueue:使用於FreeBSD4.1+,OpenBSD2.9+,NetBSD2.0和MacOSX.使用雙處理器的MacOSX系統使用kqueue可能會造成核心崩潰。
Epoll:使用於Linux核心2.6版本及以後的系統。
/dev/poll:使用於Solaris711/99+,HP/UX11.22+(eventport),IRIX6.5.15+和Tru64UNIX5.1A+。
Eventport:使用於Solaris10。為了防止出現核心崩潰的問題,有必要安裝安全補丁。
worker_connections204800;
沒個背景工作處理序的最大串連數量。根據硬體調整,和前面背景工作處理序配合起來用,盡量大,但是別把cpu跑到100%就行。每個進程允許的最多串連數,理論上每台nginx伺服器的最大串連數為。worker_processes*worker_connections
keepalive_timeout60;
keepalive逾時時間。
client_header_buffer_size4k;
用戶端要求標頭部的緩衝區大小。這個可以根據你的系統分頁大小來設定,一般一個要求標頭的大小不會超過1k,不過由於一般系統分頁都要大於1k,所以這裡設定為分頁大小。
分頁大小可以用命令getconfPAGESIZE取得。
[root@web001~]#getconfPAGESIZE
4096
但也有client_header_buffer_size超過4k的情況,但是client_header_buffer_size該值必須設定為“系統分頁大小”的整倍數。
open_file_cachemax=65535inactive=60s;
這個將為開啟檔案指定緩衝,預設是沒有啟用的,max指定緩衝數量,建議和開啟檔案數一致,inactive是指經過多長時間檔案沒被請求後刪除緩衝。
open_file_cache_valid80s;
這個是指多長時間檢查一次緩衝的有效資訊。
open_file_cache_min_uses1;
open_file_cache指令中的inactive參數時間內檔案的最少使用次數,如果超過這個數字,檔案描述符一直是在緩衝中開啟的,如上例,如果有一個檔案在inactive時間內一次沒被使用,它將被移除。
}
##設定http伺服器,利用它的反向 Proxy功能提供負載平衡支援
http
{
includemime.types;
設定mime類型,類型由mime.type檔案定義
default_typeapplication/octet-stream;
log_formatmain '$remote_addr-$remote_user[$time_local]"$request"'
'$status$body_bytes_sent"$http_referer"'
'"$http_user_agent""$http_x_forwarded_for"';
log_formatlog404 '$status[$time_local]$remote_addr$host$request_uri$sent_http_location';
日誌格式設定。
$remote_addr與$http_x_forwarded_for用以記錄用戶端的ip地址;
$remote_user:用來記錄用戶端使用者名稱稱;
$time_local:用來記錄訪問時間與時區;
$request:用來記錄請求的url與http協議;
$status:用來記錄請求狀態;成功是200,
$body_bytes_sent:記錄發送給用戶端檔案主體內容大小;
$http_referer:用來記錄從那個頁面連結訪問過來的;
$http_user_agent:記錄客戶瀏覽器的相關資訊;
通常web伺服器放在反向 Proxy的後面,這樣就不能擷取到客戶的IP地址了,通過$remote_add拿到的IP地址是反向 Proxy伺服器的iP地址。反向 Proxy伺服器在轉寄請求的http頭資訊中,可以增加x_forwarded_for資訊,用以記錄原有用戶端的IP地址和原來用戶端的請求的伺服器位址。
access_loglogs/host.access.logmain;
access_loglogs/host.access.404.loglog404;
用了log_format指令設定了日誌格式之後,需要用access_log指令指定記錄檔的存放路徑;
server_names_hash_bucket_size128;
#儲存伺服器名字的hash表是由指令server_names_hash_max_size和server_names_hash_bucket_size所控制的。參數hashbucketsize總是等於hash表的大小,並且是一路處理器緩衝大小的倍數。在減少了在記憶體中的存取次數後,使在處理器中加速尋找hash表索引值成為可能。如果hashbucketsize等於一路處理器緩衝的大小,那麼在尋找鍵的時候,最壞的情況下在記憶體中尋找的次數為2。第一次是確定儲存單元的地址,第二次是在儲存單元中尋找索引值。因此,如果Nginx給出需要增大hashmaxsize或hashbucketsize的提示,那麼首要的是增大前一個參數的大小.
client_header_buffer_size4k;
用戶端要求標頭部的緩衝區大小。這個可以根據你的系統分頁大小來設定,一般一個請求的頭部大小不會超過1k,不過由於一般系統分頁都要大於1k,所以這裡設定為分頁大小。分頁大小可以用命令getconfPAGESIZE取得。
large_client_header_buffers8128k;
客戶要求標頭緩衝大小。nginx預設會用client_header_buffer_size這個buffer來讀取header值,如果
header過大,它會使用large_client_header_buffers來讀取。
open_file_cachemax=102400inactive=20s;
這個指令指定緩衝是否啟用。
例:open_file_cachemax=1000inactive=20s;
open_file_cache_valid30s;
open_file_cache_min_uses2;
open_file_cache_errorson;
open_file_cache_errors
文法:open_file_cache_errorson|off預設值:open_file_cache_errorsoff使用欄位:http,server,location這個指令指定是否在搜尋一個檔案是記錄cache錯誤.
open_file_cache_min_uses
文法:open_file_cache_min_usesnumber預設值:open_file_cache_min_uses1使用欄位:http,server,location這個指令指定了在open_file_cache指令無效的參數中一定的時間範圍內可以使用的最小檔案數,如果使用更大的值,檔案描述符在cache中總是開啟狀態.
open_file_cache_valid
文法:open_file_cache_validtime預設值:open_file_cache_valid60使用欄位:http,server,location這個指令指定了何時需要檢查open_file_cache中快取項目的有效資訊.
client_max_body_size300m;
設定通過nginx上傳檔案的大小
sendfileon;
sendfile指令指定nginx是否調用sendfile函數(zerocopy方式)來輸出檔案,對於普通應用,必須設為on。如果用來進行下載等應用磁碟IO重負載應用,可設定為off,以平衡磁碟與網路IO處理速度,降低系統uptime。
tcp_nopushon;
此選項允許或禁止使用socke的TCP_CORK的選項,此選項僅在使用sendfile的時候使用
proxy_connect_timeout90;
後端伺服器串連的逾時時間_發起握手等候響應逾時時間
proxy_read_timeout180;
串連成功後_等候後端伺服器回應時間_其實已經進入後端的排隊之中等候處理(也可以說是後端伺服器處理請求的時間)
proxy_send_timeout180;
後端伺服器資料回傳時間_就是在規定時間之內後端伺服器必須傳完所有的資料
proxy_buffer_size256k;
設定從被Proxy 伺服器讀取的第一部分應答的緩衝區大小,通常情況下這部分應答中包含一個小的應答頭,預設情況下這個值的大小為指令proxy_buffers中指定的一個緩衝區的大小,不過可以將其設定為更小
proxy_buffers4256k;
設定用於讀取應答(來自被Proxy 伺服器)的緩衝區數目和大小,預設情況也為分頁大小,根據作業系統的不同可能是4k或者8k
proxy_busy_buffers_size256k;
proxy_temp_file_write_size256k;
設定在寫入proxy_temp_path時資料的大小,預防一個背景工作處理序在傳遞檔案時阻塞太長
proxy_temp_path/data0/proxy_temp_dir;
proxy_temp_path和proxy_cache_path指定的路徑必須在同一分區
proxy_cache_path/data0/proxy_cache_dirlevels=1:2keys_zone=cache_one:200minactive=1dmax_size=30g;
#設定記憶體緩衝空間大小為200MB,1天沒有被訪問的內容自動清除,硬碟緩衝空間大小為30GB。
keepalive_timeout120;
keepalive逾時時間。
tcp_nodelayon;
client_body_buffer_size512k;
如果把它設定為比較大的數值,例如256k,那麼,無論使用firefox還是IE瀏覽器,來提交任意小於256k的圖片,都很正常。如果注釋該指令,使用預設的client_body_buffer_size設定,也就是作業系統頁面大小的兩倍,8k或者16k,問題就出現了。
無論使用firefox4.0還是IE8.0,提交一個比較大,200k左右的圖片,都返回500InternalServerError錯誤
proxy_intercept_errorson;
表示使nginx阻止HTTP應答代碼為400或者更高的應答。
upstreambakend{
server127.0.0.1:8027;
server127.0.0.1:8028;
server127.0.0.1:8029;
hash$request_uri;
}
nginx的upstream目前支援4種方式的分配
1、輪詢(預設)
每個請求按時間順序逐一分配到不同的後端伺服器,如果後端伺服器down掉,能自動剔除。
2、weight
指定輪詢幾率,weight和訪問比率成正比,用於後端伺服器效能不均的情況。
例如:
upstreambakend{
server192.168.0.14weight=10;
server192.168.0.15weight=10;
}
2、ip_hash
每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個後端伺服器,可以解決session的問題。
例如:
upstreambakend{
ip_hash;
server192.168.0.14:88;
server192.168.0.15:80;
}
3、fair(第三方)
按後端伺服器的回應時間來分配請求,回應時間短的優先分配。
upstreambackend{
serverserver1;
serverserver2;
fair;
}
4、url_hash(第三方)
按訪問url的hash結果來分配請求,使每個url定向到同一個後端伺服器,後端伺服器為緩衝時比較有效。
例:在upstream中加入hash語句,server語句中不能寫入weight等其他的參數,hash_method是使用的hash演算法
upstreambackend{
serversquid1:3128;
serversquid2:3128;
hash$request_uri;
hash_methodcrc32;
}
tips:
upstreambakend{#定義負載平衡裝置的Ip及裝置狀態}{
ip_hash;
server127.0.0.1:9090down;
server127.0.0.1:8080weight=2;
server127.0.0.1:6060;
server127.0.0.1:7070backup;
}
在需要使用負載平衡的server中增加
proxy_passhttp://bakend/;
每個裝置的狀態設定為:
1.down表示單前的server暫時不參與負載
2.weight為weight越大,負載的權重就越大。
3.max_fails:允許請求失敗的次數預設為1.當超過最大次數時,返回proxy_next_upstream模組定義的錯誤
4.fail_timeout:max_fails次失敗後,暫停時間。
5.backup:其它所有的非backup機器down或者忙的時候,請求backup機器。所以這台機器壓力會最輕。
nginx支援同時設定多組的負載平衡,用來給不用的server來使用。
client_body_in_file_only設定為On可以講clientpost過來的資料記錄到檔案中用來做debug
client_body_temp_path設定記錄檔案的目錄可以設定最多3層目錄
location對URL進行匹配.可以進行重新導向或者進行新的代理負載平衡
##配置虛擬機器
server
{
listen80;
配置監聽連接埠
server_nameimage.***.com;
配置訪問網域名稱
location~*\.(mp3|exe)${
對以“mp3或exe”結尾的地址進行負載平衡
proxy_passhttp://img_relay$request_uri;
設定被Proxy 伺服器的連接埠或通訊端,以及URL
proxy_set_headerHost$host;
proxy_set_headerX-Real-IP$remote_addr;
proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
以上三行,目的是將Proxy 伺服器收到的使用者的資訊傳到真實伺服器上
}
location/face{
if($http_user_agent~*"xnp"){
rewrite^(.*)$http://211.151.188.190:8080/face.jpgredirect;
}
proxy_passhttp://img_relay$request_uri;
proxy_set_headerHost$host;
proxy_set_headerX-Real-IP$remote_addr;
proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
error_page404502=@fetch;
}
location@fetch{
access_log/data/logs/face.loglog404;
rewrite^(.*)$http://211.151.188.190:8080/face.jpgredirect;
}
location/image{
if($http_user_agent~*"xnp"){
rewrite^(.*)$http://211.151.188.190:8080/face.jpgredirect;
}
proxy_passhttp://img_relay$request_uri;
proxy_set_headerHost$host;
proxy_set_headerX-Real-IP$remote_addr;
proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
error_page404502=@fetch;
}
location@fetch{
access_log/data/logs/image.loglog404;
rewrite^(.*)$http://211.151.188.190:8080/face.jpgredirect;
}
}
##其他舉例
server
{
listen80;
server_name*.***.com*.***.cn;
location~*\.(mp3|exe)${
proxy_passhttp://img_relay$request_uri;
proxy_set_headerHost$host;
proxy_set_headerX-Real-IP$remote_addr;
proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
}
location/{
if($http_user_agent~*"xnp"){
rewrite^(.*)$http://i1.***img.com/help/noimg.gifredirect;
}
proxy_passhttp://img_relay$request_uri;
proxy_set_headerHost$host;
proxy_set_headerX-Real-IP$remote_addr;
proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
#error_page404http://i1.***img.com/help/noimg.gif;
error_page404502=@fetch;
}
location@fetch{
access_log/data/logs/baijiaqi.loglog404;
rewrite^(.*)$http://i1.***img.com/help/noimg.gifredirect;
}
}
server
{
listen80;
server_name*.***img.com;
location~*\.(mp3|exe)${
proxy_passhttp://img_relay$request_uri;
proxy_set_headerHost$host;
proxy_set_headerX-Real-IP$remote_addr;
proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
}
location/{
if($http_user_agent~*"xnp"){
rewrite^(.*)$http://i1.***img.com/help/noimg.gif;
}
proxy_passhttp://img_relay$request_uri;
proxy_set_headerHost$host;
proxy_set_headerX-Real-IP$remote_addr;
proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
#error_page404http://i1.***img.com/help/noimg.gif;
error_page404=@fetch;
}
#access_logoff;
location@fetch{
access_log/data/logs/baijiaqi.loglog404;
rewrite^(.*)$http://i1.***img.com/help/noimg.gifredirect;
}
}
server
{
listen8080;
server_namengx-ha.***img.com;
location/{
stub_statuson;
access_logoff;
}
}
server{
listen80;
server_nameimgsrc1.***.net;
roothtml;
}
server{
listen80;
server_name***.comw.***.com;
#access_log/usr/local/nginx/logs/access_logmain;
location/{
rewrite^(.*)$http://www.***.com/;
}
}
server{
listen80;
server_name*******.comw.*******.com;
#access_log/usr/local/nginx/logs/access_logmain;
location/{
rewrite^(.*)$http://www.*******.com/;
}
}
server{
listen80;
server_name******.com;
#access_log/usr/local/nginx/logs/access_logmain;
location/{
rewrite^(.*)$http://www.******.com/;
}
}
location/NginxStatus{
stub_statuson;
access_logon;
auth_basic"NginxStatus";
auth_basic_user_fileconf/htpasswd;
}
#設定查看Nginx狀態的地址
location~/\.ht{
denyall;
}
#禁止訪問.htxxx檔案
}
注釋:變數
Ngx_http_core_module模組支援內建變數,他們的名字和apache的內建變數是一致的。
首先是說明客戶請求title中的行,例如$http_user_agent,$http_cookie等等。
此外還有其它的一些變數
$args此變數與請求行中的參數相等
$content_length等於請求行的“Content_Length”的值。
$content_type等同與要求標頭部的”Content_Type”的值
$document_root等同於當前請求的root指令指定的值
$document_uri與$uri一樣
$host與要求標頭部中“Host”行指定的值或是request到達的server的名字(沒有Host行)一樣
$limit_rate允許限制的串連速率
$request_method等同於request的method,通常是“GET”或“POST”
$remote_addr用戶端ip
$remote_port用戶端port
$remote_user等同於使用者名稱,由ngx_http_auth_basic_module認證
$request_filename當前請求的檔案的路徑名,由root或alias和URIrequest組合而成
$request_body_file
$request_uri含有參數的完整的初始URI
$query_string與$args一樣
$sheemehttp模式(http,https)盡在要求是評估例如
Rewrite^(.+)$$sheme://example.com$;Redirect;
$server_protocol等同於request的協議,使用“HTTP/或“HTTP/
$server_addrrequest到達的server的ip,一般獲得此變數的值的目的是進行系統調用。為了避免系統調用,有必要在listen指令中指明ip,並使用bind參數。
$server_name請求到達的伺服器名
$server_port請求到達的伺服器的連接埠號碼
$uri等同於當前request中的URI,可不同於初始值,例如內部重新導向時或使用index