Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Nginx,烧瓶,Gunicorn 502错误_Python_Nginx_Flask_Gunicorn - Fatal编程技术网

Python Nginx,烧瓶,Gunicorn 502错误

Python Nginx,烧瓶,Gunicorn 502错误,python,nginx,flask,gunicorn,Python,Nginx,Flask,Gunicorn,我对Flask/Nginx/Gunicorn仍然很陌生,因为这只是我第二个使用这种组合的站点。我创建了一个基于Miguel Grinberg的网站,因此我的文件结构与本教程中的完全相同 在我以前的Flask应用程序中,我的应用程序位于一个名为app.py的文件中,因此当我使用Gunicorn时,我只调用了 gunicorn应用程序:应用程序 现在,我的新应用程序被拆分为多个文件,我使用了一个文件run.py来启动该应用程序,但我不确定现在应该如何调用Gunicorn。我读过其他的问题和教程,但都

我对Flask/Nginx/Gunicorn仍然很陌生,因为这只是我第二个使用这种组合的站点。我创建了一个基于Miguel Grinberg的网站,因此我的文件结构与本教程中的完全相同

在我以前的Flask应用程序中,我的应用程序位于一个名为
app.py
的文件中,因此当我使用Gunicorn时,我只调用了
gunicorn应用程序:应用程序

现在,我的新应用程序被拆分为多个文件,我使用了一个文件
run.py
来启动该应用程序,但我不确定现在应该如何调用Gunicorn。我读过其他的问题和教程,但都不管用。当我运行
gunicorn run:app
并尝试访问该站点时,我收到一个502错误网关

我认为我的问题更像是Gunicorn而不是Nginx或Flask,因为只要我键入
/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)
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)
文件:
app/views.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)
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;
    }
}

当gunicorn导入
app.py
时,开发服务器正在运行。您只希望在直接执行文件时发生这种情况(例如,
python app.py


完成此更改后,您应该能够使用
gunicorn run:app
运行应用程序。请注意,默认情况下,gunicorn使用端口8000。如果您希望在备用端口(例如8001)上运行,则需要使用
gunicorn-b:8001 run:app

指定您是否检查了是否可以访问?然后-我会将“location@gunicorn_proxy”更改为“location/”(并注释掉前面的位置部分)@soerium我可以访问127.0.0.1:8001,因为当我从命令行运行我的应用程序时,Flask打印运行在http://127.0.0.1:8001。谢谢你回复我。我尝试更改
run.py
以包含您建议的
if
语句,如果我直接从命令(如is
$)/run.py
执行它,它会工作,但如果我尝试gunicorn
$gunicorn rup:app
,它不会工作。哇,这是一个打字错误。当我键入
$Gunicorn run:app
时,Gunicorn似乎不起作用。我更新了答案,以包含有关指定端口的信息。