Nginx 設定檔詳解

來源:互聯網
上載者:User

標籤:

Nginx 設定檔詳解

 

user nginx ;

#使用者

 

worker_processes 8;

#背景工作處理序,根據硬體調整,大於等於cpu核心數

 

error_log logs/nginx_error.log crit;

#錯誤記錄檔

 

pid logs/nginx.pid;

#pid放置的位置

 

worker_rlimit_nofile 204800;

#指定進程可以開啟的最大描述符

這個指令是指當一個nginx進程開啟的最多檔案描述符數目,理論值應該是最多開啟文

件數(ulimit -n)與nginx進程數相除,但是nginx分配請求並不是那麼均勻,所以最好與ulimit -n 的值保持一致。

現在在linux 2.6核心下開啟檔案開啟數為65535,worker_rlimit_nofile就相應應該填寫65535。

這是因為nginx調度時分配請求到進程並不是那麼的均衡,所以假如填寫10240,總並發量達到3-4萬時就有進程可能超過10240了,這時會返回502錯誤。

 

events

 

{

use epoll;

#使用epoll的I/O 模型

補充說明:

與apache相類,nginx針對不同的作業系統,有不同的事件模型

A)標準事件模型

Select、poll屬於標準事件模型,如果當前系統不存在更有效方法,nginx會選擇select或poll

B)高效事件模型

Kqueue:使用於FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 和 MacOS X.使用雙處理器的MacOS X系統使用kqueue可能會造成核心崩潰。

Epoll:使用於Linux核心2.6版本及以後的系統。

/dev/poll:使用於Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+。

Eventport:使用於Solaris 10. 為了防止出現核心崩潰的問題, 有必要安裝安全補丁

 

 

worker_connections 204800;

#背景工作處理序的最大串連數量,根據硬體調整,和前面背景工作處理序配合起來用,盡量大,但是別把cpu跑到100%就行

每個進程允許的最多串連數, 理論上每台nginx伺服器的最大串連數為worker_processes*worker_connections

 

keepalive_timeout 60;

 

keepalive逾時時間。

 

client_header_buffer_size 4k;

 

用戶端要求標頭部的緩衝區大小,這個可以根據你的系統分頁大小來設定,一般一個要求標頭的大小不會超過1k,不過由於一般系統分頁都要大於1k,所以這裡設定為分頁大小。

分頁大小可以用命令getconf PAGESIZE 取得。

[[email protected] ~]# getconf PAGESIZE

4096

但也有client_header_buffer_size超過4k的情況,但是client_header_buffer_size該值必須設定為“系統分頁大小”的整倍數。

 

open_file_cache max=65535 inactive=60s;

 

這個將為開啟檔案指定緩衝,預設是沒有啟用的,max指定緩衝數量,建議和開啟檔案數一致,inactive是指經過多長時間檔案沒被請求後刪除緩衝。

 

open_file_cache_valid 80s;

 

這個是指多長時間檢查一次緩衝的有效資訊。

 

open_file_cache_min_uses 1;

 

open_file_cache指令中的inactive參數時間內檔案的最少使用次數,如果超過這個數字,檔案描述符一直是在緩衝中開啟的,如上例,如果有一個檔案在inactive時間內一次沒被使用,它將被移除。

 

 

}

 

#設定http伺服器,利用它的反向 Proxy功能提供負載平衡支援

http

{

include mime.types;

#設定mime類型,類型由mime.type檔案定義

default_type application/octet-stream;

log_format main ‘$host $status [$time_local] $remote_addr [$time_local] $request_uri ‘

‘"$http_referer" "$http_user_agent" "$http_x_forwarded_for" ‘

‘$bytes_sent $request_time $sent_http_x_cache_hit‘;

log_format log404 ‘$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_s ent :記錄發送給用戶端檔案主體內容大小;

$http_referer:用來記錄從那個頁面連結訪問過來的;

$http_user_agent:記錄客戶毒啊瀏覽器的相關資訊;

通常web伺服器放在反向 Proxy的後面,這樣就不能擷取到客戶的IP地址了,通過$remote_add拿到的IP地址是反向 Proxy伺服器的iP地址。反向 Proxy伺服器在轉寄請求的http頭資訊中,可以增加x_forwarded_for資訊,用以記錄原有用戶端的IP地址和原來用戶端的請求的伺服器位址;

access_log /dev/null;

#用了log_format指令設定了日誌格式之後,需要用access_log指令指定記錄檔的存放路徑;

# access_log /usr/local/nginx/logs/access_log main;

server_names_hash_bucket_size 128;

#儲存伺服器名字的hash表是由指令server_names_hash_max_size 和server_names_hash_bucket_size所控制的。參數hash bucket size總是等於hash表的大小,並且是一路處理器緩衝大小的倍數。在減少了在記憶體中的存取次數後,使在處理器中加速尋找hash表索引值成為可能。如果hash bucket size等於一路處理器緩衝的大小,那麼在尋找鍵的時候,最壞的情況下在記憶體中尋找的次數為2。第一次是確定儲存單元的地址,第二次是在儲存單元中尋找鍵 值。因此,如果Nginx給出需要增大hash max size 或 hash bucket size的提示,那麼首要的是增大前一個參數的大小.

 

client_header_buffer_size 4k;

用戶端要求標頭部的緩衝區大小,這個可以根據你的系統分頁大小來設定,一般一個請求的頭部大小不會超過1k,不過由於一般系統分頁都要大於1k,所以這裡設定為分頁大小。分頁大小可以用命令getconf PAGESIZE取得。

 

large_client_header_buffers 8 128k;

客戶要求標頭緩衝大小
nginx預設會用client_header_buffer_size這個buffer來讀取header值,如果

header過大,它會使用large_client_header_buffers來讀取
如果設定過小HTTP頭/Cookie過大 會報400 錯誤nginx 400 bad request
求行如果超過buffer,就會報HTTP 414錯誤(URI Too Long)
nginx接受最長的HTTP頭部大小必須比其中一個buffer大,否則就會報400的

HTTP錯誤(Bad Request)。

open_file_cache max 102400

使用欄位:http, server, location 這個指令指定緩衝是否啟用,如果啟用,將記錄檔案以下資訊: ·開啟的檔案描述符,大小資訊和修改時間. ·存在的目錄資訊. ·在搜尋檔案過程中的錯誤資訊 --沒有這個檔案,無法正確讀取,參考open_file_cache_errors指令選項:
·max -指定緩衝的最大數目,如果緩衝溢出,最長使用過的檔案(LRU)將被移除
例: open_file_cache max=1000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on;

open_file_cache_errors
文法:open_file_cache_errors on | off 預設值:open_file_cache_errors off 使用欄位:http, server, location 這個指令指定是否在搜尋一個檔案是記錄cache錯誤.

open_file_cache_min_uses

文法:open_file_cache_min_uses number 預設值:open_file_cache_min_uses 1 使用欄位:http, server, location這個指令指定了在open_file_cache指令無效的參數中一定的時間範圍內可以使用的最小檔案數,如 果使用更大的值,檔案描述符在cache中總是開啟狀態.
open_file_cache_valid

文法:open_file_cache_valid time 預設值:open_file_cache_valid 60 使用欄位:http, server, location 這個指令指定了何時需要檢查open_file_cache中快取項目的有效資訊.

 

client_max_body_size 300m;

設定通過nginx上傳檔案的大小

 

sendfile on;

#sendfile指令指定 nginx 是否調用sendfile 函數(zero copy 方式)來輸出檔案,
對於普通應用,必須設為on。
如果用來進行下載等應用磁碟IO重負載應用,可設定為off,以平衡磁碟與網路IO處理速度,降低系統uptime。

tcp_nopush on;

此選項允許或禁止使用socke的TCP_CORK的選項,此選項僅在使用sendfile的時候使用

 

proxy_connect_timeout 90; 
#後端伺服器串連的逾時時間_發起握手等候響應逾時時間

proxy_read_timeout 180;

#串連成功後_等候後端伺服器回應時間_其實已經進入後端的排隊之中等候處理(也可以說是後端伺服器處理請求的時間)

proxy_send_timeout 180;

#後端伺服器資料回傳時間_就是在規定時間之內後端伺服器必須傳完所有的資料

proxy_buffer_size 256k;

#設定從被Proxy 伺服器讀取的第一部分應答的緩衝區大小,通常情況下這部分應答中包含一個小的應答頭,預設情況下這個值的大小為指令proxy_buffers中指定的一個緩衝區的大小,不過可以將其設定為更小

proxy_buffers 4 256k;

#設定用於讀取應答(來自被Proxy 伺服器)的緩衝區數目和大小,預設情況也為分頁大小,根據作業系統的不同可能是4k或者8k

proxy_busy_buffers_size 256k;

 

proxy_temp_file_write_size 256k;

#設定在寫入proxy_temp_path時資料的大小,預防一個背景工作處理序在傳遞檔案時阻塞太長

proxy_temp_path /data0/proxy_temp_dir;

#proxy_temp_path和proxy_cache_path指定的路徑必須在同一分區
proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
#設定記憶體緩衝空間大小為200MB,1天沒有被訪問的內容自動清除,硬碟緩衝空間大小為30GB。

keepalive_timeout 120;

keepalive逾時時間。

tcp_nodelay on;

client_body_buffer_size 512k;
如果把它設定為比較大的數值,例如256k,那麼,無論使用firefox還是IE瀏覽器,來提交任意小於256k的圖片,都很正常。如果注釋該指令,使用預設的client_body_buffer_size設定,也就是作業系統頁面大小的兩倍,8k或者16k,問題就出現了。
無論使用firefox4.0還是IE8.0,提交一個比較大,200k左右的圖片,都返回500 Internal Server Error錯誤

proxy_intercept_errors on;

表示使nginx阻止HTTP應答代碼為400或者更高的應答。

 

upstream img_relay {

server 127.0.0.1:8027;

server 127.0.0.1:8028;

server 127.0.0.1:8029;

hash $request_uri;

}

nginx的upstream目前支援4種方式的分配

1、輪詢(預設)

每個請求按時間順序逐一分配到不同的後端伺服器,如果後端伺服器down掉,能自動剔除。

2、weight
指定輪詢幾率,weight和訪問比率成正比,用於後端伺服器效能不均的情況。
例如:
upstream bakend {
server 192.168.0.14 weight=10;
server 192.168.0.15 weight=10;
}

2、ip_hash
每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個後端伺服器,可以解決session的問題。
例如:
upstream bakend {
ip_hash;
server 192.168.0.14:88;
server 192.168.0.15:80;
}

3、fair(第三方)
按後端伺服器的回應時間來分配請求,回應時間短的優先分配。
upstream backend {
server server1;
server server2;
fair;
}

4、url_hash(第三方)

按訪問url的hash結果來分配請求,使每個url定向到同一個後端伺服器,後端伺服器為緩衝時比較有效。

例:在upstream中加入hash語句,server語句中不能寫入weight等其他的參數,hash_method是使用的hash演算法

upstream backend {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}

tips:

upstream bakend{#定義負載平衡裝置的Ip及裝置狀態
ip_hash;
server 127.0.0.1:9090 down;
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:6060;
server 127.0.0.1:7070 backup;
}
在需要使用負載平衡的server中增加
proxy_pass http://bakend/;

每個裝置的狀態設定為:
1.down表示單前的server暫時不參與負載
2.weight預設為1.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 可以講client post過來的資料記錄到檔案中用來做debug
client_body_temp_path設定記錄檔案的目錄 可以設定最多3層目錄

location對URL進行匹配.可以進行重新導向或者進行新的代理 負載平衡

 

server

#配置虛擬機器

{

listen 80;

#配置監聽連接埠

server_name image.***.com;

#配置訪問網域名稱

location ~* \.(mp3|exe)$ {

#對以“mp3或exe”結尾的地址進行負載平衡

proxy_pass http://img_relay$request_uri;

#設定被Proxy 伺服器的連接埠或通訊端,以及URL

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

#以上三行,目的是將Proxy 伺服器收到的使用者的資訊傳到真實伺服器上

}

location /face {

if ($http_user_agent ~* "xnp") {

rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;

}

proxy_pass http://img_relay$request_uri;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

error_page 404 502 = @fetch;

}

location @fetch {

access_log /data/logs/face.log log404;

#設定本伺服器的訪問日誌

rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;

}

 

location /image {

if ($http_user_agent ~* "xnp") {

rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;

}

proxy_pass http://img_relay$request_uri;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

error_page 404 502 = @fetch;

}

location @fetch {

access_log /data/logs/image.log log404;

rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;

}

}

 

server

{

listen 80;

server_name *.***.com *.***.cn;

 

location ~* \.(mp3|exe)$ {

proxy_pass http://img_relay$request_uri;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

location / {

if ($http_user_agent ~* "xnp") {

rewrite ^(.*)$ http://i1.***img.com/help/noimg.gif redirect;

}

proxy_pass http://img_relay$request_uri;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

#error_page 404 http://i1.***img.com/help/noimg.gif;

error_page 404 502 = @fetch;

}

location @fetch {

access_log /data/logs/baijiaqi.log log404;

rewrite ^(.*)$ http://i1.***img.com/help/noimg.gif redirect;

}

#access_log off;

}

 

server

{

listen 80;

server_name *.***img.com;

 

location ~* \.(mp3|exe)$ {

proxy_pass http://img_relay$request_uri;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

 

location / {

if ($http_user_agent ~* "xnp") {

rewrite ^(.*)$ http://i1.***img.com/help/noimg.gif;

}

proxy_pass http://img_relay$request_uri;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

#error_page 404 http://i1.***img.com/help/noimg.gif;

error_page 404 = @fetch;

}

#access_log off;

location @fetch {

access_log /data/logs/baijiaqi.log log404;

rewrite ^(.*)$ http://i1.***img.com/help/noimg.gif redirect;

}

}

 

server

{

listen 8080;

server_name ngx-ha.***img.com;

location / {

stub_status on;

access_log off;

}

}

server {

listen 80;

server_name imgsrc1.***.net;

root html;

}

server {

listen 80;

server_name ***.com w.***.com;

# access_log /usr/local/nginx/logs/access_log main;

location / {

rewrite ^(.*)$ http://www.***.com/ ;

}

}

server {

listen 80;

server_name *******.com w.*******.com;

# access_log /usr/local/nginx/logs/access_log main;

location / {

rewrite ^(.*)$ http://www.*******.com/;

}

}

server {

listen 80;

server_name ******.com;

# access_log /usr/local/nginx/logs/access_log main;

location / {

rewrite ^(.*)$ http://www.******.com/;

}

}

location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd;
}

#設定查看Nginx狀態的地址

 

location ~ /\.ht {
deny all;
}

#禁止訪問.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和URI request組合而成

$request_body_file

$request_uri含有參數的完整的初始URI

$query_string與$args一樣

$sheeme http模式(http,https)盡在要求是評估例如

Rewrite ^(.+)$ $sheme://example.com$; Redirect;

$server_protocol等同於request的協議,使用“HTTP/或“HTTP/

$server_addr request到達的server的ip,一般獲得此變數的值的目的是進行系統調用。為了避免系統調用,有必要在listen指令中指明ip,並使用bind參數。

$server_name請求到達的伺服器名

$server_port請求到達的伺服器的連接埠號碼

$uri等同於當前request中的URI,可不同於初始值,例如內部重新導向時或使用index

 

 

nginx中文維基百科 http://wiki.nginx.org/NginxChs

http://www.queryer.cn/DOC/nginxCHS/index.html

Nginx 設定檔詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.