首页 > 解决方案 > NGINX - HTTP 500 上游过早关闭连接,同时从上游读取响应标头

问题描述

我们在 docker 容器中有一个 Apache 服务器,在另一个容器中有 NGINX 作为代理服务器。有时我的网站会停止响应 HTTP 500 或 HTTP 502 错误。当我检查 NGINX 的错误日志时,我收到以下错误:

*upstream prematurely closed connection while reading response header from upstream*

经过一些研究,我发现很多人通过增加 time_out 来解决它,但这里不应该是这种情况,因为我在从我的网站发送一些申请后立即收到此错误。我也不认为这应该是资源问题,因为在发送请求后我检查了消耗,它并没有显着增加。我使用以下命令来检查它:

docker stats

htop

我们还验证了 NGINX 的问题,将请求直接发送到我的网站时,我没有遇到任何错误。

我的 NGINX 配置是:

server_tokens off;
#client_max_body_size 1G;
#client_body_buffer_size 80M;
proxy_cache_path /tmp/cache keys_zone=one:10m;
#proxy_request_buffering off;
#proxy_buffering off;

upstream mySite
{
    server 10.20.40.1:443;
    #server 10.20.40.2:443;
    #server 10.20.40.3:443;
}    

log_format name '[$time_local] src_ip="$remote_addr" X-Forwarded-For="$http_x_forwarded_for"                     
http_hostname="$host" connection="$sent_http_connection" remote_user="$remote_user"                         
request="$request" http_status="$status" bytes_sent="$body_bytes_sent" 
content_length="$http_content_length" referer="$http_referer" user_agent="$http_user_agent" 
session_id="$cookie_PHPSESSID" http_method="$request_method" connection_serial="$connection" 
request_time="$request_time" proxy_host="$proxy_host" upstream_addr="$upstream_addr" 
upstream_status="$upstream_status" upstream_cache_status="$upstream_cache_status" 
upstream_response_time="$upstream_response_time"';

server
{
    listen 80;
    listen 443 ssl reuseport;
    listen [::]:80;
    listen [::]:443 ssl;

    proxy_cache one;

    #access_log /var/log/nginx/lbmySite-access.log name;
    #error_log /var/log/nginx/lbmySite-error.log;
    access_log syslog:server=10.20.10.1:6515 name;

    ssl_certificate /opt/mySite_certs/mySite_cert.crt;
    ssl_certificate_key /opt/mySite_certs/mySite_cert.key;

    proxy_buffering on;
    #access_log off;

    location /
    {
        proxy_set_header Host $host;
        proxy_pass https://mySite;
        proxy_redirect off;
        #proxy_connect_timeout 120s;
        #proxy_set_header Host $host;
        proxy_set_header X-Connection-Serial $connection;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

标签: apachenginx

解决方案


推荐阅读