Fabric框架:重塑AI辅助工作的智能引擎
2026/1/7 4:54:08
Nginx默认配置能跑,但性能发挥不出来。
这篇分享一些生产环境的Nginx优化配置,让你的Nginx能扛住更大的流量。
# 通常设置为CPU核心数 worker_processes auto; # 或者手动指定 worker_processes 8; # 绑定CPU,减少进程切换 worker_cpu_affinity auto;events { # 单个worker的最大连接数 worker_connections 65535; # 使用epoll(Linux) use epoll; # 一次接受多个连接 multi_accept on; }# nginx.conf worker_rlimit_nofile 65535;系统层面也要调整:
# /etc/security/limits.conf * soft nofile 65535 * hard nofile 65535 # /etc/sysctl.conf fs.file-max = 2000000http { # 零拷贝 sendfile on; # 减少网络报文段数量 tcp_nopush on; tcp_nodelay on; # 保持连接 keepalive_timeout 65; keepalive_requests 1000; }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"; }http { # 客户端请求体缓冲区 client_body_buffer_size 16k; client_max_body_size 100m; # 代理缓冲区 proxy_buffer_size 64k; proxy_buffers 8 64k; proxy_busy_buffers_size 128k; }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; } }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; }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; } } }location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ { expires 30d; add_header Cache-Control "public, immutable"; }http { # 定义限制区域 limit_conn_zone $binary_remote_addr zone=conn_limit:10m; server { # 每个IP最多100个连接 limit_conn conn_limit 100; } }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; } } }http { server_tokens off; }add_header X-Frame-Options "SAMEORIGIN"; add_header X-Content-Type-Options "nosniff"; add_header X-XSS-Protection "1; mode=block";if ($request_method !~ ^(GET|POST|PUT|DELETE)$) { return 405; }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| 指标 | 说明 |
|---|---|
| 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服务器在机房,怎么在本地修改配置和调试?
我用星空组网工具把本地和服务器连起来,直接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 |
优化完记得压测验证效果,别凭感觉。