首页 文章

使用Nginx和Gunicorn运行Flask应用程序

提问于
浏览
-1

这是我第一次部署webapp,我真的需要你的帮助 . 我想使用Nginx和Gunicorn在服务器上运行Flask Web应用程序 . 我找到了this教程,但我无法正确运行该应用程序 . 我也尝试过在互联网上找到的其他方式,但没有 . 这是我目前的/etc/nginx/sites-available/test.conf文件

server {
listen 80;
server_name hello.itu24.com;

root /home/ubuntu/test;

location / {
    proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://127.0.0.1:8000;    
    }
}

相反,这是我的应用程序/home/ubuntu/test.py

from flask import Flask
from werkzeug.contrib.fixers import ProxyFix
app = Flask(__name__)

@app.route('/')
def test():
    return "Hello world!"

app.wsgi_app = ProxyFix(app.wsgi_app)

if __name__ == '__main__':
    app.run()

然后我运行以下命令

ubuntu@ace:~$ sudo service nginx reload
* Reloading nginx configuration nginx                           [ OK ] 
ubuntu@ace:~$ sudo gunicorn -b h 127.0.0.1:8000 test:app

然后,如果我转到机器的IP地址,因为我从另一台机器连接,我可以看到Nginx页面 . 但是如果我在地址IP:8000中添加端口,则找不到页面 . 我究竟做错了什么?

2 回答

  • 0

    您只将gunicorn服务器绑定到本地接口127.0.0.1,因此无法从外部访问它 . 您必须在端口80使用nginx作为反向代理 .

  • 1

    Nginx可能有另一个站点默认为端口80.确保您已从 /etc/nginx/sites-enabled 删除了default.conf符号链接 . 检查nginx.conf以确保它没有配置服务器 .

    你的gunicorn命令看起来也不正确 . 您需要在应用程序的目录中使用 gunicorn -b 127.0.0.1:8000 test:app .

相关问题