【问题标题】:Nginx and flask static directory - CSS not linking correctlyNginx 和烧瓶静态目录 - CSS 未正确链接
【发布时间】:2015-05-04 13:03:48
【问题描述】:

这是我的静态目录:

  • /var/www/myapp/app/static/css
  • /var/www/myapp/app/static/js
  • /var/www/myapp/app/static/img

在我的 nginx 配置中,我有这个:

location /static {
    alias /var/www/myapp/app/static;
    expires 30d;
    access_log off;
}

然后我在我的模板中使用这样的东西:

<img height="60" src="{{url_for('static', filename='img/logo.jpg')}}"></img>

但是,它不会渲染。当我查看在烧瓶下运行的 HTML 页面的源代码时,它呈现如下:

<img height="60" src="/static/img/logo.jpg">

我在这里做错了什么?

【问题讨论】:

  • 如果你尝试直接在浏览器中访问图片(或者是css?)会发生什么?
  • @PaulRooney 如果我访问以下两个网址中的任何一个,我会得到 404:www.site.com/static/css/style.css 或 www.site.com/css/style.css
  • 你的 nginx 服务器块中的根变量是否设置正确?

标签: python html css nginx flask


【解决方案1】:

nginx_server.conf 示例

    root  /var/www/website/;

# make path root + location `name` = root + /static/ = real path
location /static/  {
    alias /var/www/website/static/;
    expires 30d;

}

享受吧。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    • 2012-11-28
    • 2013-08-04
    • 1970-01-01
    相关资源
    最近更新 更多