nginx - 如果应用程序映射到子目录,如何在 Nginx + Flask 设置中加载静态文件

标签 nginx flask

我有一个在域(例如 example.com)上运行的基本 html 站点,并且我想在子目录(例如:example.com/test)中运行 Flask 应用程序 Flask 应用程序使用默认的 Flask 开发服务器在端口 5433 上运行。

我使用以下 nginx 配置来实现此目的

location /test {
           rewrite /test(.*) $1 break;
           proxy_pass http://localhost:5433;
           proxy_redirect     default;
           proxy_set_header   Host             $host;
           proxy_set_header   X-Real-IP        $remote_addr;
           proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    }

    location /static/{
    alias /home/hrishi/www/example.com/test/app/static/;
    }

我可以使用 example.com/test/url 访问该应用程序,但尽管有别名,静态文件仍无法加载(给出 404 错误)。

我该如何解决这个问题?

最佳答案

这是因为有一个额外的

location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt)$ {
expires           max;

}

在服务器 block 内,我将其删除并通过

更改了静态目录的权限
chmod -R 664 /the/path/to/static/dir
chmod a+X /the/path/to/static/dir

它就像一个魅力

关于nginx - 如果应用程序映射到子目录,如何在 Nginx + Flask 设置中加载静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26909699/

相关文章:

nginx - 如果设置了 external_url,Gitlab docker 将无法工作

python-3.x - 没有名为 'urlparse' 的模块,但我没有使用 urlparse

python - 即使添加 __init__.py 后也无法导入模块

node.js - 如果 Node 关闭,如何在多个 Node 上处理 socketio

nginx - Docker Nginx 代理 : how to route traffic to different container using path and not hostname

phpinfo() 在 HHVM 中无法正常工作

python - 如何在 Flask-Admin 中将方法中的访问与 BaseModelView 类分开

python - Nginx 对 Django 应用程序响应 404,但静态文件可以工作

python - 我无法与 SQL 通信

flask - 可以使用 wtforms 来验证 GET 参数吗?