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

Nginx报uWSGI上游读取超时,Flask应用请求输出300KB求解决

Fixing Nginx uWSGI Upstream Timeout for Flask Apps with Large Responses

Let’s walk through how to resolve this timeout issue—your setup (Flask + uWSGI Emperor + Nginx) is common, but the ~300KB per-request response size combined with potential misconfigured timeouts or buffer limits is likely causing the problem. Here’s what to check and adjust:

1. Tweak Nginx’s uWSGI Timeout & Buffer Settings

Nginx’s default uWSGI timeout values might be too short for your response size, especially if the app takes a moment to generate or transmit that 300KB data. Update your Nginx server block’s location configuration for the app:

location /context-path {
    include uwsgi_params;
    uwsgi_pass unix:/etc/uwsgi/sockets/your-app-name.sock;

    # Extend timeouts to accommodate slower response generation/transmission
    uwsgi_connect_timeout 30s;  # Time to establish connection to uWSGI
    uwsgi_send_timeout 60s;     # Time to send request to uWSGI
    uwsgi_read_timeout 120s;    # Time to wait for full response from uWSGI

    # Adjust buffers to handle your 300KB response efficiently
    uwsgi_buffer_size 64k;      # Buffer for the first part of the response
    uwsgi_buffers 8 64k;        # Total buffer space (8*64k = 512k, enough for 300KB)
    uwsgi_busy_buffers_size 128k;  # Buffer size for active responses
}

After making these changes, reload Nginx with sudo systemctl reload nginx.

2. Optimize uWSGI Configuration for Large Responses

Your current uWSGI config has 4 processes and 20 threads, which is reasonable for many workloads, but we can add parameters to improve response handling:

Update your uWSGI .ini file:

[uwsgi]
# Your existing config...
chdir = /var/www/%n
socket = /etc/uwsgi/sockets/%n.sock
chmod-socket = 660
vaccum = true
processes = 4
threads = 20
virtualenv = /var/www/%n/.venv
module = app:app
logto = /var/log/uwsgi/%n.log

# Add these settings:
buffer-size = 65536  # Increase socket buffer to 64KB (matches Nginx's buffer)
tcp-nodelay = true   # Disable Nagle's algorithm to reduce transmission latency
harakiri = 120       # Kill requests that take longer than 120s (prevents stuck workers)
log-format = "%(addr) - %(user) [%(ltime)] %(method) %(uri) %(proto) %(status) %(size) %(time)"  # Log request duration for debugging

Restart uWSGI Emperor to apply changes: sudo systemctl restart uwsgi.

3. Improve Flask App’s Response Generation

If your app is generating the entire 300KB response in memory before sending it, that can slow down transmission and trigger timeouts. Use Flask’s streaming response to send data in chunks:

from flask import Flask, Response, stream_with_context

app = Flask(__name__)

@app.route('/context-path', methods=['POST'])
def large_response():
    def generate_response():
        # Replace this with your actual content generation logic
        # Split your 300KB content into smaller chunks (e.g., 4KB each)
        chunk_size = 4096
        total_bytes = 300 * 1024
        sent_bytes = 0

        while sent_bytes < total_bytes:
            current_chunk_size = min(chunk_size, total_bytes - sent_bytes)
            # Generate or fetch the chunk here
            chunk = b'your-content-here' * (current_chunk_size // len(b'your-content-here'))
            yield chunk
            sent_bytes += current_chunk_size

    # Return a streaming response
    return Response(stream_with_context(generate_response()), mimetype='application/octet-stream')

Streaming reduces memory usage and starts sending data to Nginx immediately, cutting down on time spent waiting for the full response to be ready.

4. Check System-Level Limits

If the above fixes don’t work, verify your system isn’t imposing limits that slow down connections:

  • File Descriptors: Ensure uWSGI has enough file descriptors to handle concurrent connections. Check with ulimit -n (should be at least 1024, higher for busy apps). You can increase this in /etc/security/limits.conf for the uWSGI user.
  • Network Buffers: Adjust system-wide network buffer sizes if needed. Run sysctl net.core.wmem_max to check the maximum write buffer size; if it’s less than 1MB, increase it with sysctl -w net.core.wmem_max=1048576 and add the setting to /etc/sysctl.conf to persist after reboot.

Final Debugging Tip

Check the uWSGI log with tail -f /var/log/uwsgi/your-app.log while sending requests. The updated log-format will show how long each request takes (%(time) field). If requests are taking longer than 120s, you’ll need to optimize the actual logic in your Flask app (e.g., slow database queries, heavy computations) to reduce processing time.

内容的提问来源于stack exchange,提问作者Mouli Kalakota

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:24:48