You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nginx请求耗时远超uWSGI问题排查求助

问题:Nginx在AWS ELB后出现请求耗时异常(仅2-3%请求受影响)

我们在AWS ELB负载均衡器后运行Nginx与uWSGI,uWSGI处理请求耗时不足100ms,但Nginx会产生额外开销,导致最终请求耗时远高于uWSGI,该问题仅出现在2-3%的请求中。我也曾尝试用gunicorn替代uWSGI,但Nginx+gunicorn组合仍存在类似问题,恳请提供技术帮助。

日志格式

'$status [$time_local] "$request" $body_bytes_sent $request_length $request_time $upstream_response_time $upstream_connect_time $upstream_header_time $upstream_status $pipe';

日志示例

200 [21/Sep/2022:11:46:46 +0000] "POST api_end_point HTTP/1.1" 7238 1546 24.848 0.036 0.000 0.036 200 .

200 [21/Sep/2022:11:46:57 +0000] "POST api_end_point HTTP/1.1" 1130 1558 2.178 0.044 0.000 0.040  200 .

200 [21/Sep/2022:11:46:56 +0000] "POST api_end_point HTTP/1.1" 1130 1565 10.212 0.028 0.000 0.024  200 .

日志分析

  • 日志1:upstream请求耗时36ms,upstream连接耗时0,但Nginx请求耗时24.848秒
  • 日志2:upstream请求耗时44ms,upstream连接耗时0,但Nginx请求耗时2.178秒
  • 日志3:upstream请求耗时28ms,upstream连接耗时0,但Nginx请求耗时10.212秒

Nginx配置

error_log  /var/log/nginx/error.log info;
worker_processes  auto;
worker_rlimit_nofile 30000;

events {
   worker_connections  1000;
   use epoll;
   multi_accept on;
}

http {
   include       /etc/nginx/mime.types;
   default_type  application/json;
   client_max_body_size 5m;
   client_body_buffer_size      256k;
   client_header_buffer_size    1k;
   large_client_header_buffers  8 64k;
   client_header_timeout  1m;
   client_body_timeout    2m;
   send_timeout           2m;
   reset_timedout_connection on;
   server_tokens off;

   sendfile        on;
   tcp_nopush      on;
   tcp_nodelay     on;

   keepalive_timeout  200;
   keepalive_requests  2000;

   log_format mycombined '$status [$time_local] "$request" $body_bytes_sent $request_length $request_time $upstream_response_time $upstream_connect_time $upstream_header_time $upstream_status $pipe';
   map $status $loggable {
       ~^[23]  0;
       default 1;
   }

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


server {
    listen       our_custom_port;
    server_name  localhost;
    access_log /var/log/nginx/access.log mycombined;
    location api_end_point {
         include uwsgi_params;
         uwsgi_read_timeout  5s;
         uwsgi_pass  unix:/opt/apis/server/uwsgi_socket.sock;
         uwsgi_ignore_client_abort on;
    }
    location /_health {
        include uwsgi_params;
        uwsgi_read_timeout  5s;
        uwsgi_pass  unix:/opt/apis/server/uwsgi_socket.sock;
        uwsgi_ignore_client_abort on;
   }
}

内容的提问来源于stack exchange,提问作者Shakir Khan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 02:30:53