nginx優化

最近在服務器上搞了一些nginx 研究了一下 總結總結nginx配置文件裏面需要注意的一些參數

worker_processes 8

nginx要開啓的進程數 一般等於cpu的總核數 其實一般情況下開4個或8個就可 我開2個

以了 多了沒有太多用
每個nginx進程消耗的內存10兆的模樣

worker_cpu_affinity
僅適用於linux,使用該選項可以綁定worker進程和CPU(2.4內核的機器用不

了)
假如是8 cpu 分配如下:
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000

00100000 01000000 10000000

nginx可以使用多個worker進程,原因如下:

to use SMP 
to decrease latency when workers blockend on disk I/O 
to limit number of connections per process when select()/poll() is

used 
The worker_processes and worker_connections from the event sections

allows you to calculate maxclients value: k

max_clients = worker_processes * worker_connections

worker_rlimit_nofile 102400;

每個nginx進程打開文件描述符最大數目 配置要和系統的單進程打開文件數一

致,linux 2.6內核下開啓文件打開數爲65535,worker_rlimit_nofile就相應

應該填寫65535 
nginx調度時分配請求到進程並不是那麼的均衡,假如超過會返回502錯誤。我

這裏寫的大一點

use epoll

Nginx使用了最新的epoll(Linux 2.6內核)和kqueue(freebsd)網絡I/O模

型,而Apache則使用的是傳統的select模型。
處理大量的連接的讀寫,Apache所採用的select網絡I/O模型非常低效。
在高併發服務器中,輪詢I/O是最耗時間的操作 目前Linux下能夠承受高併發

訪問的Squid、Memcached都採用的是epoll網絡I/O模型。

worker_connections 65535;
每個工作進程允許最大的同時連接數 (Maxclient = work_processes * worker_connections)

keepalive_timeout 75

keepalive超時時間

這裏需要注意官方的一句話:
The parameters can differ from each other. Line Keep-Alive:

timeout=time understands Mozilla and Konqueror. MSIE itself shuts

keep-alive connection approximately after 60 seconds.

client_header_buffer_size 16k
large_client_header_buffers 4 32k
客戶請求頭緩衝大小 
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中緩存項目的有效信息.


開啓gzip
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css

application/xml;
gzip_vary on;

緩存靜態文件:

location ~* ^.+\.(swf|gif|png|jpg|js|css)$ {
root /usr/local/ku6/ktv/show.ku6.com/;
expires 1m;
}


優化Linux內核參數

vi /etc/sysctl.conf

  

# Add
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768

net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216

net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2

net.ipv4.tcp_tw_recycle = 1
#net.ipv4.tcp_tw_len = 1
net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800

#net.ipv4.tcp_fin_timeout = 30
#net.ipv4.tcp_keepalive_time = 120
net.ipv4.ip_local_port_range = 1024 65535

附錄:一些錯誤排查


php-cgi進程數不夠用、php執行時間長(mysql慢)、或者是php-cgi進程死掉

,都會出現502錯誤

一般來說Nginx 502 Bad Gateway和php-fpm.conf的設置有關,而Nginx 504 Gateway Time-out則是與nginx.conf的設置有關

1、查看當前的PHP FastCGI進程數是否夠用:

netstat -anpo | grep "php-cgi" | wc -l

  如果實際使用的“FastCGI進程數”接近預設的“FastCGI進程數”,那麼

,說明“FastCGI進程數”不夠用,需要增大。

  2、部分PHP程序的執行時間超過了Nginx的等待時間,可以適當增加

nginx.conf配置文件中FastCGI的timeout時間,例如:

http 
{
......
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;

......
}


413 Request Entity Too Large
增大client_max_body_size

client_max_body_size:指令指定允許客戶端連接的最大請求實體大小,它出現在請求頭部的Content-Length字段. 如果請求大於指定的值,客戶端將收到一個"Request Entity Too Large" (413)錯誤. 記住,瀏覽器並不知道怎樣顯示這個錯誤.

php.ini中增大
post_max_size 和upload_max_filesize

發佈了46 篇原創文章 · 獲贊 5 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章