如何最佳化高流量網站的nginx和php-fpm的幾個方面

來源:互聯網
上載者:User
通訊機制的選擇

nginx和php-fpm 是使用 tcp socket 還是 unix socket 。 合理的配置nginx處理請求數

#cat /proc/cpuinfo | grep processor #查看伺服器cpu的處理器數量# vi /etc/nginx/nginx.confworker_processes 16;  #修改為處理器數量events {   worker_connections 4096; # 單個woker進程最大串連並發數   multi_accept on;  #linux2.6+預設epoll,如果使用了更優秀的kqueue模型,則使用預設off。}
配置nginx+php-fpm負載平衡

單機能力有限,比如要支援1000台並發,產生兩個sock檔案,讓每個php-fpm處理500台。

# nginx.confupstream backend {   server unix:/dev/shm/php-fpm.sock1 weight=100 max_fails=5 fail_timeout=5;   server unix:/dev/shm/php-fpm.sock2 weight=100 max_fails=5 fail_timeout=5; }# php-fpm.conf(同理,php7在的設定檔末行引入了pool.d的所有配置)# www1.conflisten = /dev/shm/php-fpm.sock1;listen.backlog = -1  listen.allowed_clients = 127.0.0.1pm.max_children = 500pm.max_requests = 5000rlimit_files = 50000request_slowlog_timeout = 20sslowlog = /var/log/php-slow.log# cp www1.conf www.conf2listen = /dev/shm/php-fpm.sock2;
禁止訪問記錄檔

高流量網站涉及大量I/O,必須線上程間同步。

# nginx.confaccess_log off; log_not_found off; error_log /var/log/nginx-error.log warn;

如果不能關閉日誌訪問,至少設定緩衝

access_log /var/log/nginx/access.log main buffer=16k;
啟用GZip
# nginx.confgzip on; gzip_disable "msie6"; gzip_vary on; gzip_proxied any; gzip_comp_level 6; gzip_min_length 1100; gzip_buffers 16 8k; gzip_http_version 1.1; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
緩衝經常訪問的檔案
# nginx.confopen_file_cache max=2000 inactive=20s; open_file_cache_valid 60s; open_file_cache_min_uses 5; open_file_cache_errors off;
調整用戶端逾時
# nginx.confclient_max_body_size 50M; client_body_buffer_size 1m; client_body_timeout 15; client_header_timeout 15; keepalive_timeout 2 2; send_timeout 15; sendfile on; tcp_nopush on; tcp_nodelay on;
調整輸出緩衝區
# nginx.conffastcgi_buffers 256 16k; fastcgi_buffer_size 128k; fastcgi_connect_timeout 3s; fastcgi_send_timeout 120s; fastcgi_read_timeout 120s; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; reset_timedout_connection on; server_names_hash_bucket_size 100;
調整/etc/sysctl.conf
# Recycle Zombie connections net.inet.tcp.fast_finwait2_recycle=1 net.inet.tcp.maxtcptw=200000 # Increase number of files kern.maxfiles=65535 kern.maxfilesperproc=16384 # Increase page share factor per process vm.pmap.pv_entry_max=54272521 vm.pmap.shpgperproc=20000 # Increase number of connections vfs.vmiodirenable=1 kern.ipc.somaxconn=3240000 net.inet.tcp.rfc1323=1 net.inet.tcp.delayed_ack=0 net.inet.tcp.restrict_rst=1 kern.ipc.maxsockbuf=2097152 kern.ipc.shmmax=268435456 # Host cache net.inet.tcp.hostcache.hashsize=4096 net.inet.tcp.hostcache.cachelimit=131072 net.inet.tcp.hostcache.bucketlimit=120 # Increase number of ports net.inet.ip.portrange.first=2000 net.inet.ip.portrange.last=100000 net.inet.ip.portrange.hifirst=2000 net.inet.ip.portrange.hilast=100000 kern.ipc.semvmx=131068 # Disable Ping-flood attacks net.inet.tcp.msl=2000 net.inet.icmp.bmcastecho=1 net.inet.icmp.icmplim=1 net.inet.tcp.blackhole=2 net.inet.udp.blackhole=1
Nginx狀態監控

Nginx中的stub_status模組主要用於查看Nginx的一些狀態資訊,預設不會編譯進Nginx,重新編譯安裝nginx stub_status模組,

持續監視開啟的串連數,可用記憶體和等待線程數。 設定警報以在閾值超過時通知您。您可以自己構建這些警報,或使用像ServerDensity。 請務必安裝NGINX stub_status模組 你需要重新編譯NGINX -

./configure \--prefix=/usr/local/nginx \--with-http_stub_status_module \make && make install

安裝完畢後在server塊中加入location

server{           location /nginx-status {               stub_status on;          }  } 

重啟nginx後訪問www.x.com/nginx-status即可看到返回的資訊

active connections – 活躍的串連數量server accepts handled requests — 總共處理了11989個串連 , 成功建立11989次握手, 總共處理了11991個請求reading — 讀取用戶端的串連數.writing — 響應資料到用戶端的數量waiting — 開啟 keep-alive 的情況下,這個值等於 active – (reading+writing), 意思就是 Nginx 已經處理完正在等候下一次請求指令的駐留串連.

參考
Nginx基本配置整理
Nginx Core functionality
Optimizing NGINX and PHP-fpm for high traffic sites
啟用nginx status狀態詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.