標準狀況下,軟件默認的參數都是對安裝軟件的硬件標準(最低配置)來設置的,目前咱們服務器的硬件資源遠遠大於要求的標準,因此爲了讓服務器性能更加出衆,充分利用服務器的硬件資源,咱們通常須要優化APP的併發數來提高服務器的性能。
總結來講:1.服務器大併發實現;2.提高用戶體驗;3.爲公司省錢。javascript
Nginx是主進程+工做進程模型,也就是一個主進程會掛幾個工做進程。php
$ cat /proc/cpuinfo | grep "flags" | wc -l 4
worker_processes 4; worker_cpu_affinity 0001 0010 0100 1000; # 依次表明從1到4這四個核 events { worker_connections 10240; # 這個值通常先設比較小,觀察進程消耗再決定是否增長併發數 }
# 查看進程及進程是否處於對應核心上 $ ps -eo psr,pid,args | grep "nginx" 0 13977 nginx: master process /usr/local/nginx/sbin/nginx # 父進程 0 13978 nginx: worker process 1 13979 nginx: worker process 2 13980 nginx: worker process 3 13981 nginx: worker process 0 14284 grep --color=auto nginx # 查看nginx鏈接數 $ netstat -antpl | grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13977/nginx: master # 查看nginx當前鏈接的人數 $ netstat -antpl | grep nginx | grep ESTABLISHED | wc -l 0
nginx這種web服務器軟件都是屬於TCP協議的軟件,TCP協議的特色就在於每次鏈接時要進行三次握手,握手成功後再通信數據,通信結束後四次揮手斷開鏈接。
而http協議是一個無狀態協議, 每次進行通信都要三次握手四次揮手,那服務器就須要常常去維護握手和斷開。 服務器壓力過大、浪費的資源也過多。
所以通常都須要開啓長鏈接,在第一個請求結束後,等一段時間,若是這個時間內,再有請求過來,則不斷開鏈接,直接將數據發送給客戶端。這樣就下降了握手和揮手的次數和頻率。css
# 關閉長鏈接:0表明關閉 keepalive_timeout 0; # 開啓長鏈接 # keepalive_timeout 65; # 一個長鏈接處理最大請求數(按期釋放內存,防止內存溢出) # keepalive_requests 8192;
關閉長鏈接時:html
# 瀏覽器訪問完馬上進入TIME_WAIT狀態(主動關閉) $ netstat -antpl Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1317/master tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 14698/nginx: master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1095/sshd tcp 0 0 192.168.31.42:80 192.168.31.28:64977 TIME_WAIT - tcp 0 0 192.168.31.42:80 192.168.31.28:64978 TIME_WAIT - tcp 0 0 192.168.31.42:80 192.168.31.28:64979 TIME_WAIT - tcp 0 0 192.168.31.42:22 192.168.31.28:64678 ESTABLISHED 14636/sshd: root@pt tcp6 0 0 ::1:25 :::* LISTEN 1317/master tcp6 0 0 :::22 :::* LISTEN 1095/sshd # 一段時間後鏈接消息消失 $ netstat -antpl Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1317/master tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 14698/nginx: master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1095/sshd tcp 0 0 192.168.31.42:22 192.168.31.28:64678 ESTABLISHED 14636/sshd: root@pt tcp6 0 0 ::1:25 :::* LISTEN 1317/master tcp6 0 0 :::22 :::* LISTEN 1095/sshd
開啓長鏈接時:前端
# 因爲前面設置長鏈接超時時間是65秒,這段時間內一直保持在ESTABLISHED狀態: $ netstat -antpl Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1317/master tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 14828/nginx: master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1095/sshd tcp 0 0 192.168.31.42:80 192.168.31.28:51700 ESTABLISHED 14829/nginx: worker tcp 0 0 192.168.31.42:22 192.168.31.28:50373 ESTABLISHED 14777/sshd: root@pt tcp 0 0 192.168.31.42:22 192.168.31.28:64678 ESTABLISHED 14636/sshd: root@pt tcp 0 0 192.168.31.42:80 192.168.31.28:51701 ESTABLISHED 14829/nginx: worker tcp6 0 0 ::1:25 :::* LISTEN 1317/master tcp6 0 0 :::22 :::* LISTEN 1095/sshd # 時間超時長鏈接斷開 $ netstat -antpl Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1317/master tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 14828/nginx: master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1095/sshd tcp 0 0 192.168.31.42:22 192.168.31.28:50373 ESTABLISHED 14777/sshd: root@pt tcp 0 0 192.168.31.42:22 192.168.31.28:64678 ESTABLISHED 14636/sshd: root@pt tcp6 0 0 ::1:25 :::* LISTEN 1317/master tcp6 0 0 :::22 :::* LISTEN 1095/sshd
關閉長鏈接時:
java
開啓長鏈接時:
nginx
gzip(GNU-ZIP)是一種壓縮技術。通過gzip壓縮後頁面大小能夠變爲原來的30%甚至更小,這樣,用戶瀏覽頁面的時候速度會塊得多。gzip的壓縮頁面須要瀏覽器和服務器雙方都支持,實際上就是服務器端壓縮,傳到瀏覽器後瀏覽器解壓並解析。瀏覽器那裏不須要咱們擔憂,由於目前的巨大多數瀏覽器都支持解析gzip過的頁面。
Nginx的壓縮輸出有一組gzip壓縮指令來實現。相關指令位於http{….}兩個大括號之間。web
gzip on; # 啓動gzip壓縮功能 gzip_proxied any; # nginx作前端代理時啓用該選項,表示不管後端服務器的headers返回什麼信息,都無條件啓用壓縮 gzip_min_length 1k; # 小於1k的小文件不壓縮(小文件可能會越壓縮越大) gzip_buffers 4 8k; # 設置系統獲取幾個單位的緩存用於存儲gzip的壓縮結果數據流,按照原始數據大小以8k爲單位申請4倍內存空間 gzip_comp_level 6; # gzip壓縮級別,1壓縮比最小處理速度最快,9壓縮比最大處理最慢也最消耗CPU,通常設置爲3便可 gzip_types text/plain text/css application/x-javascript application/javascript application/xml; # 什麼類型的頁面或文檔啓用壓縮 gzip_vary on; # 開啓在http header中添加Vary:Accept-Encoding
語法:expires [time|epoch|max|off]
默認值:expires off
做用域:http,server,location後端
location ~* \.(js|css)?$ { expires 1h; }
server { listen 80; server_name localhost; location / { root html/web1; index index.html index.htm; } # 客戶端緩存設置:png或gif文件在客戶端緩存一個小時 location ~* \.(png|gif)$ { expires 1h; }
測試瀏覽器刷新以Chrome爲例:瀏覽器
公司網站平常PV 60萬,Nginx服務器該如何優化?