我还是很新的Flask/Nginx/Gunicorn,因为这只是我第二个使用这个组合的站点。我创建了一个基于Miguel Grinberg的tutorial的网站,因此我的文件结构与本教程完全相同。Nginx,Flask,Gunicorn 502错误
在我以前的瓶的应用程序,我的应用程序是在一个名为app.py
这样一个文件,当我用Gunicorn我只是叫
gunicorn app:app
现在,我的新的应用程序分为我用一个文件中的多个文件run.py
开始应用程序,但我不知道我现在应该怎么打电话给Gunicorn。我已阅读其他问题和教程,但他们没有奏效。当我运行gunicorn run:app
并尝试访问站点时,出现502错误网关错误。
我觉得我的问题比Nginx或Flask更加Gunicorn,因为如果我只键入./run.py
,网站就可以工作。无论如何,我已经在下面包含了我的Nginx配置和一些其他文件。非常感谢你的帮助!
文件:run.py
#!flask/bin/python
from app import app
from werkzeug.contrib.fixers import ProxyFix
app.wsgi_app = ProxyFix(app.wsgi_app)
app.run(debug = True, port=8001)
文件:app/views.py
from app import app
@app.route('/')
@app.route('/index')
def index():
posts = Post.query.order_by(Post.id.desc()).all()
return render_template('index.html', posts=posts)
文件:nginx.conf
server {
listen 80;
server_name example.com;
root /var/www/example.com/public_html/app;
access_log /var/www/example.com/logs/access.log;
error_log /var/www/example.com/logs/error.log;
client_max_body_size 2M;
location/{
try_files $uri @gunicorn_proxy;
}
location @gunicorn_proxy {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://127.0.0.1:8001;
}
}
您是否检查过您可以访问http://127.0.0.1:8001?然后 - 我将“location @ gunicorn_proxy”更改为“location /”(并注释掉以前的位置部分) – soerium 2014-10-06 07:40:36
@soerium我可以访问127.0.0.1:8001,因为当我从命令行运行我的应用程序时,Flask会打印'* Running在http://127.0.0.1:8001'上。 – Connie 2014-10-06 17:17:20