nginx 的一些優化

 

通常來講 nginx 配置文件中對優化比較有做用的爲如下幾項: 
worker_processes 8; 
nginx 進程數,建議按照 cpu 數目來指定,通常爲它的倍數。 
worker_cpu_affinity  00000001  00000010  00000100  00001000  00010000  00100000 
01000000 10000000; 
爲每一個進程分配 cpu,上例中將 8 個進程分配到 8 個 cpu,固然能夠寫多個,或者將一
個進程分配到多個 cpu。 
worker_rlimit_nofile 102400; 
這個指令是指當一個 nginx 進程打開的最多文件描述符數目,理論值應該是最多打開文
件數(ulimit -n)與 nginx 進程數相除,可是 nginx 分配請求並非那麼均勻,因此最好與 ulimit 
-n 的值保持一致。 
use epoll; 
使用 epoll 的 I/O 模型,這個不用說了吧。 
worker_connections 102400; 
每 個 進程 容許 的最 多連 接 數, 理論 上每 臺 nginx   服 務 器的 最大 鏈接 數爲
worker_processes*worker_connections。 
keepalive_timeout 60; 
keepalive 超時時間。 
client_header_buffer_size 4k; 
客戶端請求頭部的緩衝區大小,這個能夠根據你的系統分頁大小來設置,通常一個請求
頭的大小不會超過 1k,不過因爲通常系統分頁都要大於 1k,因此這裏設置爲分頁大小。分
頁大小能夠用命令 getconf PAGESIZE 取得。 
open_file_cache max=102400 inactive=20s; 
這個將爲打開文件指定緩存,默認是沒有啓用的,max 指定緩存數量,建議和打開文件
數一致,inactive 是指通過多長時間文件沒被請求後刪除緩存。 
open_file_cache_valid 30s; 
這個是指多長時間檢查一次緩存的有效信息。 
open_file_cache_min_uses 1; 
open_file_cache 指令中的 inactive 參數時間內文件的最少使用次數,若是超過這個數字,文
件描述符一直是在緩存中打開的,如上例,若是有一個文件在 inactive 時間內一次沒被使用,
它將被移除。


 
關於內核參數的優化: 
net.ipv4.tcp_max_tw_buckets = 6000 
timewait 的數量,默認是 180000。 
net.ipv4.ip_local_port_range = 1024    65000 
容許系統打開的端口範圍。 
net.ipv4.tcp_tw_recycle = 1 
啓用 timewait 快速回收。 
net.ipv4.tcp_tw_reuse = 1 
開啓重用。容許將 TIME-WAIT sockets 從新用於新的 TCP 鏈接。 
net.ipv4.tcp_syncookies = 1 
開啓 SYN Cookies,當出現 SYN 等待隊列溢出時,啓用 cookies 來處理。 
net.core.somaxconn = 262144 
web 應用中 listen 函數的 backlog 默認會給咱們內核參數的 net.core.somaxconn 限制到
128,而 nginx 定義的 NGX_LISTEN_BACKLOG 默認爲 511,因此有必要調整這個值。 
net.core.netdev_max_backlog = 262144 
每一個網絡接口接收數據包的速率比內核處理這些包的速率快時,容許送到隊列的數據包
的最大數目。 
net.ipv4.tcp_max_orphans = 262144 
系統中最多有多少個 TCP 套接字不被關聯到任何一個用戶文件句柄上。若是超過這個數
字,孤兒鏈接將即刻被複位並打印出警告信息。這個限制僅僅是爲了防止簡單的 DoS 攻擊,
不能過度依靠它或者人爲地減少這個值,更應該增長這個值(若是增長了內存以後)。 
net.ipv4.tcp_max_syn_backlog = 262144 
記錄的那些還沒有收到客戶端確認信息的鏈接請求的最大值。對於有 128M 內存的系統而
言,缺省值是 1024,小內存的系統則是 128。 
net.ipv4.tcp_timestamps = 0 
時間戳能夠避免序列號的卷繞。一個 1Gbps 的鏈路確定會遇到之前用過的序列號。時間
戳可以讓內核接受這種「異常」的數據包。這裏須要將其關掉。 
net.ipv4.tcp_synack_retries = 1 
爲了打開對端的鏈接,內核須要發送一個 SYN 並附帶一個迴應前面一個 SYN 的 ACK。也
就是所謂三次握手中的第二次握手。這個設置決定了內核放棄鏈接以前發送 SYN+ACK  包的
數量。 
net.ipv4.tcp_syn_retries = 1 
在內核放棄創建鏈接以前發送 SYN 包的數量。 
net.ipv4.tcp_fin_timeout = 1 
若是套接字由本端要求關閉,這個參數決定了它保持在 FIN-WAIT-2  狀態的時間。對端
能夠出錯並永遠不關閉鏈接,甚至意外當機。缺省值是 60 秒。2.2  內核的一般值是 180 秒,
 

 
你能夠按這個設置,但要記住的是,即便你的機器是一個輕載的 WEB 服務器,也有由於大
量的死套接字而內存溢出的風險,FIN-  WAIT-2 的危險性比 FIN-WAIT-1 要小,由於它最多隻
能吃掉 1.5K 內存,可是它們的生存期長些。 
net.ipv4.tcp_keepalive_time = 30 
當 keepalive 起用的時候,TCP 發送 keepalive 消息的頻度。缺省是 2 小時。 
下面貼一個完整的內核優化設置: 
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 
net.ipv4.tcp_syncookies = 1 
kernel.msgmnb = 65536 
kernel.msgmax = 65536 
kernel.shmmax = 68719476736 
kernel.shmall = 4294967296 
net.ipv4.tcp_max_tw_buckets = 6000 
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.core.netdev_max_backlog = 262144 
net.core.somaxconn = 262144 
net.ipv4.tcp_max_orphans = 3276800 
net.ipv4.tcp_max_syn_backlog = 262144 
net.ipv4.tcp_timestamps = 0 
net.ipv4.tcp_synack_retries = 1 
net.ipv4.tcp_syn_retries = 1 
net.ipv4.tcp_tw_recycle = 1 
net.ipv4.tcp_tw_reuse = 1 
net.ipv4.tcp_mem = 94500000 915000000 927000000 
net.ipv4.tcp_fin_timeout = 1 
net.ipv4.tcp_keepalive_time = 30 
net.ipv4.ip_local_port_range = 1024    65000 
 

 
下面是一個簡單的 nginx 配置文件: 
user  www www; 
worker_processes 8; 
worker_cpu_affinity  00000001  00000010  00000100  00001000  00010000  00100000 
01000000; 
error_log  /www/log/nginx_error.log  crit; 
pid        /usr/local/nginx/nginx.pid; 
worker_rlimit_nofile 204800; 
 
events 

  use epoll; 
  worker_connections 204800; 

 
http 

  include        mime.types; 
  default_type  application/octet-stream; 
 
  charset  utf-8; 
 
  server_names_hash_bucket_size 128; 
  client_header_buffer_size 2k; 
  large_client_header_buffers 4 4k; 
  client_max_body_size 8m; 
 
  sendfile on; 
  tcp_nopush      on; 
 
  keepalive_timeout 60; 
 
  fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 
                keys_zone=TEST:10m 
                inactive=5m; 
  fastcgi_connect_timeout 300; 
  fastcgi_send_timeout 300; 
  fastcgi_read_timeout 300; 
  fastcgi_buffer_size 4k; 
  fastcgi_buffers 8 4k; 
  fastcgi_busy_buffers_size 8k; 
  fastcgi_temp_file_write_size 8k; 
 

 
  fastcgi_cache TEST; 
  fastcgi_cache_valid 200 302 1h; 
  fastcgi_cache_valid 301 1d;   
  fastcgi_cache_valid any 1m; 
  fastcgi_cache_min_uses 1; 
  fastcgi_cache_use_stale error timeout invalid_header http_500; 
   
  open_file_cache max=204800 inactive=20s; 
  open_file_cache_min_uses 1; 
  open_file_cache_valid 30s; 
   
 
 
  tcp_nodelay on; 
   
  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; 
 
 
  server 
  { 
    listen        8080; 
    server_name  backup.aiju.com; 
    index index.php index.htm; 
    root  /www/html/; 
 
    location /status 
    { 
        stub_status on; 
    } 
 
    location ~ .*\.(php|php5)?$ 
    { 
        fastcgi_pass 127.0.0.1:9000; 
        fastcgi_index index.php; 
        include fcgi.conf; 
    } 
 
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ 
 

 
    { 
      expires      30d; 
    } 
 
    log_format  access  '$remote_addr - $remote_user [$time_local] "$request" ' 
              '$status $body_bytes_sent "$http_referer" ' 
              '"$http_user_agent" $http_x_forwarded_for'; 
    access_log  /www/log/access.log  access; 
      } 

關於 FastCGI 的幾個指令: 
fastcgi_cache_path      /usr/local/nginx/fastcgi_cache      levels=1:2      keys_zone=TEST:10m 
inactive=5m; 
這個指令爲 FastCGI 緩存指定一個路徑,目錄結構等級,關鍵字區域存儲時間和非活動
刪除時間。 
fastcgi_connect_timeout 300; 
指定鏈接到後端 FastCGI 的超時時間。 
fastcgi_send_timeout 300; 
向 FastCGI 傳送請求的超時時間,這個值是指已經完成兩次握手後向 FastCGI 傳送請求的
超時時間。 
fastcgi_read_timeout 300; 
接收 FastCGI 應答的超時時間,這個值是指已經完成兩次握手後接收 FastCGI 應答的超時
時間。 
fastcgi_buffer_size 4k; 
指定讀取 FastCGI 應答第一部分須要用多大的緩衝區,通常第一部分應答不會超過 1k,
因爲頁面大小爲 4k,因此這裏設置爲 4k。 
fastcgi_buffers 8 4k; 
指定本地須要用多少和多大的緩衝區來緩衝 FastCGI 的應答。 
fastcgi_busy_buffers_size 8k; 
這個指令我也不知道是作什麼用,只知道默認值是 fastcgi_buffers 的兩倍。 
fastcgi_temp_file_write_size 8k; 
在寫入 fastcgi_temp_path 時將用多大的數據塊,默認值是 fastcgi_buffers 的兩倍。 
fastcgi_cache TEST 
開啓 FastCGI 緩存而且爲其制定一個名稱。我的感受開啓緩存很是有用,能夠有效下降
CPU 負載,而且防止 502 錯誤。 
fastcgi_cache_valid 200 302 1h; 
 

 
fastcgi_cache_valid 301 1d; 
fastcgi_cache_valid any 1m; 
爲指定的應答代碼指定緩存時間,如上例中將 200,302 應答緩存一小時,301 應答緩
存 1 天,其餘爲 1 分鐘。 
fastcgi_cache_min_uses 1; 
緩存在 fastcgi_cache_path 指令 inactive 參數值時間內的最少使用次數,如上例,若是在
5 分鐘內某文件 1 次也沒有被使用,那麼這個文件將被移除。 
fastcgi_cache_use_stale error timeout invalid_header http_500; 
不知道這個參數的做用,猜測應該是讓 nginx 知道哪些類型的緩存是沒用的。 
以上爲 nginx 中 FastCGI 相關參數,另外,FastCGI 自身也有一些配置須要進行優化,如
果你使用 php-fpm 來管理 FastCGI,能夠修改配置文件中的如下值: 
<value name="max_children">60</value> 
同時處理的併發請求數,即它將開啓最多 60 個子線程來處理併發鏈接。 
<value name="rlimit_files">102400</value> 
最多打開文件數。 
<value name="max_requests">204800</value> 
每一個進程在重置以前可以執行的最多請求數。 

參考資料: 
http://blog.chinaunix.net/u3/105004/showart_2087155.html 
http://nginx.179401.cn/ 
http://blog.s135.com/nginx_php_v5/javascript

 

 

 

kernel.core_uses_pid = 1 
net.ipv4.ip_forward = 1 
net.ipv4.conf.lo.arp_ignore = 1 
net.ipv4.conf.lo.arp_announce = 2 
net.ipv4.conf.all.arp_ignore = 1 
net.ipv4.conf.all.arp_announce = 2 
net.ipv4.tcp_tw_reuse = 1 
net.ipv4.tcp_tw_recycle = 1php

net.ipv4.tcp_fin_timeout = 30 
net.ipv4.tcp_keepalive_time = 300 
net.ipv4.tcp_window_scaling = 0 
net.ipv4.tcp_sack = 0 
net.ipv4.tcp_timestamps = 0 
net.ipv4.tcp_syncookies = 1 
net.ipv4.icmp_echo_ignore_broadcasts = 1 
net.ipv4.icmp_ignore_bogus_error_responses = 1 
net.ipv4.conf.all.log_martians = 1 
net.ipv4.tcp_max_syn_backlog = 4096 
net.ipv4.tcp_max_tw_buckets = 1440000 
net.ipv4.ip_local_port_range = 1024 65536 
net.core.rmem_max = 16777216 
net.core.wmem_max = 16777216 
net.ipv4.tcp_rmem = 4096 87380 16777216 
net.ipv4.tcp_wmem = 4096 65536 16777216 
net.ipv4.tcp_keepalive_intvl = 15 
net.ipv4.tcp_retries2 = 5 
fs.file-max = 655360 
net.core.somaxconn = 4096css

相關文章
相關標籤/搜索