?
?
# nginx不同于apache服務器,當進行了大量優化設置后會魔術般的明顯性能提升效果
# nginx在安裝完成后,大部分參數就已經是最優化了,我們需要管理的東西并不多#user nobody;#阻塞和非阻塞網絡模型:
#同步阻塞模型,一請求一進(線)程,當進(線)程增加到一定程度后
#更多CPU時間浪費到切換一,性能急劇下降,所以負載率不高
#Nginx基于事件的非阻塞多路復用(epoll或kquene)模型
#一個進程在短時間內可以響應大量的請求
#建議值 <= cpu核心數量,一般高于cpu數量不會帶好處,也許還有進程切換開銷的負面影響
worker_processes 4;#將work process綁定到特定cpu上,避免進程在cpu間切換的開銷
worker_cpu_affinity 0001 0010 0100 1000
#4內核4進程時的設置方法
#8內核4進程時的設置方法 worker_cpu_affinity 00000001 00000010 00000100 10000000# 每進程最大可打開文件描述符數量(linux上文件描述符比較廣義,網絡端口、設備、磁盤文件都是)
# 文件描述符用完了,新的連接會被拒絕,產生502類錯誤
# linux最大可打開文件數可通過ulimit -n FILECNT或 /etc/security/limits.conf配置
# 理論值 系統最大數量 / 進程數。但進程間工作量并不是平均分配的,所以可以設置的大一些
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 {# 并發響應能力的關鍵配置值# 每個進程允許的最大同時連接數,work_connectins * worker_processes = maxConnection;# 要注意maxConnections不等同于可響應的用戶數量,# 因為一般一個瀏覽器會同時開兩條連接,如果反向代理,nginx到后端服務器的連接也要占用連接數# 所以,做靜態服務器時,一般 maxClient = work_connectins * worker_processes / 2# 做反向代理服務器時 maxClient = work_connectins * worker_processes / 4# 這個值理論上越大越好,但最多可承受多少請求與配件和網絡相關,也可最大可打開文件,最大可用sockets數量(約64K)有關worker_connections 500;# 指明使用epoll 或 kquene (*BSD)use epoll# 備注:要達到超高負載下最好的網絡響應能力,還有必要優化與網絡相關的linux內核參數
}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"';# 關閉此項可減少IO開銷,但也無法記錄訪問信息,不利用業務分析,一般運維情況不建議使用access_log off# 只記錄更為嚴重的錯誤日志,可減少IO壓力error_log logs/error.log crit;#access_log logs/access.log main;# 啟用內核復制模式,應該保持開啟達到最快IO效率sendfile on;# 簡單說,啟動如下兩項配置,會在數據包達到一定大小后再發送數據# 這樣會減少網絡通信次數,降低阻塞概率,但也會影響響應及時性# 比較適合于文件下載這類的大數據包通信場景#tcp_nopush on; 在 #tcp_nodelay on|off on禁用Nagle算法 #keepalive_timeout 0;# HTTP1.1支持持久連接alive# 降低每個連接的alive時間可在一定程度上提高可響應連接數量,所以一般可適當降低此值keepalive_timeout 30s;# 啟動內容壓縮,有效降低網絡流量gzip on; # 過短的內容壓縮效果不佳,壓縮過程還會浪費系統資源gzip_min_length 1000;# 可選值1~9,壓縮級別越高壓縮率越高,但對系統性能要求越高gzip_comp_level 4;# 壓縮的內容類別gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;# 靜態文件緩存# 最大緩存數量,文件未使用存活期open_file_cache max=655350 inactive=20s;# 驗證緩存有效期時間間隔open_file_cache_valid 30s;# 有效期內文件最少使用次數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;# }#}}