应将应直接提供服务的静态文件放在服务器根目录下的何处? [英] Where to put static files that should be served directly under the server root?

查看:24
本文介绍了应将应直接提供服务的静态文件放在服务器根目录下的何处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚迁移了一个旧的Django项目以使用staticfiles应用程序.在此之前,我在名为static的目录中拥有所有需要的文件,这些文件直接在服务器根目录下提供.该目录现在可以在STATIC_URL下提供,这很好,除了应该直接在服务器根目录下提供的文件之外.

I just migrated an old Django project to make use of the staticfiles app. Before that i had all needed files in a directory called static that got served directly under the server root. This directory is now served under STATIC_URL which is fine, except for the files that should be served directly under the server root.

我知道如何直接从根目录提供文件(例如/favicon.ico或/robots.txt),但是我应该将它们放在哪里?如果我将它们放在STATIC_ROOT下的任何位置,则将通过两个URL(例如/file.txt和/static/foobar/file.txt)来提供服务,这不是一个好习惯.

I know how to serve files directly from root (like /favicon.ico or /robots.txt) but where should i put those? If i put them anywhere beneath STATIC_ROOT they will be served by two URLs (e.g. /file.txt and /static/foobar/file.txt) which is not good practice.

有什么想法吗?

推荐答案

将其保持在静态状态,并使您的网络服务器将/static/favicon.ico 重定向到/favicon.ico >.

Keep them in static and have your webserver redirect /static/favicon.ico to /favicon.ico.

要更完整地回答:

如果您有文件 favicon.ico ,则这是一个静态文件,因此应位于 STATIC_ROOT 中.但是,此文件是正常规则的例外,您不希望它存在于/static/favicon.ico 中,而希望它存在于/favicon.ico 中.由于这是该规则的例外,因此,您只为此文件添加一个特殊规则到您的Web服务器配置中,以便也可以在/favicon.ico 中使用该文件.

If you have the file favicon.ico, this is a static file and as such should exist inside of STATIC_ROOT. However this file is an exception to the normal rule and you do not want it to exist at /static/favicon.ico, you want it to exist at /favicon.ico. Since this is an exception to the rule, you add in a special rule just for this file to your webserver configuration so that it is also served at /favicon.ico.

现在,您拥有由2个不同的网址提供的相同资源,这是一件坏事.由于您不加限制地添加规则以使文件在/中提供,因此,我们假定这是规范的url,并告诉Web服务器重定向/static/favicon.ico /favicon.ico .现在,您拥有从一个位置提供的相同资源.

Now you have the same resource served by 2 different urls which is a bad thing. Since you went out of your way to add the rule to make your file served at /, We'll assume that this is the canonical url and tell the webserver to redirect /static/favicon.ico to /favicon.ico. Now you have the same resource, served from one location.

/static/根目录中的其他文件不会受到此影响,因为在您为 favicon.ico 在网络服务器中设置的规则中,您指定了 favicon.ico ,因为此文件(以及您要从/提供的任何其他文件)的特殊性质.

Other files in the root of /static/ will not be affected by this, because in the rules you setup in the webserver for favicon.ico, you specified favicon.ico because of the exceptional nature of this file (and any other file you want to serve from /).

这篇关于应将应直接提供服务的静态文件放在服务器根目录下的何处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆