【问题标题】:How to load static files in an Nginx + Flask setup if the app is mapped to a sub-directory如果应用程序映射到子目录,如何在 Nginx + Flask 设置中加载静态文件
【发布时间】:2015-01-10 15:31:13
【问题描述】:

我有一个在域(例如 example.com)上运行的基本 html 站点,我想在子目录(例如:example.com/test)中运行烧瓶应用程序 烧瓶应用程序使用默认烧瓶开发服务器在端口 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 错误),尽管有别名。

我该如何解决这个问题?

【问题讨论】:

  • 这不是子域。我的子域在你的域名之前开始。在您的情况下,它将是 test.exemple.com。在这种情况下,它永远不会起作用。

标签: nginx flask


【解决方案1】:

这是因为有一个额外的

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

}

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

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

它就像一个魅力

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-09
    • 2014-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多