Nginx性能優化配置(三)
本文主要介紹Nginx的性能優化配置,文章的層次架構如圖所示。
1.Nginx的工作進程優化
1.worker_processes
作用:worker進程的數量;通常應該等於小於當前主機的cpu的物理核心數;auto表示根據CPU的物理核心數自動調整工作進程數。使用lscpu或或者cat /proc/cpuinfo | grep 'processor' | wc -l 可以查看CPU的物理核心數。
配置:worker_processes auto;
配置段:main
2.worker_cpu_affinity cpumask
作用:使用這個指令可以綁定到具體那顆CPU。如果你有4顆CPU,可以這麼配置
配置:worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;
配置段:main
3.worker_priority number;
作用:指定worker進程的nice值,設定worker進程優先級;[-20,20]
配置: worker_priority 0;
配置段:main
4.worker_rlimit_nofile number;
作用:worker進程所能夠打開的文件數量上限;
配置:worker_rlimit_nofile 20960;
配置段:main
5.accept_mutex
作用:如果你有多個workers,那麼還要配置影響worker的相關指令。accept_mutex指令將使進程worker逐個接受新連接。默認爲on,如果爲off會導致驚羣現象,即每秒重複多次連接,產生過多的上下文切換,降低服務器的性能。
配置:accept_mutex on;
配置段:events
6.accept_mutex_delay
作用:acceppet_mutex開啓時纔有效,這表示只有一個互斥的worker進程接受連接,其他的在延遲世間內輪流等待新的連接。
配置: accept_mutex_delay 500ms;
配置段:events
7.worker_connections
作用:指明worker進程的最大連接數。
配置:worker_connections 10240;
配置段:events
8.multi_accept
作用:worker進程是否同時接受連接所有新請求。默認爲off,表示一次只接受一個新的請求。
配置:multi_accept off;
配置段:events
2.I/O優化
1.Sendfile
作用:數據直接在內核空間完成文件描述符之間的複製而不經由應用層,這使得操作系統資源的利用率提高。
配置:sendifile on;
配置段:http,server和location代碼塊
2.直接I/O(directio)
作用:操作系統內核通常嘗試優化和緩存任何讀/寫請求,但是對於一些較大的數據或者說緩存命中率較差的數據,我們卻不期望緩存在高速緩存中,我們應該按需加載。直接I/O就提供這麼一個功能,它讓應用數據直接從磁盤中進行I/O請求。這樣一來能夠更好地利用CPU週期和提高緩存命中率。
配置:directio 4m;#任何大於4m的文件將由直接I/O加載。
directio_alignment 512; #設置數據傳輸的塊大小。
配置段:http,server和location代碼塊
3.異步I/O(aio)
作用:異步I/O允許進程進行不受阻塞或者不需要等待I/O完成的I/O操作。
配置:aio off;#按需配置,在linux上需要啓動direction
配置段:http,server和location代碼塊
3.TCP優化
http使用tcp傳輸數據,數據以tcp分組的形式傳遞。Nginx提供了改變底層TCP棧行爲的指令,這些屬性能夠更改單個套接字連接的屬性。
1.TCP_NODELAY
作用:TCP/IP傳輸時存在大量的小包問題。啓用這個指令表示在keepalived模式中不緩存,不延遲小文件而把小文件快速發出去。
配置: tcp_nodelay on;
配置段:http,server和location代碼塊
2.TCP_CORK
作用:它不把小文件發出去,而是打包發出去。
配置:tcp_nopush off;
配置段:http,server和location代碼塊
4.buffer優化
在接受請求時,nginx提供可可以將請求數據寫入請求緩存區的功能。這些緩存區的數據可以作爲Nginx的變量使用。緩存區大小與請求的數據大小相比,若小於請求的數據,則將數據寫入磁盤文件中。
1.client_body_buffer_size
作用:此指令設置請求主體的緩衝區大小。 如果主體超過緩衝區大小,則完整主體或其一部分將寫入臨時文件。
配置:client_body_buffer_size 16k;
配置段:http,server和location代碼塊
2.client_max_body_size
作用:此指令設置NGINX能處理的最大請求主體大小。 如果請求大於指定的大小,則NGINX發回HTTP 413(Request Entity too large)錯誤。
配置: client_max_body_size 2m;
配置段:http,server和location代碼塊
3.client_body_in_file_only
作用:此指令禁用NGINX緩衝區並將請求體存儲在臨時文件中。
配置:client_body_in_file_only off;
配置段:http,server和location代碼塊
4.client_body_in_single_buffer
作用:該指令設置NGINX將完整的請求主體存儲在單個緩衝區中。默認off,啓用能夠優化讀取$request_body變量涉及的I/O操作。
配置:client_body_in_single_buffer off;
配置段:http,server和location代碼塊
5.client_header_buffer_size
作用:它爲請求頭分配一個緩衝區。 如果請求頭大小大於指定的緩衝區,則使用large_client_header_buffers指令分配更大的緩衝區。
配置:client_header_buffer_size 1m;
配置段:http, server
6.large_client_header_buffers
作用:此指令規定了用於讀取大型客戶端請求頭的緩衝區的最大數量和大小。當在默認的緩存區不足時按需分配,如果請求轉入長連接則釋放緩存區。
配置: large_client_header_buffers 4 8k;
配置段:http, server
5.靜態內容緩存優化
使用open_file_cache進一步提高性能, NGINX緩存將最近使用的文件描述符和相關元數據(如修改時間,大小等)存儲在緩存中。 緩存不會存儲所請求文件的內容。
1.open_file_cache
作用:緩存文件描述符的元數據,如大小,修改時間,權限等等。緩存清理使用LRU算法。
配置:open_file_cache max=1000 inactive=20s;
配置段:http,server和location代碼塊
2.open_file_cache_valid
作用:檢驗緩存中元素有效性的頻率;默認爲60s。
配置:open_file_cache_valid 30s;
配置段:http,server和location代碼塊
3.open_file_cache_min_uses
作用:NGINX將在非活動時間段之後從高速緩存中清除元素。在非活動時長訪問的次數超過了指定次數,則不淘汰;否則淘汰。
配置:open_file_cache_min_uses 4;
配置段:http,server和location代碼塊
4.open_file_cache_errors
作用:NGINX可以緩存在文件訪問期間發生的錯誤。
配置: open_file_cache_errors off;
配置段:http,server和location代碼塊
6.FastCGI緩存優化
1.啓用緩存fastcgi_cache_path
作用:定義fastcgi的緩存;緩存位置爲磁盤上的文件系統,由path所指定路徑來定義。levels=levels:指定緩存目錄的層級數量,以及每一級的目錄數量。keys_zone=name:size,指定k/v映射的內存空間的名稱及大小。inactive=time,指定非活動時長。max_size=size,磁盤上用於緩存數據的緩存空間上限。
配置:fastcgi_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m;
fastcgi_cache_key $request_method://$host$request_uri;
配置段:http,server和location代碼塊
2.fastcgi_cache
作用:調用指定的緩存空間來緩存數據
配置:fastcgi_cache off;
配置段;http,server和location代碼塊
3.fastcgi_cache_key string;
作用:定義緩存項的key的字符串。
配置:fastcgi_cache_key localhost:9000$request_uri;
配置段;http,server和location代碼塊
4.fastcgi_cache_methods
作用:爲哪些請求方法使用緩存。
配置:fastcgi_cache_methods GET HEAD;
配置段;http,server和location代碼塊
5.fastcgi_cache_min_uses
作用:緩存空間中的緩存項在非活動時間內至少要被訪問到此處所指定的次數方可被認作活動項;
配置:fastcgi_cache_min_uses 4;
配置段;http,server和location代碼塊
6.fastcgi_cache_valid
作用:指定緩存的響應碼和各自的緩存時長。
配置:fastcgi_cache_valid 200 302 10m;
配置段:http,server和location代碼塊
7.fastcgi_keep_conn
作用:指定FastCGI服務是否開啓長連接。默認爲off。
配置:fastcgi_keep_conn off;
配置段:http,server和location代碼塊
8.FastCGI緩存示例
示例:
http { ... fastcgi_cache_path /var/cache/nginx/fastcgi_cache levels=1:2:1 keys_zone=fcgi:20m inactive=120s; ... server { ... location ~* \.php$ { ... fastcgi_cache fcgi; fastcgi_cache_key $request_uri; fastcgi_cache_valid 200 302 10m; fastcgi_cache_valid 301 1h; fastcgi_cache_valid any 1m; ... } ... } ... }
7.proxy緩存優化
1.proxy_pass
作用:指明代理的路徑
配置:proxy_pass http://localhost:8000/uri/;
配置段:location, if in location, limit_except
2. proxy_set_header
作用:設定發往後端主機的請求報文的請求首部的值。
配置:proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
配置段:http,server和location代碼塊
3.proxy_cache_path
作用:設置緩存的路徑以及其他的相關屬性
配置:proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m;
配置段:http
4. proxy_cache
作用:調用的緩存的名稱,或禁用緩存;
配置:proxy_cache off;
配置段:http,server和location代碼塊
5.proxy_cache_key
作用:緩存條目的鍵
配置:proxy_cache_key "$host$request_uri $cookie_user";
配置段:http,server和location代碼塊
6. proxy_cache_valid
作用:定義各類響應碼的緩存時長;
配置:proxy_cache_valid 200 302 10m;
配置段;http,server和location代碼塊
8.memcached緩存優化
Memcache是一個通用的內存緩存系統,通常用於web加速。nginx提供許多memcached的指令,從而使得可以直接訪問memcached提供的內容,避免對上游服務器的影響。
$memcached_key變量,用於執行高速緩存查找。如果要使用Memcached進行查找操作,必須給$memcached_key設置一個值,該值根據URL確定。
1.memcached_pass
作用:此指令用於指定memcached服務器的位置。可以使用三種方式指定:(1)域名或IP,以及可選端口(2)unix套接字(3)upstream創建的一組服務器
配置:set $memached_key $uri;
memcached_pass localhost:11211;
配置段:location, if in location
2.memcached_connect_timeout
作用:指定連接超時時長
配置:memcached_connect_timeout 30s;
配置段:http,server和location代碼塊
3.memcached_ send_timeout
作用:指定請求寫入memcached服務器的超時時長
配置:.memcached_ send_timeout 30s;
配置段:http,server和location代碼塊
4.memcached_read_timeout
作用:指定從memcached服務器讀取響應的超時超時。
配置:memcached_read_timeout 30s;
配置段;http,server和location代碼塊
5.memcached_bind
作用:將服務器的IP與memcached服務器綁定。默認爲關閉,但會自動爲服務器自動選擇一臺服務器的IP連接memcached。
示例: server{ location /python/css/ { alias "/code/location/css/"; } location /python/ { set $memcached_key "$request_method$request_uri"; charset utf-8; memcached_pass 127.0.0.1:11211; error_page 404 502 504 = @pythonfallback; default_type text/html; } location @pythonfallback { rewrite ^/python/(.*) /$1 break; proxy_pass http://127.0.0.1:5000; proxy_set_header X-Cache-Key "$request_method$request_uri"; } # Rest NGINX configuration omitted for brevity }