Nginx高并发配置实战百万级并发优化
作者:花宝宝hua
本文主要介绍了Nginx高并发配置实战百万级并发优化,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
Nginx默认配置能跑,但性能发挥不出来。
这篇分享一些生产环境的Nginx优化配置,让你的Nginx能扛住更大的流量。
一、基础配置优化
1.1 worker进程数
# 通常设置为CPU核心数 worker_processes auto; # 或者手动指定 worker_processes 8; # 绑定CPU,减少进程切换 worker_cpu_affinity auto;
1.2 worker连接数
events {
# 单个worker的最大连接数
worker_connections 65535;
# 使用epoll(Linux)
use epoll;
# 一次接受多个连接
multi_accept on;
}
1.3 文件描述符限制
# nginx.conf worker_rlimit_nofile 65535;
系统层面也要调整:
# /etc/security/limits.conf * soft nofile 65535 * hard nofile 65535 # /etc/sysctl.conf fs.file-max = 2000000
二、HTTP优化
2.1 开启高效传输
http {
# 零拷贝
sendfile on;
# 减少网络报文段数量
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 缓冲区优化
http {
# 客户端请求体缓冲区
client_body_buffer_size 16k;
client_max_body_size 100m;
# 代理缓冲区
proxy_buffer_size 64k;
proxy_buffers 8 64k;
proxy_busy_buffers_size 128k;
}
三、反向代理优化
3.1 上游服务器配置
upstream backend {
# 长连接
keepalive 100;
keepalive_requests 1000;
keepalive_timeout 60s;
# 负载均衡
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 "";
# 超时设置
proxy_connect_timeout 10s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
# 转发真实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 负载均衡策略
upstream backend {
# 轮询(默认)
server 192.168.1.100:8080;
server 192.168.1.101:8080;
# 加权轮询
# server 192.168.1.100:8080 weight=5;
# server 192.168.1.101:8080 weight=3;
# IP Hash(会话保持)
# 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;
# 缓存状态头
add_header X-Cache-Status $upstream_cache_status;
}
}
}
4.2 静态文件缓存
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
五、限流配置
5.1 连接数限制
http {
# 定义限制区域
limit_conn_zone $binary_remote_addr zone=conn_limit:10m;
server {
# 每个IP最多100个连接
limit_conn conn_limit 100;
}
}
5.2 请求速率限制
http {
# 定义限制区域:每秒10个请求
limit_req_zone $binary_remote_addr zone=req_limit:10m rate=10r/s;
server {
location /api/ {
# 允许突发20个请求,不延迟
limit_req zone=req_limit burst=20 nodelay;
}
}
}
六、安全配置
6.1 隐藏版本号
http {
server_tokens off;
}
6.2 防止点击劫持
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;
}
七、监控指标
7.1 开启状态页
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 关键指标
| 指标 | 说明 |
|---|---|
| Active connections | 当前活跃连接数 |
| accepts | 接受的连接总数 |
| handled | 处理的连接总数 |
| requests | 处理的请求总数 |
| Reading | 正在读取请求的连接 |
| Writing | 正在响应的连接 |
| 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;
# 性能优化
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;
# 上游服务
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;
}
}
}
九、远程管理Nginx
Nginx服务器在机房,怎么在本地修改配置和调试?
我用星空组网工具把本地和服务器连起来,直接SSH上去改配置:
ssh root@192.168.188.10 vim /etc/nginx/nginx.conf nginx -t && nginx -s reload
也可以用VSCode Remote SSH,直接在本地编辑服务器上的配置文件,改完保存就生效。比跳板机方便多了。
总结
Nginx优化核心:
| 方向 | 措施 |
|---|---|
| 进程优化 | worker_processes、worker_connections |
| 传输优化 | sendfile、tcp_nopush、gzip |
| 连接优化 | keepalive、长连接 |
| 缓存优化 | proxy_cache、静态文件缓存 |
| 限流保护 | limit_req、limit_conn |
优化完记得压测验证效果,别凭感觉。
到此这篇关于Nginx高并发配置实战百万级并发优化的文章就介绍到这了,更多相关Nginx 百万级并发优化内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
