首页 > 解决方案 > 带有 web uwsgi 和 nginx 的烧瓶无法运行 index.html

问题描述

我在烧瓶中很新,尝试运行一个后端作为烧瓶的 Web 应用程序。我的项目文件夹结构是
~myproject/
~myproject/app (flask api)
~myproject/web (index.html)

使用 uwsgi 和 nginx 运行
uwsgi.ini

[uwsgi]
vhost = true
socket = /tmp/flask.sock
venv = /flask_app/.env
chdir = /flask_app/app
module = app
callable = app

nginx.conf

upstream flask_server {
ip_hash;
server 0.0.0.0;
}


server {
listen 80;
server_tokens off;
server_name _;
root /flask_app/web;
index index.html index.htm index.nginx-debian.html;
charset utf-8;
client_max_body_size 75M;

 location / {
     #try_files $uri $uri/ =404;
     include uwsgi_params;
     uwsgi_pass unix:/tmp/flask.sock;
 }

 #location /static {
 #    alias /flask_app/static;
 #}

 location /flask/ {
     proxy_redirect off;
     proxy_pass http://flask_server;
     proxy_set_header Host $host;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 }

}

但是 index.html 没有加载,它只允许 api url(如:127.0.0.1/login)?

标签: nginxflaskuwsgi

解决方案


该指令告诉 Nginx 将与该位置块匹配的所有内容发送到上游服务器uwsgi_pass unix:/tmp/flask.sock

如果您只想提供 index.html 并将其他所有内容传递给上游,那么您可以像这样更改它:

 location / {
     try_files $uri/index.html @upstream;
     }

 location @upstream {
     include uwsgi_params;
     uwsgi_pass unix:/tmp/flask.sock;
     }

推荐阅读