V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
w985341237
V2EX  ›  Python

nginx+uWSGI+flask 超时问题

  •  
  •   w985341237 · 2019-10-28 11:10:14 +08:00 · 4569 次点击
    这是一个创建于 1635 天前的主题,其中的信息可能已经有所发展或是发生改变。

    nginx 做代理使用,测试只要有接口执行时间超过 60s,nginx 就报 502,errlog 显示:upstream prematurely closed connection while reading response header from upstream。现在已经把 nginx 和 uwsgi 启动配置修改了,uwsgi 增加 harakiri 参数, nginx 增加 proxy_read_timeout,但是并没有什么效果。

    程序用 supervisor 管理,贴一下启动配置: http://ww1.sinaimg.cn/large/bbe68005ly1g8dq0eyl3zj21060b8q84.jpg

    nginx 配置: http://ww1.sinaimg.cn/large/bbe68005ly1g8dq24pep0j20wm09w42c.jpg

    20 条回复    2019-10-28 12:50:03 +08:00
    lbfeng
        1
    lbfeng  
       2019-10-28 11:11:22 +08:00
    图看不了
    scukmh
        2
    scukmh  
       2019-10-28 11:11:59 +08:00   ❤️ 1
    403, 请不要使用微博图床
    hkitdog
        3
    hkitdog  
       2019-10-28 11:23:40 +08:00
    location / {
    uwsgi_read_timeout 300s;
    uwsgi_send_timeout 300s;
    uwsgi_pass my_upstream;
    include uwsgi_params;
    }

    uwsgi.ini

    [uwsgi]
    ...
    protocol = uwsgi
    harakiri = 300
    ...
    selboo
        4
    selboo  
       2019-10-28 11:26:23 +08:00
    proxy_read_timeout 300;
    proxy_send_timeout 300;
    ClericPy
        5
    ClericPy  
       2019-10-28 11:26:27 +08:00
    nginx 默认 60 秒的 timeout 以前也遇到过, 后来要么改走 WebSocket, 要么改用消息队列, 目前反正我配置的几个地方, 结合 stackoverflow 上学的配置, 都没什么用
    w985341237
        6
    w985341237  
    OP
       2019-10-28 11:28:27 +08:00
    不好意思,微博图床看不了,换一个
    uwsgi 配置:https://sm.ms/image/WS9LOCnHjTrGtDI

    nginx 配置:https://sm.ms/image/uXIPU5cs3VDqo9B
    w985341237
        7
    w985341237  
    OP
       2019-10-28 11:29:24 +08:00
    @scukmh 谢谢,换了个图床
    w985341237
        8
    w985341237  
    OP
       2019-10-28 11:30:02 +08:00
    @hkitdog 我使用的 http 协议,其实配置文件也是这么来配置的,但是就是不好用。。
    hkitdog
        9
    hkitdog  
       2019-10-28 11:31:13 +08:00 via iPhone
    因為不是 nginx 關掉 connections, 是 uwsgi 主動 closed 的,你要對應的要在 uwsgi 設 timeout
    w985341237
        10
    w985341237  
    OP
       2019-10-28 11:31:38 +08:00
    @ClericPy 看 nginx 的 errorlog 是上游断开了,貌似是 uWSGI 的问题
    lbfeng
        11
    lbfeng  
       2019-10-28 11:31:41 +08:00
    hkitdog
        12
    hkitdog  
       2019-10-28 11:35:42 +08:00
    @w985341237

    http-timeout = 300
    socket-timeout = 300
    hkitdog
        13
    hkitdog  
       2019-10-28 11:37:30 +08:00 via iPhone
    @w985341237 你不是用 uwsgi protocol, 所以 harakiri 沒效果吧
    ClericPy
        14
    ClericPy  
       2019-10-28 11:53:40 +08:00
    @w985341237 好吧, 我当时倒是还真没用 uwsgi... 那次倒是让我长了个记性, 以后这种长耗时任务, 基本都丢消息队列做了, 走 SSE 从服务器推事件来判断.
    est
        15
    est  
       2019-10-28 12:15:59 +08:00
    兄 dei,接口执行时间都超过 60s,难道还不应该报错么。。
    w985341237
        16
    w985341237  
    OP
       2019-10-28 12:24:18 +08:00
    @hkitdog 是的,之前一直用 harakiri 设置超时不管用,改用 http-timeout 貌似可以了
    w985341237
        17
    w985341237  
    OP
       2019-10-28 12:25:29 +08:00
    @hkitdog 是的,貌似是这样。不过我设置 socket-timeout 会提示加载不了 socket-timeout,单独设置 http-timeout 貌似就可以了,谢谢
    w985341237
        18
    w985341237  
    OP
       2019-10-28 12:28:00 +08:00
    @est 数据查询太慢了,临时接手的项目,在一个数据库查完的数据,还得去另一个数据库联表再查一次,记录多一点就有些慢了。。
    aneureka
        19
    aneureka  
       2019-10-28 12:44:55 +08:00 via Android
    我居然解决过跟楼主一模一样的问题… 在 nginx conf 跟 uwsgi config 都设置相应的 timeout 就行了
    w985341237
        20
    w985341237  
    OP
       2019-10-28 12:50:03 +08:00
    @aneureka 是啊,之前添加 harakiri 不好用,添加 http-timeout 和 socket-timeout 后,socket-timeout 会报错,我就把单独添加 http-timeout 给忘了。。。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2779 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 13:24 · PVG 21:24 · LAX 06:24 · JFK 09:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.