Nginx高并發(fā)配置實(shí)戰(zhàn)百萬級并發(fā)優(yōu)化
Nginx默認(rèn)配置能跑,但性能發(fā)揮不出來。
這篇分享一些生產(chǎn)環(huán)境的Nginx優(yōu)化配置,讓你的Nginx能扛住更大的流量。
一、基礎(chǔ)配置優(yōu)化
1.1 worker進(jìn)程數(shù)
# 通常設(shè)置為CPU核心數(shù) worker_processes auto; # 或者手動(dòng)指定 worker_processes 8; # 綁定CPU,減少進(jìn)程切換 worker_cpu_affinity auto;
1.2 worker連接數(shù)
events {
# 單個(gè)worker的最大連接數(shù)
worker_connections 65535;
# 使用epoll(Linux)
use epoll;
# 一次接受多個(gè)連接
multi_accept on;
}
1.3 文件描述符限制
# nginx.conf worker_rlimit_nofile 65535;
系統(tǒng)層面也要調(diào)整:
# /etc/security/limits.conf * soft nofile 65535 * hard nofile 65535 # /etc/sysctl.conf fs.file-max = 2000000
二、HTTP優(yōu)化
2.1 開啟高效傳輸
http {
# 零拷貝
sendfile on;
# 減少網(wǎng)絡(luò)報(bào)文段數(shù)量
tcp_nopush on;
tcp_nodelay on;
# 保持連接
keepalive_timeout 65;
keepalive_requests 1000;
}
2.2 Gzip壓縮
http {
gzip on;
gzip_min_length 1024;
gzip_comp_level 4;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml;
gzip_vary on;
# 對已壓縮文件不再壓縮
gzip_disable "msie6";
}
2.3 緩沖區(qū)優(yōu)化
http {
# 客戶端請求體緩沖區(qū)
client_body_buffer_size 16k;
client_max_body_size 100m;
# 代理緩沖區(qū)
proxy_buffer_size 64k;
proxy_buffers 8 64k;
proxy_busy_buffers_size 128k;
}
三、反向代理優(yōu)化
3.1 上游服務(wù)器配置
upstream backend {
# 長連接
keepalive 100;
keepalive_requests 1000;
keepalive_timeout 60s;
# 負(fù)載均衡
server 192.168.1.100:8080 weight=5;
server 192.168.1.101:8080 weight=3;
server 192.168.1.102:8080 backup;
# 健康檢查(需要第三方模塊)
# check interval=3000 rise=2 fall=3 timeout=1000;
}
server {
location /api/ {
proxy_pass http://backend;
# 使用HTTP/1.1支持長連接
proxy_http_version 1.1;
proxy_set_header Connection "";
# 超時(shí)設(shè)置
proxy_connect_timeout 10s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
# 轉(zhuǎn)發(fā)真實(shí)IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
3.2 負(fù)載均衡策略
upstream backend {
# 輪詢(默認(rèn))
server 192.168.1.100:8080;
server 192.168.1.101:8080;
# 加權(quán)輪詢
# server 192.168.1.100:8080 weight=5;
# server 192.168.1.101:8080 weight=3;
# IP Hash(會(huì)話保持)
# ip_hash;
# 最少連接
# least_conn;
# 一致性Hash
# hash $request_uri consistent;
}
四、緩存配置
4.1 代理緩存
http {
# 定義緩存路徑
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:100m max_size=10g inactive=60m;
server {
location /static/ {
proxy_pass http://backend;
# 啟用緩存
proxy_cache my_cache;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
proxy_cache_key $uri$is_args$args;
# 緩存狀態(tài)頭
add_header X-Cache-Status $upstream_cache_status;
}
}
}
4.2 靜態(tài)文件緩存
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
五、限流配置
5.1 連接數(shù)限制
http {
# 定義限制區(qū)域
limit_conn_zone $binary_remote_addr zone=conn_limit:10m;
server {
# 每個(gè)IP最多100個(gè)連接
limit_conn conn_limit 100;
}
}
5.2 請求速率限制
http {
# 定義限制區(qū)域:每秒10個(gè)請求
limit_req_zone $binary_remote_addr zone=req_limit:10m rate=10r/s;
server {
location /api/ {
# 允許突發(fā)20個(gè)請求,不延遲
limit_req zone=req_limit burst=20 nodelay;
}
}
}
六、安全配置
6.1 隱藏版本號
http {
server_tokens off;
}
6.2 防止點(diǎn)擊劫持
add_header X-Frame-Options "SAMEORIGIN"; add_header X-Content-Type-Options "nosniff"; add_header X-XSS-Protection "1; mode=block";
6.3 限制請求方法
if ($request_method !~ ^(GET|POST|PUT|DELETE)$) {
return 405;
}
七、監(jiān)控指標(biāo)
7.1 開啟狀態(tài)頁
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
allow 192.168.0.0/16;
deny all;
}
訪問 /nginx_status:
Active connections: 291 server accepts handled requests 16630948 16630948 31070465 Reading: 6 Writing: 179 Waiting: 106
7.2 關(guān)鍵指標(biāo)
| 指標(biāo) | 說明 |
|---|---|
| Active connections | 當(dāng)前活躍連接數(shù) |
| accepts | 接受的連接總數(shù) |
| handled | 處理的連接總數(shù) |
| requests | 處理的請求總數(shù) |
| Reading | 正在讀取請求的連接 |
| Writing | 正在響應(yīng)的連接 |
| Waiting | 等待請求的空閑連接 |
八、完整配置示例
user nginx;
worker_processes auto;
worker_rlimit_nofile 65535;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 65535;
use epoll;
multi_accept on;
}
http {
include /etc/nginx/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" '
'$request_time $upstream_response_time';
access_log /var/log/nginx/access.log main;
# 性能優(yōu)化
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
# Gzip
gzip on;
gzip_min_length 1024;
gzip_comp_level 4;
gzip_types text/plain text/css application/json application/javascript;
# 限流
limit_req_zone $binary_remote_addr zone=req_limit:10m rate=10r/s;
limit_conn_zone $binary_remote_addr zone=conn_limit:10m;
# 上游服務(wù)
upstream backend {
keepalive 100;
server 192.168.1.100:8080;
server 192.168.1.101:8080;
}
server {
listen 80;
server_name example.com;
# 限流
limit_req zone=req_limit burst=20 nodelay;
limit_conn conn_limit 100;
location / {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /nginx_status {
stub_status on;
allow 127.0.0.1;
deny all;
}
}
}
九、遠(yuǎn)程管理Nginx
Nginx服務(wù)器在機(jī)房,怎么在本地修改配置和調(diào)試?
我用星空組網(wǎng)工具把本地和服務(wù)器連起來,直接SSH上去改配置:
ssh root@192.168.188.10 vim /etc/nginx/nginx.conf nginx -t && nginx -s reload
也可以用VSCode Remote SSH,直接在本地編輯服務(wù)器上的配置文件,改完保存就生效。比跳板機(jī)方便多了。
總結(jié)
Nginx優(yōu)化核心:
| 方向 | 措施 |
|---|---|
| 進(jìn)程優(yōu)化 | worker_processes、worker_connections |
| 傳輸優(yōu)化 | sendfile、tcp_nopush、gzip |
| 連接優(yōu)化 | keepalive、長連接 |
| 緩存優(yōu)化 | proxy_cache、靜態(tài)文件緩存 |
| 限流保護(hù) | limit_req、limit_conn |
優(yōu)化完記得壓測驗(yàn)證效果,別憑感覺。
到此這篇關(guān)于Nginx高并發(fā)配置實(shí)戰(zhàn)百萬級并發(fā)優(yōu)化的文章就介紹到這了,更多相關(guān)Nginx 百萬級并發(fā)優(yōu)化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nginx如何設(shè)置域名轉(zhuǎn)發(fā)到服務(wù)器指定的端口
這篇文章主要介紹了Nginx如何設(shè)置域名轉(zhuǎn)發(fā)到服務(wù)器指定的端口,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-01-01
Nginx + Tomcat實(shí)現(xiàn)請求動(dòng)態(tài)數(shù)據(jù)和請求靜態(tài)資源的分離詳解
這篇文章主要給大家介紹了關(guān)于Nginx + Tomcat實(shí)現(xiàn)請求動(dòng)態(tài)數(shù)據(jù)和請求靜態(tài)資源的分離的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
Nginx 設(shè)置域名轉(zhuǎn)發(fā)到指定端口的實(shí)現(xiàn)方法
這篇文章主要介紹了Nginx 設(shè)置域名轉(zhuǎn)發(fā)到指定端口的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Nginx解決Access-Control-Allow-Origin跨域問題完全指南
跨域問題是由于瀏覽器的同源策略(Same-Origin Policy)限制導(dǎo)致的,本文給大家介紹Nginx解決Access-Control-Allow-Origin跨域問題完全指南,感興趣的朋友跟隨小編一起看看吧2026-03-03

