在日常的運維工作中,經常會用到nginx服務,也時常會碰到nginx因高并發導緻的性能瓶頸問題。今天這裡簡單梳理下nginx性能優化的配置(僅僅依據本人的實戰經驗而述,如有不妥,敬請指出~)
一、這裡的優化主要是指對nginx的配置優化,一般來說nginx配置檔案中對優化比較有作用的主要有以下幾項:
1)nginx程序數,建議按照cpu數目來指定,一般跟cpu核數相同或為它的倍數。
worker_processes 8;
2)為每個程序配置設定cpu,上例中将8個程序配置設定到8個cpu,當然可以寫多個,或者将一個程序配置設定到多個cpu。
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
3)下面這個指令是指當一個nginx程序打開的最多檔案描述符數目,理論值應該是系統的最多打開檔案數(ulimit -n)與nginx程序數相除,但是nginx配置設定請求并不是那麼均勻,是以最好與ulimit -n的值保持一緻。
worker_rlimit_nofile 65535;
4)使用epoll的I/O模型,用這個模型來高效處理異步事件
use epoll;
5)每個程序允許的最多連接配接數,理論上每台nginx伺服器的最大連接配接數為worker_processes*worker_connections。
worker_connections 65535;
6)http連接配接逾時時間,預設是60s,功能是使用戶端到伺服器端的連接配接在設定的時間内持續有效,當出現對伺服器的後繼請求時,該功能避免了建立或者重建立立連接配接。切記這個參數也不能設定過大!否則會導緻許多無效的http連接配接占據着nginx的連接配接數,終nginx崩潰!
keepalive_timeout 60;
7)用戶端請求頭部的緩沖區大小,這個可以根據你的系統分頁大小來設定,一般一個請求的頭部大小不會超過1k,不過由于一般系統分頁都要大于1k,是以這裡設定為分頁大小。分頁大小可以用指令getconf PAGESIZE取得。
client_header_buffer_size 4k;
8)下面這個參數将為打開檔案指定緩存,預設是沒有啟用的,max指定緩存數量,建議和打開檔案數一緻,inactive是指經過多長時間檔案沒被請求後删除緩存。
open_file_cache max=102400 inactive=20s;
9)下面這個是指多長時間檢查一次緩存的有效資訊。
open_file_cache_valid 30s;
10)open_file_cache指令中的inactive參數時間内檔案的最少使用次數,如果超過這個數字,檔案描述符一直是在緩存中打開的,如上例,如果有一個檔案在inactive時間内一次沒被使用,它将被移除。
open_file_cache_min_uses 1;
----------------------------------------------------------------
下面是一個本人使用的簡單的nginx配置檔案:
<a target="_blank" href="http://www.cnblogs.com/kevingrace/p/6094007.html#">?</a>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<code>[root@dev-huanqiu ~]</code><code># cat /usr/local/nginx/conf/nginx.conf</code>
<code>user www www;</code>
<code>worker_processes 8;</code>
<code>worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000;</code>
<code>error_log </code><code>/www/log/nginx_error</code><code>.log crit;</code>
<code>pid </code><code>/usr/local/nginx/nginx</code><code>.pid;</code>
<code>worker_rlimit_nofile 65535;</code>
<code>events</code>
<code>{</code>
<code> </code><code>use epoll;</code>
<code> </code><code>worker_connections 65535;</code>
<code>}</code>
<code>http</code>
<code> </code><code>include mime.types;</code>
<code> </code><code>default_type application</code><code>/octet-stream</code><code>;</code>
<code> </code><code>charset utf-8;</code>
<code> </code><code>server_names_hash_bucket_size 128;</code>
<code> </code><code>client_header_buffer_size 2k;</code>
<code> </code><code>large_client_header_buffers 4 4k;</code>
<code> </code><code>client_max_body_size 8m;</code>
<code> </code><code>sendfile on;</code>
<code> </code><code>tcp_nopush on;</code>
<code> </code><code>keepalive_timeout 60;</code>
<code> </code><code>fastcgi_cache_path </code><code>/usr/local/nginx/fastcgi_cache</code> <code>levels=1:2</code>
<code> </code><code>keys_zone=TEST:10m</code>
<code> </code><code>inactive=5m;</code>
<code> </code><code>fastcgi_connect_timeout 300;</code>
<code> </code><code>fastcgi_send_timeout 300;</code>
<code> </code><code>fastcgi_read_timeout 300;</code>
<code> </code><code>fastcgi_buffer_size 16k;</code>
<code> </code><code>fastcgi_buffers 16 16k;</code>
<code> </code><code>fastcgi_busy_buffers_size 16k;</code>
<code> </code><code>fastcgi_temp_file_write_size 16k;</code>
<code> </code><code>fastcgi_cache TEST;</code>
<code> </code><code>fastcgi_cache_valid 200 302 1h;</code>
<code> </code><code>fastcgi_cache_valid 301 1d;</code>
<code> </code><code>fastcgi_cache_valid any 1m;</code>
<code> </code><code>fastcgi_cache_min_uses 1;</code>
<code> </code><code>fastcgi_cache_use_stale error timeout invalid_header http_500; </code>
<code> </code><code>open_file_cache max=204800 inactive=20s;</code>
<code> </code><code>open_file_cache_min_uses 1;</code>
<code> </code><code>open_file_cache_valid 30s; </code>
<code> </code><code>tcp_nodelay on;</code>
<code> </code>
<code> </code><code>gzip</code> <code>on;</code>
<code> </code><code>gzip_min_length 1k;</code>
<code> </code><code>gzip_buffers 4 16k;</code>
<code> </code><code>gzip_http_version 1.0;</code>
<code> </code><code>gzip_comp_level 2;</code>
<code> </code><code>gzip_types text</code><code>/plain</code> <code>application</code><code>/x-javascript</code> <code>text</code><code>/css</code> <code>application</code><code>/xml</code><code>;</code>
<code> </code><code>gzip_vary on;</code>
<code> </code><code>server</code>
<code> </code><code>{</code>
<code> </code><code>listen 8080;</code>
<code> </code><code>server_name huan.wangshibo.com;</code>
<code> </code><code>index index.php index.htm;</code>
<code> </code><code>root </code><code>/www/html/</code><code>;</code>
<code> </code><code>location </code><code>/status</code>
<code> </code><code>{</code>
<code> </code><code>stub_status on;</code>
<code> </code><code>}</code>
<code> </code><code>location ~ .*\.(php|php5)?$</code>
<code> </code><code>fastcgi_pass 127.0.0.1:9000;</code>
<code> </code><code>fastcgi_index index.php;</code>
<code> </code><code>include fcgi.conf;</code>
<code> </code><code>location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$</code>
<code> </code><code>expires 30d;</code>
<code> </code><code>log_format access </code><code>'$remote_addr - $remote_user [$time_local] "$request" '</code>
<code> </code><code>'$status $body_bytes_sent "$http_referer" '</code>
<code> </code><code>'"$http_user_agent" $http_x_forwarded_for'</code><code>;</code>
<code> </code><code>access_log </code><code>/www/log/access</code><code>.log access;</code>
<code> </code><code>}</code>
二、關于FastCGI的幾個指令
1)這個指令為FastCGI緩存指定一個路徑,目錄結構等級,關鍵字區域存儲時間和非活動删除時間。
fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10m inactive=5m;
2)指定連接配接到後端FastCGI的逾時時間。
fastcgi_connect_timeout 300;
3)向FastCGI傳送請求的逾時時間,這個值是指已經完成兩次握手後向FastCGI傳送請求的逾時時間。
fastcgi_send_timeout 300;
4)接收FastCGI應答的逾時時間,這個值是指已經完成兩次握手後接收FastCGI應答的逾時時間。
fastcgi_read_timeout 300;
5)指定讀取FastCGI應答第一部分 需要用多大的緩沖區,這裡可以設定為fastcgi_buffers指令指定的緩沖區大小,上面的指令指定它将使用1個 16k的緩沖區去讀取應答的第一部分,即應答頭,其實這個應答頭一般情況下都很小(不會超過1k),但是你如果在fastcgi_buffers指令中指 定了緩沖區的大小,那麼它也會配置設定一個fastcgi_buffers指定的緩沖區大小去緩存。
fastcgi_buffer_size 16k;
6)指定本地需要用多少和多大的緩沖區來 緩沖FastCGI的應答,如上所示,如果一個php腳本所産生的頁面大小為256k,則會為其配置設定16個16k的緩沖區來緩存,如果大于256k,增大 于256k的部分會緩存到fastcgi_temp指定的路徑中, 當然這對伺服器負載來說是不明智的方案,因為記憶體中處理資料速度要快于硬碟,通常這個值 的設定應該選擇一個你的站點中的php腳本所産生的頁面大小的中間值,比如你的站點大部分腳本所産生的頁面大小為 256k就可以把這個值設定為16 16k,或者4 64k 或者64 4k,但很顯然,後兩種并不是好的設定方法,因為如果産生的頁面隻有32k,如果用4 64k它會配置設定1個64k的緩沖區去緩存,而如果使用64 4k它會配置設定8個4k的緩沖區去緩存,而如果使用16 16k則它會配置設定2個16k去緩存頁面,這樣看起來似乎更加合理。
fastcgi_buffers 16 16k;
7)這個指令我也不知道是做什麼用,隻知道預設值是fastcgi_buffers的兩倍。
fastcgi_busy_buffers_size 32k;
8)在寫入fastcgi_temp_path時将用多大的資料塊,預設值是fastcgi_buffers的兩倍。
fastcgi_temp_file_write_size 32k;
9)開啟FastCGI緩存并且為其制定一個名稱。個人感覺開啟緩存非常有用,可以有效降低CPU負載,并且防止502錯誤。但是這個緩存會引起很多問題,因為它緩存的是動态頁面。具體使用還需根據自己的需求。
fastcgi_cache TEST
10)為指定的應答代碼指定緩存時間,如上例中将200,302應答緩存一小時,301應答緩存1天,其他為1分鐘。
fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m;
11)緩存在fastcgi_cache_path指令inactive參數值時間内的最少使用次數,如上例,如果在5分鐘内某檔案1次也沒有被使用,那麼這個檔案将被移除。
fastcgi_cache_min_uses 1;
12)不知道這個參數的作用,猜想應該是讓nginx知道哪些類型的緩存是沒用的。
fastcgi_cache_use_stale error timeout invalid_header http_500;
-----------------------------------
以上為nginx中FastCGI相關參數,
另外,FastCGI自身也有一些配置需要進行優化,如果你使用php-fpm來管理FastCGI,可以修改配置檔案中的以下值:
1)同時處理的并發請求數,即它将開啟最多60個子線程來處理并發連接配接。
<value name="max_children">60</value>
2)最多打開檔案數。
<value name="rlimit_files">65535</value>
3)每個程序在重置之前能夠執行的最多請求數。
<value name="max_requests">65535</value>
三、關于核心參數的優化,在/etc/sysctl.conf檔案内
1)timewait的數量,預設是180000。(Deven:是以如果想把timewait降下了就要把tcp_max_tw_buckets值減小)
net.ipv4.tcp_max_tw_buckets = 6000
2)允許系統打開的端口範圍。
net.ipv4.ip_local_port_range = 1024 65000
3)啟用TIME-WAIT狀态sockets快速回收功能;用于快速減少在TIME-WAIT狀态TCP連接配接數。1表示啟用;0表示關閉。這個選項一般不推薦啟用,因為在NAT(Network Address Translation)網絡下,會導緻大量的TCP連接配接建立錯誤,進而引起網站通路故障。
net.ipv4.tcp_tw_recycle = 0
----------------------------------------------------------------------------------------------------------------------------------
實際上,net.ipv4.tcp_tw_recycle功能的開啟,要需要net.ipv4.tcp_timestamps(一般系統預設是開啟這個功能的)這個開關開啟後才有效果;
當tcp_tw_recycle 開啟時(tcp_timestamps 同時開啟,快速回收 socket 的效果達到),對于位于NAT裝置後面的 Client來說,是一場災難!
會導緻到NAT裝置後面的Client連接配接Server不穩定(有的 Client 能連接配接 server,有的 Client 不能連接配接 server)。
也就是說,tcp_tw_recycle這個功能,是為内部網絡(網絡環境自己可控 ” ——不存在NAT 的情況)設計的,對于公網環境下,不宜使用。
通常來說,回收TIME_WAIT狀态的socket是因為“無法主動連接配接遠端”,因為無可用的端口,而不應該是要回收記憶體(沒有必要)。
即:需求是Client的需求,Server會有“端口不夠用”的問題嗎?
除非是前端機,需要大量的連接配接後端服務,也就是充當着Client的角色。
正确的解決這個總是辦法應該是:
net.ipv4.ip_local_port_range = 9000 6553 #預設值範圍較小
net.ipv4.tcp_max_tw_buckets = 10000 #預設值較小,還可适當調小
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 10
4)開啟重用功能,允許将TIME-WAIT狀态的sockets重新用于新的TCP連接配接。這個功能啟用是安全的,一般不要去改動!
net.ipv4.tcp_tw_reuse = 1
5)開啟SYN Cookies,當出現SYN等待隊列溢出時,啟用cookies來處理。
net.ipv4.tcp_syncookies = 1
6)web應用中listen函數的backlog預設會給我們核心參數的net.core.somaxconn限制到128,而nginx定義的NGX_LISTEN_BACKLOG預設為511,是以有必要調整這個值。
net.core.somaxconn = 262144
7)每個網絡接口接收資料包的速率比核心處理這些包的速率快時,允許送到隊列的資料包的最大數目。
net.core.netdev_max_backlog = 262144
8)系統中最多有多少個TCP套接字不被關聯到任何一個使用者檔案句柄上。如果超過這個數字,孤兒連接配接将即刻被複位并列印出警告資訊。這個限制僅僅是為了防止簡單的DoS攻擊,不能過分依靠它或者人為地減小這個值,更應該增加這個值(如果增加了記憶體之後)。
net.ipv4.tcp_max_orphans = 262144
9)記錄的那些尚未收到用戶端确認資訊的連接配接請求的最大值。對于有128M記憶體的系統而言,預設值是1024,小記憶體的系統則是128。
net.ipv4.tcp_max_syn_backlog = 262144
10)時間戳可以避免序列号的卷繞。一個1Gbps的鍊路肯定會遇到以前用過的序列号。時間戳能夠讓核心接受這種“異常”的資料包。
net.ipv4.tcp_timestamps = 1
-------------------------------------------------------------------------------------------------------------------------------------------------------
有不少伺服器為了提高性能,開啟net.ipv4.tcp_tw_recycle選項,在NAT網絡環境下,容易導緻網站通路出現了一些connect失敗的問題
個人建議:
關閉net.ipv4.tcp_tw_recycle選項,而不是net.ipv4.tcp_timestamps;
因為在net.ipv4.tcp_timestamps關閉的條件下,開啟net.ipv4.tcp_tw_recycle是不起作用的;而net.ipv4.tcp_timestamps可以獨立開啟并起作用。
11)為了打開對端的連接配接,核心需要發送一個SYN并附帶一個回應前面一個SYN的ACK。也就是所謂三次握手中的第二次握手。這個設定決定了核心放棄連接配接之前發送SYN+ACK包的數量。
net.ipv4.tcp_synack_retries = 1
12)在核心放棄建立連接配接之前發送SYN包的數量。
net.ipv4.tcp_syn_retries = 1
13)如果套接字由本端要求關閉,這個參數 決定了它保持在FIN-WAIT-2狀态的時間。對端可以出錯并永遠不關閉連接配接,甚至意外當機。預設值是60秒。2.2 核心的通常值是180秒,你可以按這個設定,但要記住的是,即使你的機器是一個輕載的WEB伺服器,也有因為大量的死套接字而記憶體溢出的風險,FIN- WAIT-2的危險性比FIN-WAIT-1要小,因為它最多隻能吃掉1.5K記憶體,但是它們的生存期長些。
net.ipv4.tcp_fin_timeout = 1
14)當keepalive起用的時候,TCP發送keepalive消息的頻度。預設是2小時。
net.ipv4.tcp_keepalive_time = 30
----------------------------------------------------------------------
下面貼出一個本人常用的核心參數的标準配置
[root@dev-huanqiu ~]# cat /etc/sysctl.conf
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 16384 4194304
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.ip_conntrack_max = 6553500
本文轉自 Mr_sheng 51CTO部落格,原文連結:http://blog.51cto.com/sf1314/2044894