uwsgi_response_write_body_do()TIMEOUT - 但uwsgi_read_timeout没有帮助(x-post)

Gre*_*reg 6 python nginx flask uwsgi

我在与Nginx接口时遇到这个错误:

uwsgi_response_write_body_do() TIMEOUT !!!
IOError: write error
Run Code Online (Sandbox Code Playgroud)

它通常工作正常.这只发生在我使用50多个并发连接进行测试时,会发送一个发送200MB数据的方法.

我一直在寻找,但所有我看到建议增加uwsgi_read_timeout :-(

(我在uwsgi github页面上问过,但我在想,因为它可能不是一个bug,最好在这里问一下)

这是我发布uwsgi的方式:

flask/bin/uwsgi -s 127.0.0.1:9001 --need-app --wsgi-file app.py --processes 1 --callable app --daemonize /opt/logs/KVAutobus-uwsgi_01.log
flask/bin/uwsgi -s 127.0.0.1:9002 --need-app --wsgi-file app.py --processes 1 --callable app --daemonize /opt/logs/KVAutobus-uwsgi_02.log
flask/bin/uwsgi -s 127.0.0.1:9003 --need-app --wsgi-file app.py --processes 1 --callable app --daemonize /opt/logs/KVAutobus-uwsgi_03.log
flask/bin/uwsgi -s 127.0.0.1:9004 --need-app --wsgi-file app.py --processes 1 --callable app --daemonize /opt/logs/KVAutobus-uwsgi_04.log
flask/bin/uwsgi -s 127.0.0.1:9005 --need-app --wsgi-file app.py --processes 1 --callable app --daemonize /opt/logs/KVAutobus-uwsgi_05.log
flask/bin/uwsgi -s 127.0.0.1:9006 --need-app --wsgi-file app.py --processes 1 --callable app --daemonize /opt/logs/KVAutobus-uwsgi_06.log
flask/bin/uwsgi -s 127.0.0.1:9007 --need-app --wsgi-file app.py --processes 1 --callable app --daemonize /opt/logs/KVAutobus-uwsgi_07.log
flask/bin/uwsgi -s 127.0.0.1:9008 --need-app --wsgi-file app.py --processes 1 --callable app --daemonize /opt/logs/KVAutobus-uwsgi_08.log
flask/bin/uwsgi -s 127.0.0.1:9009 --need-app --wsgi-file app.py --processes 1 --callable app --daemonize /opt/logs/KVAutobus-uwsgi_09.log
flask/bin/uwsgi -s 127.0.0.1:9010 --need-app --wsgi-file app.py --processes 1 --callable app --daemonize /opt/logs/KVAutobus-uwsgi_10.log
flask/bin/uwsgi -s 127.0.0.1:9011 --need-app --wsgi-file app.py --processes 1 --callable app --daemonize /opt/logs/KVAutobus-uwsgi_11.log
flask/bin/uwsgi -s 127.0.0.1:9012 --need-app --wsgi-file app.py --processes 1 --callable app --daemonize /opt/logs/KVAutobus-uwsgi_12.log
flask/bin/uwsgi -s 127.0.0.1:9013 --need-app --wsgi-file app.py --processes 1 --callable app --daemonize /opt/logs/KVAutobus-uwsgi_13.log
flask/bin/uwsgi -s 127.0.0.1:9014 --need-app --wsgi-file app.py --processes 1 --callable app --daemonize /opt/logs/KVAutobus-uwsgi_14.log
flask/bin/uwsgi -s 127.0.0.1:9015 --need-app --wsgi-file app.py --processes 1 --callable app --daemonize /opt/logs/KVAutobus-uwsgi_15.log
flask/bin/uwsgi -s 127.0.0.1:9016 --need-app --wsgi-file app.py --processes 1 --callable app --daemonize /opt/logs/KVAutobus-uwsgi_16.log
flask/bin/uwsgi -s 127.0.0.1:9017 --need-app --wsgi-file app.py --processes 1 --callable app --daemonize /opt/logs/KVAutobus-uwsgi_17.log
flask/bin/uwsgi -s 127.0.0.1:9018 --need-app --wsgi-file app.py --processes 1 --callable app --daemonize /opt/logs/KVAutobus-uwsgi_18.log
flask/bin/uwsgi -s 127.0.0.1:9019 --need-app --wsgi-file app.py --processes 1 --callable app --daemonize /opt/logs/KVAutobus-uwsgi_19.log
flask/bin/uwsgi -s 127.0.0.1:9020 --need-app --wsgi-file app.py --processes 1 --callable app --daemonize /opt/logs/KVAutobus-uwsgi_20.log
Run Code Online (Sandbox Code Playgroud)

这是/etc/nginx/nginx.conf:

user  nginx;
worker_processes  10;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  2024;
}
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  20;
    gzip  on;
    include /etc/nginx/conf.d/*.conf;
}
Run Code Online (Sandbox Code Playgroud)

这是/etc/nginx/conf.d/default.conf:

upstream internal {
    least_conn;
    server 127.0.0.1:9001;
    server 127.0.0.1:9002;
    server 127.0.0.1:9003;
    server 127.0.0.1:9004;
    server 127.0.0.1:9005;
    server 127.0.0.1:9006;
    server 127.0.0.1:9007;
    server 127.0.0.1:9008;
    server 127.0.0.1:9009;
    server 127.0.0.1:9010;
    server 127.0.0.1:9011;
    server 127.0.0.1:9012;
    server 127.0.0.1:9013;
    server 127.0.0.1:9014;
    server 127.0.0.1:9015;
    server 127.0.0.1:9016;
    server 127.0.0.1:9017;
    server 127.0.0.1:9018;
    server 127.0.0.1:9019;
    server 127.0.0.1:9020;
}

server {

    listen   9000 default_server;
    server_name  _;
  proxy_max_temp_file_size 4024m;
  #access_log  logs/host.access.log  main;
  location / {
    uwsgi_pass         internal;

    uwsgi_param   Host                 $host;
    uwsgi_param   X-Real-IP            $remote_addr;
    uwsgi_param   X-Forwarded-For      $proxy_add_x_forwarded_for;
    uwsgi_param   X-Forwarded-Proto    $http_x_forwarded_proto;

    #not sure which timeout parameter I need
    proxy_read_timeout 600;
    proxy_connect_timeout 1d;
    proxy_max_temp_file_size 5024m;
    proxy_send_timeout 600;
    uwsgi_read_timeout 600;
    uwsgi_send_timeout 600;
    include uwsgi_params;
  }
}
Run Code Online (Sandbox Code Playgroud)

小智 1

只需使用指令:

uwsgi_max_temp_file_size 0;
Run Code Online (Sandbox Code Playgroud)

或者

uwsgi_buffering off;
Run Code Online (Sandbox Code Playgroud)

禁用 Nginx 中的缓冲区。

我认为从nginx到客户端的传输速度远远慢于从uwsgi到nginx的速度。而nginx的缓冲区已满,因此uwsgi需要等待很长时间才能有空间让nginx接收新数据,这使得uwsgi的写入请求超时。

您可以在我的博客中阅读我对此问题的分析:http://kmiku7.github.io/2018/02/02/Solve-the-problem-of-uwsgi-uwsgi-response-write-body-do-TIMEOUT-错误/