Nginx日志中的HTTP状态码和错误信息是排查服务问题的关键线索。以下是常见错误代码的解析及解决方案框架:
client_max_body_size
限制)nginx
http {
client_max_body_size 20M; # 调整上传大小限制
client_header_buffer_size 4k; # 增大Header缓冲区
}
bash
chmod -R 755 /var/www/html; # 修正目录权限
setsebool -P httpd_read_user_content 1; # SELinux环境调整
nginx
location / {
allow 192.168.1.0/24; # 检查IP白名单规则
deny all;
}
root/alias
配置错误、重写规则问题nginx
location /static/ {
alias /data/files/; # 确保路径末尾带/
# 或使用 root
}
nginx
proxy_ignore_client_abort on; # 代理场景下继续处理请求
fastcgi_read_timeout 300; # 调整后端超时时间
nginx
location / {
proxy_pass http://backend;
proxy_connect_timeout 60; # 调整超时参数
proxy_read_timeout 60;
}
bash
systemctl restart php-fpm # 重启后端服务
nginx
limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;
location / {
limit_req zone=one burst=20; # 限流配置
}
nginx
proxy_read_timeout 120; # 默认60秒
fastcgi_send_timeout 300; # FastCGI参数调整
SSL_do_handshake()
failednginx
ssl_protocols TLSv1.2 TLSv1.3;
ssl_certificate /path/to/fullchain.pem; # 包含中间证书
too many open files
bash
ulimit -n 65535; # 临时生效
# 永久生效需修改 /etc/security/limits.conf
bash
awk '{print $9}' access.log | sort | uniq -c # 统计状态码频次
tail -f /var/log/nginx/error.log # 实时监控错误日志
bash
ab -n 1000 -c 100 http://example.com/
通过系统化分析状态码,可快速定位问题层次(客户端/服务端/网络)。建议结合监控工具(如Prometheus)实现预警。