Docker中出现Nginx 504网关超时错误

5
我正在使用PHP和Apache,结合nginx反向代理,在Docker上运行。我有几个长时间运行的调用在60秒后出现超时,导致504网关超时错误。我知道我的应用程序已经成功调用,因为我正在记录我的PHP应用程序的日志,并且可以看到它正在积极地写入日志。每一次都是60秒的超时,但我似乎无法找到该设置所在的位置。
我尝试了this post中的建议,但没有起作用。我已更新了我的php.ini文件中的一些与时间相关的设置,并验证了它们已被phpinfo设置。
max_input_time = 0
max_execution_time = 500

我还将内存限制增加到512,但考虑到每次都会在大约60秒后超时,所以我不认为这是问题所在。
至于更新nginx设置,我最初遵循了这个教程来调整nginx代理超时时间,但那并没有起作用。我撤销了更改,然后通过ssh进入容器并手动更新了/etc/nginx/nginx.conf文件,下面是http部分的内容。
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout           500;
    proxy_connect_timeout       600;
    proxy_send_timeout          600;
    send_timeout                600;
    client_max_body_size        5000;
    client_header_timeout       600;
    client_body_timeout         600;
    fastcgi_read_timeout        300;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

在更新nginx.conf文件后,我确保运行nginx -s reload。我不确定还有哪里需要查看,因为我已经做了几乎所有相关的事情。还有什么原因会导致nginx在60秒后超时?谢谢。

这是我的PHP dockerfile:

FROM php:7.2-fpm-alpine3.7

RUN apk update; \
    apk upgrade;

RUN docker-php-ext-install pdo_mysql

RUN apk add --no-cache php7-pear php7-dev gcc musl-dev make
RUN pecl install xdebug


RUN pecl install -o -f redis \
&&  rm -rf /tmp/pear \
&&  docker-php-ext-enable redis
3个回答

3
问题在于nginx有自己的超时时间。理想情况下,您应该同步nginx和PHP。我无法在此提到Apache,因为我不知道您使用的是哪种模式(FPM还是mod_php)。我也不确定为什么您同时运行Nginx和Apache,但是,如果您收到504响应,并且PHP仍在处理请求,则Nginx将终止请求并返回504响应。 Nginx不像使用mod_php的Apache那样运行,其中进程是相同的。 Nginx将转发请求并等待任何进程返回响应。 请参见我们配置文件中关于Nginx超时的以下设置。
# Timeouts
# The client_body_timeout and client_header_timeout directives are
# responsible for the time a server will wait for a client body or
# client header to be sent after request. If neither a body or header
# is sent, the server will issue a 408 error or Request time out.
#
# The keepalive_timeout assigns the timeout for keep-alive connections
# with the client. Simply put, Nginx will close connections with the
# client after this period of time.
#
# Finally, the send_timeout is a timeout for transmitting a response
# to the client. If the client does not receive anything within this
# time, then the connection will be closed. Send the client a "request
# timed out" if the body is not loaded by this time. Default 60.
client_body_timeout   32;
client_header_timeout 32;
# Every 60 seconds server broadcasts Sync packets, so 90 is a conservative upper bound. Default is 65
keepalive_timeout 90;
send_timeout 300;

0

这个答案可能会帮助那些在子网中使用反向代理NGINX实例和NAT网关的人。

我也曾经遇到过504上游超时错误。正如链接中所描述的那样,这既不是NGINX的问题,也不是后端API的问题。

NAT网关一次只能接受有限数量的连接。

这导致NGINX无法连接到上游。


-1
这个配置对我解决了问题。
upstream http_backend {
    server 127.0.0.1:8080;

    keepalive 16;
}

server {
    ...

    location /http/ {
        proxy_pass http://http_backend;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        ...
    }
}

特别是添加 proxy_set_header Connection "";



网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接