Django 通过 CDN 无缝升级

发布于 2024-12-06 05:17:53 字数 710 浏览 0 评论 0原文

[我正在使用 AWS,但我认为这个问题与所有 CDN 相关]

我希望将我的 Django 服务器无缝部署到 AWS 云。
所有静态内容(例如图像、javascript 等)都会转到 Amazon Cloudfront CDN。
问题是我试图使升级尽可能“原子”,而我对 CDN 对象失效的时间几乎无法控制。
根据 TFM,解决方案是对我的对象进行版本控制,即添加版本 ID 来重命名它们,例如 arrow_v123.png。显然,如果新服务器指向 arrow_v124​​.png 我可以完全控制整个分发的时间。
我查了一下,据我所知,大佬们正在这样做——Facebook 静态内容对象有一个散列名称(和路径)。

但是我如何在 DJANGO 中自动执行此操作?

我需要以某种方式:

  • 生成新版本号
  • 更改所有静态对象的所有名称
  • 更改所有模板和 python 代码以使用这些新名称

或者以某种方式与开发过程集成:

  • 我编辑图片或 JavaScript 文件
  • ,保存它并它获得一个新名称?!?!并且对它的所有引用都会自动更正?!?!

我正在使用 Fabric 进行部署,所以我需要以某种方式修改我的 fabfile 是有意义的。

请帮忙。

塔尔。

[I'm using AWS but I think this question is relevant to all CDNs]

I'm looking to seamless deploy my Django server to the AWS cloud.
All static content (e.g. images, javascript, etc.) go to the Amazon Cloudfront CDN.
The problem is that I'm trying to make the upgrade as "atomic" as possible, while I have very little control over the timing of CDN object invalidation.
According to TFM, the solution is to version my objects, i.e. rename them adding a version id, e.g. arrow_v123.png. Obviously if the new server points to arrow_v124.png I have complete control over the timing of the entire distribution.
I checked and from what I can tell the big boys are doing that - Facebook static content objects have a hashed name (and path).

BUT HOW DO I AUTOMAGICALLY DO THIS IN DJANGO?

I need to somehow:

  • Generate a new version number
  • Change all the names of all the objects that are static
  • Change all the templates and python code to use those new names

Or somehow integrate with the development process:

  • I edit a picture or a javascript file
  • I save it and it gets a new name?!?! and all references to it are auto-corrected?!?!

I'm using Fabric for deployments, so it makes sense I need to modify my fabfile somehow.

Please help.

Tal.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

晚雾 2024-12-13 05:17:53

http://www.allbuttonspressed.com/projects/django-mediagenerator 提供资产版本控制。它提供了一个函数和一个模板标签,可以根据实际文件名提供版本化文件名。它还具有一些不错的附加功能,例如 js/css 缩小和串联。

http://www.allbuttonspressed.com/projects/django-mediagenerator provides asset versioning. It provides a function and a template tag to give you the versioned filename from the actual filename. It also has some nice extras like js/css minification and concatenation.

那些过往 2024-12-13 05:17:53

看看 html5boilerplate,它有 ant 构建脚本,它还可以执行许多其他操作,将静态 js/CSS 引用重命名为随机数,并将模板文件中的任何引用更改为这些新的随机编号 js/CSS。我不认为它对图像资源有同样的作用,但我认为可以更改 ant 文件以执行相同的操作。

html5boilerplate 的 Django 端口在这里: https://bitbucket.org/samkuehn/django-html5-boilerplate< /a>

Have a look at html5boilerplate, has ant build scripts which as well as doing a lot of other things rename your static js/CSS references to a random number and changes any references in your template files to these new random numbered js/CSS. I don't think it does the same for image assets but I suppose the ant files could be changed to perform the same.

Django port of html5boilerplate is here: https://bitbucket.org/samkuehn/django-html5-boilerplate

晨与橙与城 2024-12-13 05:17:53

在我看来,最后一部分(更改所有模板和 python 代码以使用新名称)很简单。将当前样式“版本号”存储在您的设置中。确保使用模板上下文将此版本号传递到您的模板。然后,在渲染模板时使用它来选择静态文件的真实 URL。

发布一组新的静态文件涉及的步骤是:

  1. 将新文件发布到您的 CDN
  2. 编辑您的设置文件
  3. 重新加载您的应用程序设置

通过将版本号存储在数据库中,您可以使用类似的方案实现相同类型的效果而不是在设置中。这避免了重新加载并且更改是立即的,但会在每个视图渲染时添加数据库查询。

如何将每个步骤集成到您的开发过程中取决于您。可以(有条件地)自动发布到 CDN、编辑设置文件并重新启动应用程序。要轻松“编辑”设置文件,您可以将版本号存储在单独的文件中,并让设置文件在启动时读取该版本号。

Seems to me the last part (change all templates and python code to use the new names) is easy. Store the current style "version number" in your settings. Make sure this version number is passed to your templates using the template context. Then, use that when rendering the templates to select the real URL to static files.

The steps involved in publishing a new set of static files is:

  1. publish the new files to your CDN
  2. edit your settings file
  3. re-load your application settings

You can achieve the same type of effect using a similar scheme by storing the version number in the database rather than in the settings. This avoids the reload and changes are immediate, but will add a DB query at each view render.

How you integrate each of these steps into your development process is up to you. It might be possible to (conditionally) automate publishing to the CDN, editing the settings file and restarting the application. To "edit" the settings file easily, you can store the version number in a separate file and have the settings file read that on start-up.

┼── 2024-12-13 05:17:53

我会在您经常更新的所有静态资源的末尾附加一个随机字符串。

示例: style.css 将变为 style.css?ASFDSF34343SDSFDSF

在调试模式下,我确保随机字符串在每次请求时都会更改,因此我的更改会立即生效。 (这可以防止浏览器缓存)

在生产中,随机字符串仅在启动时生成一次,然后重复使用。

您可以拥有它,以便在重新启动网络服务器后随机字符串也会保留下来。

无论是在开发过程中还是在生产过程中,这对我来说都非常有效。

请注意,我将资产保留在 S3 上,并且尚未启用 CloudFront。

这是实际示例:

http://static.outsourcefactor.com/css/main.css ?ASDFASFASF434434

I'd append a random string to the end of all the static assets you update often.

Example: style.css will become style.css?ASFDSF34343SDSFDSF

In debug mode, I ensure that the random string changes on each request, so my changes take effect right away. (this prevents browser caching)

In production, the random string is ONLY generated once on start-up and reused thereafter.

You can have it so the random string sticks around after a restart of your web server too.

This has been working very well for me, both during the development as well as in production.

Mind you that I keep my assets on S3 and have not enabled the CloudFront yet.

Here is the actual example:

http://static.outsourcefactor.com/css/main.css?ASDFASFASF434434

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文