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 · 16001 次点击
    这是一个创建于 4168 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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  
       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   ·   我们的愿景   ·   实用小工具   ·   5616 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 06:43 · PVG 14:43 · LAX 23:43 · JFK 02:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.