记一次nginx 504排查

有一个导出接口,由于里面组装数据会很慢,直接表现为504 Gateway Time-out,时间为2min,梳理了下调用的链路,nginx -> spring gateway -> business server,里面打开了nginx的配置,如下:

#user  nobody;
worker_processes  4;

error_log  /app/openresty/nginx/logs/error.log  error;
pid        /app/openresty/nginx/logs/nginx.pid;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;


events {
    worker_connections 65536 ;
}


http {
    include       mime.types;
    include       custom_upstream.conf;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr|$remote_user|[$time_local]|"$request"'
                      '|$status|$request_time|$body_bytes_sent|"$http_referer"'
                      '|"$http_user_agent"|"$http_x_forwarded_for"|$upstream_response_time|$upstream_status';    
    access_log  /app/openresty/nginx/logs/access.log  main;
    
    server_tokens   off;
    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     on;
    client_max_body_size 100m;
    client_header_buffer_size 100m;
    keepalive_timeout  120;
    proxy_send_timeout 120;
    proxy_read_timeout 120;
    proxy_buffering off;
    gzip on;

    server {
        listen       80;
        server_name  localhost;

        listen 443 ssl;
        ssl_certificate_key /app/openresty/nginx/conf/key/sf.key;
        ssl_certificate /app/openresty/nginx/conf/key/sf.crt;

        #charset koi8-r;

        include       custom_location.conf;

        #location / {
        #    root   html;
        #    index  index.html index.htm;
        #}

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
		location = /50x.html {
            root   html;
        }

        location = /nstats {
            check_status;
            access_log      off;
            allow           10.116.0.0/16;
            allow           10.110.0.0/16;
            allow           10.117.0.0/16;
            allow           10.150.0.0/16;
            allow           100.0.0.0/8;
            allow           10.0.0.0/8;
            deny            all;
        }
    }
}
	location / {
		root   html;
		index  index.html index.htm;
	}
	

	location ^~ /foo {
		proxy_pass http://bar:8080;
		proxy_set_header Host $host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header Cookie $http_cookie;
		index index.html index.jsp login.jsp index.htm;    
	}

配置经过处理,已替换成foo,bar

果然在配置中找到了2min的配置,我们的接口是一个导出的接口,自然是跟响应有关,说干就干,那就简单了,改成30min分钟。

    keepalive_timeout  120;
    proxy_send_timeout 120;
    proxy_read_timeout 120;

为了不影响其他接口,这里我们新增了一个精确匹配的路由

    location = /foo/downloaddatafile {

        proxy_pass http://bar:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Cookie $http_cookie;
        keepalive_timeout  120;
        proxy_send_timeout 120;
        proxy_read_timeout 1800;        
    }

重启一波,点击导出,依然是504,这次的超时竟然还变短了,1min.what???这是什么情况。

只能打开浏览器的respose,很幸运是有东西的,提示我们去检查nginx的error.log.

好的,那我们就去看看吧,关键信息:[error] 33#0: *703 upstream timed out (110: Connection timed out)

度娘告诉我们需要改缓冲配置。

 

    location = /aom-asset-repair/repair/downloaddatafile {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Cookie $http_cookie;
        keepalive_timeout  120;
        proxy_send_timeout 120;
        proxy_read_timeout 1800;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_buffer_size 64k;
        proxy_buffers   4 32k;
        proxy_busy_buffers_size 64k;
        proxy_temp_file_write_size 64k;
        proxy_pass http://bar:8080;
    }

写在最后:

1. proxy_pass配置的顺序为啥会影响其他配置项生效?

2.导出这种慢操作,建议优化成异步处理,后台异步处理时,可以使用多线程。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到,要解决Nginx偶发的504错误,可以通过优化程序来缩短执行时间。如果是文件解析等耗时较长的任务,可以调大Nginx的超时限制参数,使程序可以正常执行。可以修改Nginx的配置文件,增加fastcgi_connect_timeout、fastcgi_send_timeout和fastcgi_read_timeout等参数来调整超时时间。另外,还可以通过修改代理连接超时、代理发送超时和代理接收超时等参数来解决问题。\[1\] 引用\[2\]中提到,有时候Nginx返回504错误是因为查询接口本身就很慢,而此时数据库插入了大批量数据,导致查询更加缓慢。在这种情况下,接口直接返回了Nginx的404错误。解决这个问题可以通过排查Nginx的配置文件,确保配置没有问题。\[2\] 引用\[3\]中给出了一个Nginx的配置示例,其中包括了一些关于超时时间的设置。可以根据实际情况调整这些超时时间参数,以解决偶发的504错误。\[3\] 综上所述,要解决Nginx偶发的504错误,可以通过优化程序、调整超时时间参数以及检查Nginx的配置文件来解决问题。 #### 引用[.reference_title] - *1* [Nginx 出现504 Gateway Time-out的解决方法](https://blog.csdn.net/qq_41978323/article/details/130557557)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Nginx报404](https://blog.csdn.net/Z_Plin/article/details/128558522)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [一次nginx配置修改,修复偶发502 504错误的问题](https://blog.csdn.net/aaronmer/article/details/106258842)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值