以下文章来源Java架构师技术,回复”Spring“获惊喜礼包责编:架构君,来源:运维派(ID:yunweipai)
大家好,我是Java架构师
在处理高并发场景时,很多运维同学会遇到这些痛点:
并发量一上来就502/504
CPU使用率飙升,但吞吐量上不去
连接数达到瓶颈,新请求被拒绝
响应时间越来越慢,用户体验极差
这些问题的根源往往不是硬件性能,而是配置和调优不到位。让我们从基础配置开始,一步步打造能扛住高并发的Nginx。
worker_processes auto;
worker_connections 65535;
worker_priority -10;
worker_cpu_affinity auto;
worker_rlimit_nofile 100000;
实战经验:worker_connections 不是越大越好,需要考虑内存消耗。每个连接大约消耗232-248字节内存。
events { useepoll; multi_accepton;
worker_connections65535; accept_mutexoff;}
http { sendfileon; tcp_nopushon; tcp_nodelayon; keepalive_timeout60; keepalive_requests10000; client_header_timeout10; client_body_timeout10; send_timeout10; client_header_buffer_size4k; large_client_header_buffers88k; client_body_buffer_size128k; client_max_body_size50m; gzipon; gzip_varyon; gzip_min_length1000; gzip_comp_level6; gzip_types text/plain text/css text/javascript application/javascript application/json application/xml; server_tokensoff;}
http { upstream backend { keepalive300; keepalive_requests10000; keepalive_timeout60s; server192.168.1.100:8080 weight=3 max_fails=3 fail_timeout=30s; server192.168.1.101:8080 weight=2 max_fails=3 fail_timeout=30s; } open_file_cache max=100000 inactive=20s; open_file_cache_valid30s; open_file_cache_min_uses2; open_file_cache_errorson; fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=fcgi:100m inactive=60m; fastcgi_cache_key"$scheme$request_method$host$request_uri"; fastcgi_cache_use_staleerror timeout invalid_header http_500;}
http { limit_req_zone$binary_remote_addr zone=api:10m rate=100r/s; limit_req_zone$binary_remote_addr zone=login:10m rate=5r/s; limit_conn_zone$binary_remote_addr zone=perip:10m; limit_conn_zone$server_name zone=perserver:10m; server { limit_req zone=api burst=200 nodelay; limit_conn perip 20; limit_conn perserver 2000; location~* \.(jpg|jpeg|png|gif|ico|css|js)$ { expires1y; add_header Cache-Control "public, immutable"; } location /api/ { limit_req zone=api burst=50 nodelay; proxy_pass http://backend; proxy_http_version1.1; proxy_set_header Connection ""; proxy_connect_timeout5s; proxy_send_timeout10s; proxy_read_timeout10s; } }}
高并发场景下,操作系统层面的优化同样重要。以下是经过实战验证的内核参数配置:
cat >> /etc/sysctl.conf << EOF
net.core.somaxconn = 65535net.core.netdev_max_backlog = 30000net.core.rmem_default = 262144net.core.rmem_max = 16777216net.core.wmem_default = 262144net.core.wmem_max = 16777216
net.ipv4.tcp_max_syn_backlog = 65535net.ipv4.tcp_syncookies = 1net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_fin_timeout = 15net.ipv4.tcp_keepalive_time = 600net.ipv4.tcp_keepalive_probes = 3net.ipv4.tcp_keepalive_intvl = 15
net.netfilter.nf_conntrack_max = 1048576net.nf_conntrack_max = 1048576
fs.file-max = 1048576
EOF
sysctl -p
# 编辑 /etc/security/limits.confcat >> /etc/security/limits.conf << EOF
# 用户进程限制* soft nofile 1048576* hard nofile 1048576* soft nproc 1048576* hard nproc 1048576
# nginx用户特殊配置nginx soft nofile 1048576nginx hard nofile 1048576
EOF
#!/bin/bash
echo"=== Nginx连接状态 ==="ss -s
echo"=== 活跃连接数 ==="netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
echo"=== Nginx进程状态 ==="ps aux | grep nginx | grep -v grep
echo"=== 系统负载 ==="uptime
echo"=== 内存使用情况 ==="free -h
echo"=== Nginx访问统计 ==="curl -s http://localhost/nginx_status
wrk -t20 -c1000 -d60s --latency http://your-domain.com/
ab -n 100000 -c 1000 http://your-domain.com/
top -p $(pgrep nginx | tr '\n' ',' | sed 's/,$//')
location / { try_files$uri@proxy;}
location@proxy { proxy_cache my_cache; proxy_cache_valid2003021h; proxy_cache_valid4041m; proxy_pass http://backend; proxy_cache_lockon; proxy_cache_lock_timeout5s;}
upstream backend { hash $remote_addr consistent; server 192.168.1.100:8080 weight=3 max_fails=3 fail_timeout=30s; server 192.168.1.101:8080 weight=3 max_fails=3 fail_timeout=30s; server 192.168.1.102:8080 weight=2 max_fails=3 fail_timeout=30s backup;}
1、502 Bad Gateway
检查upstream服务状态
调整proxy_connect_timeout
增加worker_processes
2、连接数不够
调整worker_connections
检查系统文件描述符限制
优化keepalive配置
3、内存占用过高
通过本文的全面优化,你的Nginx服务器应该能够:
支持10万+并发连接
响应时间控制在100ms以内
CPU使用率保持在合理范围
内存使用更加高效
记住,性能优化是一个持续的过程,需要根据实际业务场景不断调整。建议在生产环境应用前,先在测试环境进行充分验证。
你还有什么想要补充的吗?
欢迎有需要的同学试试,如果本文对您有帮助,也请帮忙点个 赞 + 在看 啦!❤️
在 GitHub猿 还有更多优质项目系统学习资源,欢迎分享给其他同学吧!
最后,再次推荐下我们的AI星球:
为了跟上AI时代我干了一件事儿,我创建了一个知识星球社群:ChartGPT与副业。想带着大家一起探索ChatGPT和新的AI时代。
有很多小伙伴搞不定ChatGPT账号,于是我们决定,凡是这四天之内加入ChatPGT的小伙伴,我们直接送一个正常可用的永久ChatGPT独立账户。
不光是增长速度最快,我们的星球品质也绝对经得起考验,短短一个月时间,我们的课程团队发布了8个专栏、18个副业项目:

简单说下这个星球能给大家提供什么:
1、不断分享如何使用ChatGPT来完成各种任务,让你更高效地使用ChatGPT,以及副业思考、变现思路、创业案例、落地案例分享。
2、分享ChatGPT的使用方法、最新资讯、商业价值。
3、探讨未来关于ChatGPT的机遇,共同成长。
4、帮助大家解决ChatGPT遇到的问题。
5、提供一整年的售后服务,一起搞副业
星球福利:
1、加入星球4天后,就送ChatGPT独立账号。
2、邀请你加入ChatGPT会员交流群。
3、赠送一份完整的ChatGPT手册和66个ChatGPT副业赚钱手册。
其它福利还在筹划中... 不过,我给你大家保证,加入星球后,收获的价值会远远大于今天加入的门票费用 !
本星球第一期原价399,目前属于试运营,早鸟价169,每超过50人涨价10元,星球马上要来一波大的涨价,如果你还在犹豫,可能最后就要以更高价格加入了。。
早就是优势。建议大家尽早以便宜的价格加入!
最后,整理了500多套项目,赠送读者。扫码下方二维码,后台回复【赚钱】即可获取。
--END--
版权申明:内容来源网络,版权归原创者所有。除非无法确认,我们都会标明作者及出处,如有侵权烦请告知,我们会立即删除并表示歉意。谢谢!