2026/4/6 5:23:49
网站建设
项目流程
广州正规网站建设企业,深圳网络seo优化,cms wordpress,专业店面店铺装修设计Nginx默认配置能跑#xff0c;但性能发挥不出来。
这篇分享一些生产环境的Nginx优化配置#xff0c;让你的Nginx能扛住更大的流量。 一、基础配置优化
1.1 worker进程数
# 通常设置为CPU核心数
worker_processes auto;# 或者手动指定
worker_processes 8;# 绑定CPU#x…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; # 使用epollLinux 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 weight5; server 192.168.1.101:8080 weight3; server 192.168.1.102:8080 backup; # 健康检查需要第三方模块 # check interval3000 rise2 fall3 timeout1000; } 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 weight5; # server 192.168.1.101:8080 weight3; # IP Hash会话保持 # ip_hash; # 最少连接 # least_conn; # 一致性Hash # hash $request_uri consistent; }四、缓存配置4.1 代理缓存http { # 定义缓存路径 proxy_cache_path /var/cache/nginx levels1:2 keys_zonemy_cache:100m max_size10g inactive60m; 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 zoneconn_limit:10m; server { # 每个IP最多100个连接 limit_conn conn_limit 100; } }5.2 请求速率限制http { # 定义限制区域每秒10个请求 limit_req_zone $binary_remote_addr zonereq_limit:10m rate10r/s; server { location /api/ { # 允许突发20个请求不延迟 limit_req zonereq_limit burst20 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; modeblock;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_statusActive connections: 291 server accepts handled requests 16630948 16630948 31070465 Reading: 6 Writing: 179 Waiting: 1067.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 zonereq_limit:10m rate10r/s; limit_conn_zone $binary_remote_addr zoneconn_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 zonereq_limit burst20 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; } } }九、远程管理NginxNginx服务器在机房怎么在本地修改配置和调试我用星空组网工具把本地和服务器连起来直接SSH上去改配置ssh root192.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优化完记得压测验证效果别凭感觉。