nginx 配置优化详解
生活随笔
收集整理的這篇文章主要介紹了
nginx 配置优化详解
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
?
?
# nginx不同于apache服務(wù)器,當(dāng)進(jìn)行了大量優(yōu)化設(shè)置后會魔術(shù)般的明顯性能提升效果 # nginx在安裝完成后,大部分參數(shù)就已經(jīng)是最優(yōu)化了,我們需要管理的東西并不多#user nobody;#阻塞和非阻塞網(wǎng)絡(luò)模型: #同步阻塞模型,一請求一進(jìn)(線)程,當(dāng)進(jìn)(線)程增加到一定程度后 #更多CPU時間浪費(fèi)到切換一,性能急劇下降,所以負(fù)載率不高 #Nginx基于事件的非阻塞多路復(fù)用(epoll或kquene)模型 #一個進(jìn)程在短時間內(nèi)可以響應(yīng)大量的請求 #建議值 <= cpu核心數(shù)量,一般高于cpu數(shù)量不會帶好處,也許還有進(jìn)程切換開銷的負(fù)面影響 worker_processes 4;#將work process綁定到特定cpu上,避免進(jìn)程在cpu間切換的開銷 worker_cpu_affinity 0001 0010 0100 1000 #4內(nèi)核4進(jìn)程時的設(shè)置方法 #8內(nèi)核4進(jìn)程時的設(shè)置方法 worker_cpu_affinity 00000001 00000010 00000100 10000000# 每進(jìn)程最大可打開文件描述符數(shù)量(linux上文件描述符比較廣義,網(wǎng)絡(luò)端口、設(shè)備、磁盤文件都是) # 文件描述符用完了,新的連接會被拒絕,產(chǎn)生502類錯誤 # linux最大可打開文件數(shù)可通過ulimit -n FILECNT或 /etc/security/limits.conf配置 # 理論值 系統(tǒng)最大數(shù)量 / 進(jìn)程數(shù)。但進(jìn)程間工作量并不是平均分配的,所以可以設(shè)置的大一些 worker_rlimit_nofile 655350 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid;events {# 并發(fā)響應(yīng)能力的關(guān)鍵配置值# 每個進(jìn)程允許的最大同時連接數(shù),work_connectins * worker_processes = maxConnection;# 要注意maxConnections不等同于可響應(yīng)的用戶數(shù)量,# 因為一般一個瀏覽器會同時開兩條連接,如果反向代理,nginx到后端服務(wù)器的連接也要占用連接數(shù)# 所以,做靜態(tài)服務(wù)器時,一般 maxClient = work_connectins * worker_processes / 2# 做反向代理服務(wù)器時 maxClient = work_connectins * worker_processes / 4# 這個值理論上越大越好,但最多可承受多少請求與配件和網(wǎng)絡(luò)相關(guān),也可最大可打開文件,最大可用sockets數(shù)量(約64K)有關(guān)worker_connections 500;# 指明使用epoll 或 kquene (*BSD)use epoll# 備注:要達(dá)到超高負(fù)載下最好的網(wǎng)絡(luò)響應(yīng)能力,還有必要優(yōu)化與網(wǎng)絡(luò)相關(guān)的linux內(nèi)核參數(shù) }http {include mime.types;default_type application/octet-stream;#log_format main '$remote_addr - $remote_user [$time_local] "$request" '# '$status $body_bytes_sent "$http_referer" '# '"$http_user_agent" "$http_x_forwarded_for"';# 關(guān)閉此項可減少IO開銷,但也無法記錄訪問信息,不利用業(yè)務(wù)分析,一般運(yùn)維情況不建議使用access_log off# 只記錄更為嚴(yán)重的錯誤日志,可減少IO壓力error_log logs/error.log crit;#access_log logs/access.log main;# 啟用內(nèi)核復(fù)制模式,應(yīng)該保持開啟達(dá)到最快IO效率sendfile on;# 簡單說,啟動如下兩項配置,會在數(shù)據(jù)包達(dá)到一定大小后再發(fā)送數(shù)據(jù)# 這樣會減少網(wǎng)絡(luò)通信次數(shù),降低阻塞概率,但也會影響響應(yīng)及時性# 比較適合于文件下載這類的大數(shù)據(jù)包通信場景#tcp_nopush on; 在 #tcp_nodelay on|off on禁用Nagle算法 #keepalive_timeout 0;# HTTP1.1支持持久連接alive# 降低每個連接的alive時間可在一定程度上提高可響應(yīng)連接數(shù)量,所以一般可適當(dāng)降低此值keepalive_timeout 30s;# 啟動內(nèi)容壓縮,有效降低網(wǎng)絡(luò)流量gzip on; # 過短的內(nèi)容壓縮效果不佳,壓縮過程還會浪費(fèi)系統(tǒng)資源gzip_min_length 1000;# 可選值1~9,壓縮級別越高壓縮率越高,但對系統(tǒng)性能要求越高gzip_comp_level 4;# 壓縮的內(nèi)容類別gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;# 靜態(tài)文件緩存# 最大緩存數(shù)量,文件未使用存活期open_file_cache max=655350 inactive=20s;# 驗證緩存有效期時間間隔open_file_cache_valid 30s;# 有效期內(nèi)文件最少使用次數(shù)open_file_cache_min_uses 2;server {listen 80;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;location / {root html;index index.html index.htm;}#error_page 404 /404.html;# redirect server error pages to the static page /50x.html#error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {# proxy_pass http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {# root html;# fastcgi_pass 127.0.0.1:9000;# fastcgi_index index.php;# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;# include fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {# deny all;#}}# another virtual host using mix of IP-, name-, and port-based configuration##server {# listen 8000;# listen somename:8080;# server_name somename alias another.alias;# location / {# root html;# index index.html index.htm;# }#}# HTTPS server##server {# listen 443 ssl;# server_name localhost;# ssl_certificate cert.pem;# ssl_certificate_key cert.key;# ssl_session_cache shared:SSL:1m;# ssl_session_timeout 5m;# ssl_ciphers HIGH:!aNULL:!MD5;# ssl_prefer_server_ciphers on;# location / {# root html;# index index.html index.htm;# }#}}
轉(zhuǎn)載于:https://www.cnblogs.com/zyw-205520/p/5078003.html
總結(jié)
以上是生活随笔為你收集整理的nginx 配置优化详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: file_get_content和cur
- 下一篇: 数制学习笔记