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

2012-11-28 16:50:09 +08:00
 jmania
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"
16011 次点击
所在节点    Python
21 条回复
koon_kai
2012-11-28 18:08:52 +08:00
我刚好也是这样部署Flask,刚好也是出现这样的错误,LZ解决了的话告诉我。今晚我再折腾一下看。
lepture
2012-11-28 22:13:35 +08:00
如果使用了 flask.ext 的话,uwsgi 是部署不成功的。你可以尝试一下简单的 hello world。

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

via http://python-china.org/topic/100
SErHo
2012-11-28 22:17:26 +08:00
直接使用 gunicorn 就挺好的,uWSGI 安装配置太麻烦了,而且它的异步模式在 Flask 上可能出现问题。
yetone
2012-11-28 22:20:17 +08:00
/tmp/uwsgi.sock 权限问题?
sudo chmod 755 /tmp/uwsgi.sock
paitoubing
2012-11-28 22:38:32 +08:00
uwsgi部署的,木有出现过问题,~~,得瑟飘过。
jmania
2012-11-28 23:25:24 +08:00
@yetone 试过,好像没有用
@lepture 确实是最简单的hello world
yetone
2012-11-28 23:27:31 +08:00
@jmania 你是不是直接用了 uWSGI 提供的 http 服务?
jmania
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
2012-11-29 00:10:38 +08:00
nginx 的配置是怎樣呢?
yetone
2012-11-29 00:16:56 +08:00
贴上 Nginx 配置吧
jmania
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
2012-11-29 11:23:52 +08:00
mark , 俺之前部署也是一片悲伤。

无奈换了apache2 mod_wsgi
xuwenbao
2012-11-29 12:42:27 +08:00
试下这个呢: unix:///tmp/uwsgi.sock;
jmania
2012-11-29 13:00:55 +08:00
@xuwenbao 还是不行
achaocha
2012-11-29 13:41:05 +08:00
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
2012-11-30 13:23:55 +08:00
jmania
2012-12-21 12:25:59 +08:00
谢谢楼上的各位,配置没有问题。
偶然的情况下用safari竟然可以访问了,
这个困扰了我很长时间的问题非常奇怪,
我的是mac系统,
chrome下访问502,
但是在safari和opera下面没有问题。
mdjhny
2013-01-22 04:41:03 +08:00
你需要设置一下buf-size参数,设得稍微大点,比如30000(最多不超过65535),也可以在uwsgi加-b参数运行。

以上详见uwsgi文档,希望有用。
jmania
2013-01-22 09:46:23 +08:00
@mdjhny 谢谢
现在确定是 macos 下 chrome 缓存的问题了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/53532

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX