我仍然是相当新的水瓶座/Nginx/Gunicorn,因为这是我的第二个网站使用组合。我创建了一个基于米格尔·格林伯格的教程的网站,所以我的文件结构与本教程完全相同。
在我以前的应用程序中,我的应用程序位于一个名为app.py的文件中,所以当我使用Gunicorn时,我刚刚调用了
gunicorn app:app
现在我的新应用程序被分割成多个文件,我使用了一个文件run.py来启动这个应用程序,但我不知道现在该如何调用Gunicorn。我读过其他的问题和教程,但它们都没有用。当我运行gunicorn run:app并尝试访问该站点时,我会得到一个502坏网关错误。
我想我的问题更多的是贡尼孔,而不是Nginx或酒瓶,因为如果我只输入./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;
}
}发布于 2014-10-06 11:51:41
正在发生的情况是,当gunicorn导入app.py时,开发服务器正在运行。您只希望在直接执行文件(例如,python app.py)时发生这种情况。
#!flask/bin/python
from app import app
from werkzeug.contrib.fixers import ProxyFix
app.wsgi_app = ProxyFix(app.wsgi_app)
if __name__ == '__main__':
# You don't need to set the port here unless you don't want to use 5000 for development.
app.run(debug=True)完成此更改后,您应该能够使用gunicorn run:app运行应用程序。注意,gunicorn默认使用端口8000。如果希望在备用端口(例如,8001)上运行,则需要使用gunicorn -b :8001 run:app指定该端口。
https://stackoverflow.com/questions/26211267
复制相似问题