使用uWSGI+Nginx部署Flask应用,不成功 - V2EX
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
jmania
V2EX    Python

使用uWSGI+Nginx部署Flask应用,不成功

  •  
  •   jmania 2012-11-28 16:50:09 +08:00 16790 次点击
    这是一个创建于 4712 天前的主题,其中的信息可能已经有所发展或是发生改变。
    uwsgi启动后
    WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x7fe603c0b1f0 pid: 1228 (default app)
    *** uWSGI is running in multiple interpreter mode ***
    spawned uWSGI master process (pid: 1228)
    spawned uWSGI worker 1 (pid: 1229, cores: 1)
    spawned uWSGI worker 2 (pid: 1230, cores: 1)
    spawned uWSGI worker 3 (pid: 1231, cores: 1)
    spawned uWSGI worker 4 (pid: 1232, cores: 1)

    页面显示
    502 Bad Gateway

    uwsgi输出
    invalid request block size: 4665 (max 4096)...skip
    Wed Nov 28 16:45:14 2012 - error parsing request

    nginx log:
    2012/11/28 16:47:51 [error] 1240#0: *25 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/tmp/uwsgi.sock:", host: "localhost"
    2012/11/28 16:47:51 [error] 1240#0: *25 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", upstream: "uwsgi://unix:/tmp/uwsgi.sock:", host: "localhost"
    21 条回复    2015-11-29 12:30:04 +08:00
    koon_kai
        1
    koon_kai  
       2012-11-28 18:08:52 +08:00
    我刚好也是这样部署Flask,刚好也是出现这样的错误,LZ解决了的话告诉我。今晚我再折腾一下看。
    lepture
        2
    lepture  
       2012-11-28 22:13:35 +08:00
    如果使用了 flask.ext 的话,uwsgi 是部署不成功的。你可以尝试一下简单的 hello world。

    但是问题是,你为何要用 uwsgi?更好的选择是什么?

    via http://python-china.org/topic/100
    SErHo
        3
    SErHo &nbp;
       2012-11-28 22:17:26 +08:00
    直接使用 gunicorn 就挺好的,uWSGI 安装配置太麻烦了,而且它的异步模式在 Flask 上可能出现问题。
    yetone
        4
    yetone  
       2012-11-28 22:20:17 +08:00
    /tmp/uwsgi.sock 权限问题?
    sudo chmod 755 /tmp/uwsgi.sock
    paitoubing
        5
    paitoubing  
       2012-11-28 22:38:32 +08:00
    uwsgi部署的,木有出现过问题,~~,得瑟飘过。
    jmania
        6
    jmania  
    OP
       2012-11-28 23:25:24 +08:00
    @yetone 试过,好像没有用
    @lepture 确实是最简单的hello world
    yetone
        7
    yetone  
       2012-11-28 23:27:31 +08:00
    @jmania 你是不是直接用了 uWSGI 提供的 http 服务?
    jmania
        8
    jmania  
    OP
       2012-11-28 23:40:56 +08:00
    @yetone 这个是我uWSGI的配置
    <uwsgi>
    <socket>/tmp/uwsgi.sock</socket>
    <virtualenv>/Users/jmania/Python/virtualenv/testenv</virtualenv>
    <pythonpath>/Users/jmania/Python/myflask/test</pythonpath>
    <chdir>/Users/jmania/Python/myflask/test</chdir>
    <module>store</module>
    <callable>app</callable>
    <master/>
    <processes>4</processes>
    <memory-report/>
    <chmod-socket>666</chmod-socket>
    </uwsgi>
    raptium
        9
    raptium  
       2012-11-29 00:10:38 +08:00
    nginx 的配置是怎呢?
    yetone
        10
    yetone  
       2012-11-29 00:16:56 +08:00
    贴上 Nginx 配置吧
    jmania
        11
    jmania  
    OP
       2012-11-29 10:18:33 +08:00
    @raptium
    @yetone
    nginx 的配置,
    我的系统是Mac
    server {
    listen 80;
    server_name www.myapp.com;

    location / {
    include uwsgi_params;
    uwsgi_pass unix:/tmp/uwsgi.sock;
    }
    }
    @raptium
    @yetone
    domizzi
        12
    domizzi  
       2012-11-29 11:23:52 +08:00
    mark , 俺之前部署也是一片悲伤。

    无奈换了apache2 mod_wsgi
    xuwenbao
        13
    xuwenbao  
       2012-11-29 12:42:27 +08:00
    试下这个呢: unix:///tmp/uwsgi.sock;
    jmania
        14
    jmania  
    OP
       2012-11-29 13:00:55 +08:00
    @xuwenbao 还是不行
    achaocha
        15
    achaocha  
       2012-11-29 13:41:05 +08:00
    lnehe
        16
    lnehe  
       2012-11-29 13:43:41 +08:00
    创建目录(site_doc是main.py的存放目录,/home/python_venv是你的python虚拟环境)
    mkdir -p /home/python_venv && \
    mkdir -p /home/site_doc && \
    chown -R www:www /site_doc && \
    chmod -R 775 /site_doc

    # 安 virtualenv ###########################
    easy_install virtualenv && \
    virtualenv --no-site-packages /home/python_venv

    # 安 xx 到虚拟环境 ###########################
    # 我安装了flask,你的bottle就换成bottle
    source /home/python_venv/bin/activate && \
    easy_install Flask && \
    deactivate

    # nginx配置
    location / {
    uwsgi_pass 127.0.0.1:8888;
    include uwsgi_params;
    uwsgi_param UWSGI_PYHOME /home/python_venv;
    uwsgi_param UWSGI_CHDIR /home/site_doc;
    uwsgi_param UWSGI_SCRIPT main;
    }

    # uwsgi启动
    /usr/local/uwsgi/uwsgi -s :8888 -M -p 4 -t 30 -R 10000 --post-buffering 40960 -d /usr/local/uwsgi/logs/uwsgi.log --disable-logging --close-on-exec --env PYTHON_EGG_CACHE=/tmp --socket-timeout 60 --http-timeout 60 --limit-as 512 --gid www --uid www --no-site --vhost


    注意路径问题,自己修改一下
    da_a
        17
    da_a  
       2012-11-30 13:23:55 +08:00
    jmania
        18
    jmania  
    OP
       2012-12-21 12:25:59 +08:00
    谢谢楼上的各位,配置没有问题。
    偶然的情况下用safari竟然可以访问了,
    这个困扰了我很长时间的问题非常奇怪,
    我的是mac系统,
    chrome下访问502,
    但是在safari和opera下面没有问题。
    mdjhny
        19
    mdjhny  
       2013-01-22 04:41:03 +08:00
    你需要设置一下buf-size参数,设得稍微大点,比如30000(最多不超过65535),也可以在uwsgi加-b参数运行。

    以上详见uwsgi文档,希望有用。
    jmania
        20
    jmania  
    OP
       2013-01-22 09:46:23 +08:00
    @mdjhny 谢谢
    现在确定是 macos 下 chrome 缓存的问题了
    zhang330700
        21
    zhang330700  
       2015-11-29 12:30:04 +08:00
    我遇到同样问题,在 uwsgi.yml 文件中加了一个 plugin 参数就搞定了
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3980 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 10:13 PVG 18:13 LAX 03:13 JFK 06:13
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86