sách gpt4 ăn đã đi

python - 使用 nginx 和 gunicorn 运行 flask 应用程序

In lại Tác giả: IT Lão Cao 更新时间:2023-10-28 21:01:47 25 4
mua khóa gpt4 giày nike

我是新手,只使用 nginx 来提供静态文件。我现在已经安装了 flask 和 gunicorn。如果我运行 gunicorn -b 127.0.0.2:8000 hello:app 然后从服务器 wget 它运行良好。但是,如果我尝试从浏览器访问它,它会返回 404 错误(我在托管位于根目录的 wordpress 站点的服务器上运行它)。

flask 应用:

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

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

app.wsgi_app = ProxyFix(app.wsgi_app)

nếu __name__ == '__main__':
app.run()

以及我的 nginx 配置的相关部分:

location /flask {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_\
for;
proxy_pass http://127.0.0.2:8000;
proxy_redirect off;
}

我希望这是所有相关信息。如果没有,请告诉。谢谢!

câu trả lời hay nhất

这就是我在 Nginx 中提供 flask 应用程序的方式:

使用套接字运行 gunicorn 守护进程:

  sudo gunicorn app:app --bind unix:/tmp/gunicorn_flask.sock -w 4 -D

相关的nginx配置:

    upstream flask_server {
# swap the commented lines below to switch between socket and port
server unix:/tmp/gunicorn_flask.sock fail_timeout=0;
#server 127.0.0.1:5000 fail_timeout=0;
}
máy chủ {
listen 80;
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}

máy chủ {
listen 80;
client_max_body_size 4G;
server_name example.com;

keepalive_timeout 5;

# path for static files
location /static {
alias /path/to/static;
autoindex on;
expires max;
}

vị trí / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;

if (!-f $request_filename) {
proxy_pass http://flask_server;
phá vỡ;
}
}
}

}

关于python - 使用 nginx 和 gunicorn 运行 flask 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13660118/

25 4 0
Cựu chiến binh CNTT
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress