499状态码表示客户端在服务器处理请求前关闭了连接,通常与以下配置不当有关: 1. 上游服务器响应时间过长 2. Nginx代理超时设置不合理 3. 负载均衡或故障转移(failover)机制配置错误
http {
proxy_connect_timeout 60s; # 与上游服务器建立连接的超时时间
proxy_send_timeout 60s; # 发送请求到上游服务器的超时时间
proxy_read_timeout 60s; # 从上游服务器读取响应的超时时间
keepalive_timeout 75s; # 保持连接的超时时间
# 针对特定location的配置
server {
location / {
proxy_pass http://backend;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_next_upstream_timeout 10s; # 切换到下一个上游服务器前的超时
proxy_next_upstream_tries 3; # 尝试切换上游服务器的次数
}
}
}
upstream backend {
# 配置多个后端服务器
server backend1.example.com max_fails=3 fail_timeout=30s;
server backend2.example.com max_fails=3 fail_timeout=30s;
server backend3.example.com max_fails=3 fail_timeout=30s backup; # 备份服务器
# 负载均衡策略
least_conn; # 最少连接数策略
# 健康检查配置
check interval=3000 rise=2 fall=3 timeout=1000;
}
server {
# 处理客户端过早关闭连接的情况
reset_timedout_connection on;
# 客户端请求体读取超时
client_body_timeout 30s;
# 客户端请求头读取超时
client_header_timeout 30s;
# 发送响应到客户端的超时
send_timeout 30s;
}
http {
log_format upstream_time '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'rt=$request_time uct="$upstream_connect_time" '
'uht="$upstream_header_time" urt="$upstream_response_time"';
access_log /var/log/nginx/access.log upstream_time;
}
ab
或wrk
进行压力测试error.log
)和访问日志curl -v
观察请求/响应全过程通过以上配置调整,应该能够有效减少499错误并确保failover机制正常工作。