从上游读取响应头时上游超时(110:连接超时)

Dev*_*kar 7 php nginx amazon-web-services amazon-elb laravel-5.1

我是 nginx 服务器的新手。我目前面临的问题是从 iOS 应用程序拨打电话后连接超时问题。我收到的 Json 很大,因此服务器上记录了超时。

还检查了在stackoverflow上发布的各种答案。但没有帮助我。

下面是我的访问日志和错误日志。请帮忙。

错误日志

upstream timed out (110: Connection timed out) while reading response header from upstream, client: xxxxxxxxxxx, server: xxxxxxxxxxx, request: "POST /api/event/gallery HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock", host: "xxxxxxxxxxx"
Run Code Online (Sandbox Code Playgroud)

访问日志

"POST /api/event/gallery HTTP/1.1" 504 192 "-" "mysite/2 CFNetwork/889.9 Darwin/16.7.0"
Run Code Online (Sandbox Code Playgroud)

配置文件

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    client_max_body_size 50M;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
# 
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}
Run Code Online (Sandbox Code Playgroud)

Dev*_*kar 6

更新

当我检查这个问题时,我增加了时间设置 -request_terminate_timeout在 \etc\php\7.0\fpm\pool.d\www.conf 中,遇到了 HTTP 状态代码 499。 访问日志

"POST /api/event/gallery HTTP/1.1" 499 0 "-" "mysite/2 CFNetwork/889.9 Darwin/16.7.0"
Run Code Online (Sandbox Code Playgroud)

所以我开始调查这个问题,发现有负载均衡器(AWS ELB),它有一个默认为 60 秒的超时设置。参考https://www.cadence-labs.com/2017/07/fix-nginx-timeout-499-client-closed-request/

在那里增加超时(ELB)后,我设法在邮递员那里得到了结果。但事实证明它非常不稳定。这意味着我会在 4-5 次尝试中得到一次回复。

所以我设法在 etc\nginx\sites-available\default 中添加了一些设置,在每个服务器和位置块中添加了下面的设置。

fastcgi_read_timeout 540;
proxy_connect_timeout 3000s;
proxy_send_timeout   3000;
proxy_read_timeout   3000;
Run Code Online (Sandbox Code Playgroud)

并重新启动nginx。

这解决了这个问题。但是我仍然认为响应所花费的时间很高,性能还没有达到可以接受的水平。

此外,任何以更好的方式解决这个问题的建议/意见/建议都是最受欢迎的。