【问题标题】:Request timeout in nginxnginx中的请求超时
【发布时间】:2016-07-07 10:20:59
【问题描述】:

我在应用程序的服务器端(在 nginx 上)收到 499,在客户端收到 504。客户端的默认超时为 5000 秒,而我得到的响应正好在 60 秒后。

下面是我的应用程序的nginx.conf

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

events {
        worker_connections 768;
        # multi_accept on;
}

http {
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        underscores_in_headers on;
        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # 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;
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
    }

gunicorn config 是:

bind = '127.0.0.1:8001'
backlog = 2048
workers = 8
worker_class = 'sync'
worker_connections = 1000
timeout = 300
keepalive = 2
spew = False
daemon = False
pidfile = None
umask = 0
user = None
group = None
tmp_upload_dir = None
errorlog = 'gunicorn_error.log'
loglevel = 'info'
accesslog = 'gunicorn_access.log'
proc_name = None
def post_fork(server, worker):
    server.log.info("Worker spawned (pid: %s)", worker.pid)

def pre_fork(server, worker):
    pass

def pre_exec(server):
    server.log.info("Forked child, re-executing.")

def when_ready(server):
    server.log.info("Server is ready. Spawning workers")

def worker_int(worker):
    worker.log.info("worker received INT or QUIT signal")

    ## get traceback info
    import threading, sys, traceback
    id2name = dict([(th.ident, th.name) for th in threading.enumerate()])
    code = []
    for threadId, stack in sys._current_frames().items():
        code.append("\n# Thread: %s(%d)" % (id2name.get(threadId,""),
            threadId))
        for filename, lineno, name, line in traceback.extract_stack(stack):
            code.append('File: "%s", line %d, in %s' % (filename,
                lineno, name))
            if line:
                code.append("  %s" % (line.strip()))
    worker.log.debug("\n".join(code))

def worker_abort(worker):
    worker.log.info("worker received SIGABRT signal")

有人能准确告诉我为什么会这样吗?还有我该如何纠正它。

【问题讨论】:

    标签: django nginx http-status-codes request-timed-out


    【解决方案1】:

    我在长时间执行命令时遇到了同样的问题。我在 nginx 配置中使用类似的根端点配置解决了这个问题:

    location / { 
        proxy_pass http://127.0.0.1:8001; 
        ...
        proxy_connect_timeout 5000s; 
        proxy_read_timeout 5000s; 
    }
    

    注意proxy_connect_timeoutproxy_read_timeout

    还可能有助于向用户返回一些快速响应,例如“正在处理...”或“正在加载...”并在后台运行主任务。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-23
      • 2018-11-09
      • 1970-01-01
      • 2011-02-26
      • 2019-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多