Nginx 日志中的 HTTP 状态码(错误码)是服务器对客户端请求的响应标识,以下是常见状态码及其含义的详细分类和解决方案指南:
400 Bad Request
401 Unauthorized
Authorization
头或登录状态。403 Forbidden
chmod 644 /path/to/file
allow/deny
规则。404 Not Found
location
配置或文件路径。try_files
指令处理静态资源:nginx
location / {
try_files $uri $uri/ /index.html;
}
413 Payload Too Large
client_max_body_size
:http {
client_max_body_size 20M; # 默认 1M
}
429 Too Many Requests
500 Internal Server Error
error_log
)。nginx
location ~ \.php$ {
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
include fastcgi_params;
}
502 Bad Gateway
systemctl status php-fpm
proxy_pass
地址是否正确。503 Service Unavailable
504 Gateway Timeout
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
499 Client Closed Request
494 Request Header Too Large
large_client_header_buffers
限制。http {
large_client_header_buffers 4 16k; # 默认 8k
}
bash
tail -f /var/log/nginx/error.log | grep -E "50[0-9]|40[0-9]"
bash
awk '{print $9}' access.log | sort | uniq -c | sort -nr
通过状态码快速定位问题类型(客户端/服务端),结合 Nginx 配置和日志分析,可高效解决大多数错误。