Nginx性能优化与监控
一、核心优化方向worker 进程配置worker_processes auto;自动匹配 CPU 核心数worker_connections 10240;单进程最大连接数需配合系统 ulimitworker_rlimit_nofile 65535;提升最大文件句柄数事件模型优化nginxevents { use epoll; multi_accept on; accept_mutex off; }TCP / 网络优化nginxhttp { sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; keepalive_requests 1000; }压缩优化nginxgzip on; gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xmlrss text/javascript; gzip_min_length 1k;静态资源缓存nginxlocation ~* \.(jpg|jpeg|png|gif|ico|css|js)$ { expires 30d; add_header Cache-Control public, max-age2592000; }隐藏版本、提升安全nginxserver_tokens off;二、系统层面优化配合 Nginx修改/etc/security/limits.confplaintextnginx soft nofile 65535 nginx hard nofile 65535内核参数/etc/sysctl.confplaintextnet.core.somaxconn 65535 net.core.netdev_max_backlog 65535 net.ipv4.tcp_syncookies 1 net.ipv4.tcp_tw_reuse 1 net.ipv4.tcp_fin_timeout 30执行sysctl -p三、监控指标与工具1. 关键监控指标活跃连接数、等待连接数请求 QPS、响应时间状态码2xx/3xx/4xx/5xx 占比CPU、内存、句柄、磁盘 I/Oupstream 后端健康状态2. 内置状态页nginxlocation /nginx_status { stub_status; allow 127.0.0.1; deny all; }访问http://ip/nginx_status3. 常用监控工具nginx -t配置语法检查nginx -s reload平滑重载netstat -anp | grep nginx/ss -stop、htop、iostat、dstat日志分析awk、goaccess接入 Prometheus Grafananginx-vts-exporter四、日志与排错访问日志记录请求、状态、耗时、UA、来源错误日志定位 upstream、权限、配置、端口冲突常用排查502后端服务挂 / 超时 / 不可达504超时403权限 / 目录索引 / IP 限制404路径 / 路由 / 文件不存在
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2424115.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!