简体   繁体   English

为什么 Django 不在生产中提供静态文件?

[英]Why isn't Django serving staticfiles in production?

I am wondering the reason why Django does not serve the statifiles in production, when DEGUB = False .我想知道为什么 Django 在DEGUB = False时不提供生产中的状态文件的原因。

STATICFILES_DIRS STATICFILES_DIRS
We specify STATICFILES_DIRS to tell Django where to look for staticfiles that are tied up to a specified app.我们指定STATICFILES_DIRS来告诉 Django 在哪里寻找与指定应用程序相关的静态文件。

STATIC_ROOT静态根
We specify STATIC_ROOT to tell Django where to store the files once we run python manage.py collectstatic , so everystatic file is stored in the path specified in STATIC_ROOT .一旦我们运行python manage.py collectstatic ,我们指定STATIC_ROOT来告诉 Django 将文件存储在哪里,因此每个静态文件都存储在STATIC_ROOT中指定的路径中。
Assume that we set STATIC_ROOT = "staticfiles/" .假设我们设置STATIC_ROOT = "staticfiles/"
This means that once we run the collectstatic command, all the files that are inside STATICFILES_DIRS paths are going to be stored in "staticfiles/"这意味着一旦我们运行 collectstatic 命令, STATICFILES_DIRS路径中的所有文件都将存储在“staticfiles/”中

STATIC_URL Finally we specify STATIC_URL as "prefix" to tell Djando where to look for staticfiles, for example in the HTML <link> tag, the url that we see is based on STATIC_URL value STATIC_URL最后我们将STATIC_URL指定为“前缀”来告诉 Djando 在哪里寻找静态文件,例如在 HTML <link>标签中,我们看到的 url 是基于STATIC_URL

When we upload our project to the server, we upload the entire project, so every single file.当我们将项目上传到服务器时,我们会上传整个项目,因此每个文件。 Why can't Django serve staticfiles itself when running on server?为什么 Django 在服务器上运行时不能自己提供静态文件?
As I just said, we upload the entire folder, so the files we uploaded are there (and the staticfiles too.).正如我刚才所说,我们上传了整个文件夹,所以我们上传的文件在那里(还有静态文件。)。

QUESTIONS问题

  • I am just wondering, why do we have to specify the staticfiles based on server in production, when Django could do everything for us as it have always done in localhost?我只是想知道,为什么我们必须在生产中指定基于服务器的静态文件,而 Django 可以像在本地主机中一样为我们做所有事情?
  • Isn't load the files from another storage so much slower than load them from main folder of the project?从另一个存储加载文件不是比从项目的主文件夹加载文件慢得多吗?

I am just wondering, why do we have to specify the staticfiles based on server in production, when Django could do everything for us as it have always done in localhost?我只是想知道,为什么我们必须在生产中指定基于服务器的静态文件,而 Django 可以像在本地主机中一样为我们做所有事情?

Because it is likely inefficient and insecure .因为它可能效率低下且不安全 Each time a request is made, the request passes through all middleware then the view will produce a response that will again pass through the middleware to the client.每次发出请求时,请求都会通过所有中间件,然后视图将产生一个响应,该响应将再次通过中间件传递给客户端。 If you request the same file a second time, it will likely not have any caching, and thus repeat that process again.如果您第二次请求同一个文件,它可能没有任何缓存,因此再次重复该过程。 If you work with a webserver like Nginx/Apache, it will probably cache the result.如果您使用像 Nginx/Apache 这样的网络服务器,它可能会缓存结果。 If you work with a CDN, then it will also contact the nearest server and thus get access to these resources in a more efficient way.如果您使用 CDN,那么它还将联系最近的服务器,从而以更有效的方式访问这些资源。

Another problem is security.另一个问题是安全性。 If you specify a path to a file that is not supposed to be served, then the webserver should prevent the browser from accessing that file.如果您指定了不应提供的文件的路径,则网络服务器应阻止浏览器访问该文件。 Some hackers for example try to access the source files of the browser to then look for vulnerabilities.例如,一些黑客试图访问浏览器的源文件,然后寻找漏洞。 This should not be possible.这应该是不可能的。 Likely a web server like Apache or Nginx will have more advanced security mechanisms for this in place.可能像 Apache 或 Nginx 这样的 web 服务器将为此配备更高级的安全机制。

If you really want to, you can use WhiteNoise to let Django serve static files and media files in production.如果你真的想要,你可以使用WhiteNoise让 Django 在生产中服务 static 文件和媒体文件。 This Django application has been optimized for security and efficiency.此 Django 应用程序已针对安全性和效率进行了优化。 Although it is hard to tell if it will have the same level as aan Apache or Nginx server.虽然很难判断它是否与 Apache 或 Nginx 服务器具有相同的级别。

Isn't load the files from another storage so much slower than load them from main folder of the project?从另一个存储加载文件不是比从项目的主文件夹加载文件慢得多吗?

The webserver will not contact the other storage: the browser will do that.网络服务器不会联系其他存储:浏览器会这样做。 It thus is possible that instead of the webserver, it will contact a CDN.因此,它可能会联系 CDN,而不是网络服务器。 It is possible that this is slightly less efficient, since a webbrowser usually reuses the open connection to the server to make more requests, but often you already contacted that CDN, for example for JavaScript files.这可能效率稍低,因为网络浏览器通常会重用与服务器的开放连接来发出更多请求,但您通常已经联系过该 CDN,例如 JavaScript 文件。 Furthermore CDNs are optimized to deliver content as efficient as possible: the browser will usually contact a browerser close to the client, and usually there is also load balancing and redundancy in place to make it less likely that the server can no longer serve the resource.此外,CDN 被优化以尽可能高效地交付内容:浏览器通常会联系靠近客户端的浏览器,并且通常还会有负载平衡和冗余,以降低服务器不再提供资源的可能性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM