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 取得。

[root@web001 ~]# 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服務器,利用它的反向代理功能提供負載均衡支持

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服務器放在反向代理的後面,這樣就不能獲取到客戶的IP地址了,通過$remote_add拿到的IP地址是反向代理服務器的iP地址。反向代理服務器在轉發請求的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_buffers中指定的一個緩衝區的大小,不過可以將其設置爲更小

proxy_buffers 4 256k;

#設置用於讀取應答(來自被代理服務器)的緩衝區數目和大小,默認情況也爲分頁大小,根據操作系統的不同可能是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;

#設置被代理服務器的端口或套接字,以及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;

#以上三行,目的是將代理服務器收到的用戶的信息傳到真實服務器上

}

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

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