簡體   English   中英

使用 Nginx 和 Gunicorn 運行 Flask 應用程序

[英]Run Flask application with Nginx and Gunicorn

這是我第一次部署 webapp,我真的需要你的幫助。 我想在使用 Nginx 和 Gunicorn 的服務器上運行 Flask Web 應用程序。 我找到了教程,但我無法正確運行該應用程序。 我也嘗試過在互聯網上找到的其他方法,但沒有。 這是我當前的 /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 中添加端口,則找不到頁面。 我究竟做錯了什么?

Nginx 可能有另一個默認端口為 80 的站點。確保您已從/etc/nginx/sites-enabled刪除 default.conf 符號鏈接。 檢查 nginx.conf 以確保其中沒有配置服務器。

您的 gunicorn 命令看起來也不完全正確。 您需要使用應用目錄中的gunicorn -b 127.0.0.1:8000 test:app

您僅將 gunicorn 服務器綁定到本地接口 127.0.0.1,因此無法從外部訪問。 您必須使用 nginx 作為端口 80 的反向代理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM