分佈式架構 Nginx優化
Nginx介紹
- Nginx默認配置路徑 /conf/nginx.conf文件,可在啓動時 通過-c 指定安裝路徑
- Nginx啓動原理
1.啓動nginx會啓動一個Master進程,這個進程不處理任何客戶端請求,主要用來產生worker進程,一個worker進程用來處理一個request
2.單獨worker之間互不影響,worker_connections可以配置,指每個進程裏面可以啓動多少個worker
優化思路
- 默認Nginx是經過優化的,而我們針對Nginx優化主要集中在配置調整上
- 如果優化後效果不理想,增加硬件,如機器,負載均衡
常見配置文件如下
- nginx.conf 應用程序基本配置 /核心配置
- mime.types MIME關聯擴展文件
- fastcgi.conf 與fastcgi相關配置
- proxy.conf 與proxy相關配置
- sites.conf 配置Nginx提供的網站,包括虛擬主機
nginx.conf配置講解,常用核心模塊指令可以參考如下
- http://www.howtocn.org/nginx:directiveindex 純中文
- http://nginx.org/en/docs/ 官網
nginx.conf配置
error_log logs/error.log crit
user root;
worker_processes 2;
worker_rlimit_nofile 15360;
events {
use epoll;
worker_connections 10240/20480;
}
//web反向代理
http {
include mime.types;//引入其他文件
include proxy.conf;//引入代理文件
default_type application/octet-stream;
access_log logs/access.log main
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;
//負載均衡配置節點
upstream zachary.cn{
#server 127.0.0.1:9080 weight=5
server 127.0.0.1 weight=5
#server 127.0.0.1:1010
}
server{
listen 80;
server_name zachary.cn;
index index.php index.htm;
root /www/html/; 靜態資源路徑
location ~ .*/.(gif|jpg|jpeg|png|bmp|swf|ico)${
if(-f $request_filename){
#expires 30d;
break;
}
}
location ~ .*/.(html|htm|js|css)${
#expires 1d;
}
location /{
proxy_set_header Host $host;
proxy_set_header X-Porwarded-Por $remote_addr //獲取真實ip
proxy_pass http://zachary.cn
proxy_next_upstream [error|timeout|invalid_header|http_500|http_502|http_503|http_504|http_404|off] //默認 error timeout
}
location ~ .*/.(php|php5)?${
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
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;
}
error_page 500 502 503 504 /501.html;
location = /501.html{
internal;
root errors/html
}
}
Nginx.conf 講解
處理過程:
- 請求進來worker會監控80端口,過濾靜態資源後,請求反向代理到後臺服務器(如tomcat)後臺服務器可以負載均衡配置多臺,可通過權重如weight、iphash 指定服務器接收請求策略。
- 靜態資源不代理到後臺服務器 直接可以到專有靜態資源服務器、分佈式共享存儲獲取 如NFS\MFS
- nginx單點的HA高可用:proxy_next_upstream可配置(如果指向服務器 此時服務器1處於不正常,它會重新把請求指向下臺服務器依次類推
詳細講解
- 併發總數是 worker_processes *worker_connections ,反向代理下要除以4
- events裏面的I/O模型,Linux推薦使用epoll模型,FreeBSD推薦採用kqueue
- worker_processes建議配置服務器CPU總核數,或者2倍,性能會更好
- worker_rlimit_nofile打開最大文件數,配置Linux內核下文件打開數一致,可以通過ulimit -n查看,默認1024,可以修改/etc/security/limits.conf 增加到65535
- worker_connections 單個進程允許的客戶端最大連接數
Nginx配置二級域名
server_name www.zachary.cn *.zachary.cn;
判斷是二級域名,可自定義變量設置
if($host ~* (\b(?!www\b)\w+)\.\w+\.com){
set $zach1 $1;
set $zachurl $request_uri;
}
rewrite可以使用自定義變量
Nginx 配置HTTP主要有以下幾塊
http{ //協議級別
server{ //服務器級別
location/ {//請求級別
}
}
}
location區段通過指定模式來和客戶端請求URL進行匹配,基本語法
location [=|~|~*|^~|@] pattern{.....}
server{
server_name zachary.cn
location /cc56{
......
}
}
區分大小寫 如下匹配
http://zachary.cn/cc56
http://zachary.cn/cc56/
http://zachary.cn/cc56cincnb2
http://zachary.cn/cc56/?cccc=1&bbbb=2
server{
server_name zachary.cn
location ~* ^/cc56{
......
}
}
不區分大小寫 如下匹配
http://zachary.cn/cc56
http://zachary.cn/CC56
http://zachary.cn/cc56/?cccc=1&bbbb=2
HTTP反向代理
常用proxy_pass, 如 location /{proxy_pass http://zachary.cn}
Nginx負載均衡
流行負載均衡 如(DNS輪詢、硬件F5、軟件LVS、Nginx)
Nginx通過upstream
upstream zachary.cn{
ip_hash
#server 127.0.0.1:9080 weight=5
server 127.0.0.1 weight=5
#server 127.0.0.1:1010
}
默認weight=1,不推薦使用ip_hash,客戶端ip會變化,如動態ip、翻牆、代理
Eewrite模塊,用來指定URL重定向,這個機制可以處理惡意訪問url
Nginx兼容http/https
場景:有些網站或者系統想提高安全性,故從http升級至https,處理過程如下:
- 需要有域名
- 申請ssl認證證書,阿里雲、騰訊雲免費申請證書(有效期一年)如:https://console.cloud.tencent.com/ssl
- nginx配置ssl證書(下面申請的證書文件,其中包括pem和key的文件)
Nginx配置過程如下:
server {
listen 443 ssl;
server_name zachary.cn;
ssl_certificate cert/ce.pem;
ssl_certificate_key cert/ce.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 6m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root /usr/local/nginx/html;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#http兼容https
server {
listen 80;
server_name zachary.cn;
rewrite ^(.*)$ https://$host$1 permanent;
}
優化過程
- 優化併發總數 worker_processes *worker_connections,worker_processes參考CPU核數,worker_connections每個進程允許連接數,1GB內存可以打開10W文件數
- keepalive_timeout 建議設置60-70左右
- client_header_buffer_size,建議設置請求緩存
- gzip_comp_level 開啓Gzip壓縮,建議設置3-5,高了耗損CPU
- access日誌優化,如果使用其它統計工具,可關閉,減少磁盤寫入,提高I/O效率
- sendfile 指定是否調用sendfile函數來輸出文件,建議設置on
- buffersize優化,如果太小會導致nginx使用臨時文件存儲response,client_body_buffer_size 處理客戶端請求體buffer大小,處理post請求數據、上傳文件等
- tcp_nopush 建議開啓提高吞吐量
- tcp_nodelay 不緩存數據,一段一段發送,建議開啓
- client_header_timeout 設置請求頭超時時間,建議10-20s
- client_body_timeout 設置請求體超時時間,建議10-20s
- reset_timeout_connection 關閉不響應客戶端連接,建議開啓
- send_timeout 客戶端響應超時時間 默認60s,建議設置10s
總結
Nginx根據各系統的結構進行特定優化,優化順序 (先優化單臺配置、單臺到達瓶頸後可通過硬件引流到多臺機器)故達到高可用狀態,參數調優可參考官方文檔仔細斟酌測試
作者簡介:張程 技術研究
更多文章請關注微信公衆號:zachary分解獅 (frankly0423)