YSlow Django 应用最佳实践,如何实现?

发布于 2024-08-10 13:57:43 字数 775 浏览 2 评论 0原文

我有一个 django 1.1.1 应用程序,实际上正在开发中,在最佳实践中考虑我运行了 YSlow 测试(应用了 E 级规则集: YSlow V2 ),它推荐:

<块引用>

“添加过期”标头的等级为 F

-共有 37 个静态组件,没有远期到期日期。

使用内容分发网络 (CDN) 的 F 级

-有 37 个静态组件不在 CDN 上。

使用 gzip 压缩组件的 F 级

-有 17 个纯文本组件应压缩发送

如何使用 Django 实现它?

更多上下文:Python 2.5,部署在 webfaction

示例:

<块引用>

F 级,减少 HTTP 请求

此页面有 14 个外部 Javascript 脚本。尝试将它们合二为一。 此页面有 4 个外部样式表。尝试将它们合并为一个。

可以使用Django-Compress解决

I have an django 1.1.1 app, actually in developement, thinking in best practices I ran the YSlow test (Grade E Ruleset applied: YSlow V2 ) it recomends:

Grade F on Add Expires headers

-There are 37 static components without a far-future expiration date.

Grade F on Use a Content Delivery Network (CDN)

-There are 37 static components that are not on CDN.

Grade F on Compress components with gzip

-There are 17 plain text components that should be sent compressed

How can I implement it with Django?

More context: Python 2.5, deployment at webfaction

Example:

Grade F on Make fewer HTTP requests

This page has 14 external Javascript scripts. Try combining them into one.
This page has 4 external stylesheets. Try combining them into one.

Can be solved with Django-Compress

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

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

发布评论

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

评论(3

千寻… 2024-08-17 13:57:43

在您列出的三个中,有两个可在 Web 服务器级别寻址。例如,在 Linux/Apache 中:

对于 gzip,编辑 /etc/apache2/mods-available/deflate.conf

<IfModule mod_deflate.c>  
    AddOutputFilterByType DEFLATE text/html text/plain text/xml application/x-javascript text/javascript text/css application/javascript  
</IfModule>  

对于 Expires 标头,首先需要启用 mod_expires:

>cd /etc/apache2
>sudo ln -s ../mods-available/expires.load mods-enabled/expires.load

然后需要为所需的 MIME 类型配置它:

# edit /etc/apache2/sites-available/default  
ExpiresActive On  
ExpiresByType text/css "access plus 12 hours"  
ExpiresByType application/javascript "access plus 12 hours"  
ExpiresByType image/png "access plus 12 hours"  
ExpiresByType image/gif "access plus 12 hours"  

Writeup on为什么我建议此处 12 小时。

最后一项 (CDN) 通常是您外包给 Akamai 等公司的内容。它也相当昂贵。

Of the three you listed, two are addressable at the web-server level. For example, in Linux/Apache:

For gzip, edit /etc/apache2/mods-available/deflate.conf

<IfModule mod_deflate.c>  
    AddOutputFilterByType DEFLATE text/html text/plain text/xml application/x-javascript text/javascript text/css application/javascript  
</IfModule>  

For Expires headers, first you need to enable mod_expires:

>cd /etc/apache2
>sudo ln -s ../mods-available/expires.load mods-enabled/expires.load

Then you need to configure it for the MIME types you want:

# edit /etc/apache2/sites-available/default  
ExpiresActive On  
ExpiresByType text/css "access plus 12 hours"  
ExpiresByType application/javascript "access plus 12 hours"  
ExpiresByType image/png "access plus 12 hours"  
ExpiresByType image/gif "access plus 12 hours"  

Writeup on why I recommend 12 hours here.

The last item (CDN) is typically something you outsource to someone like Akamai. It's also quite expensive.

太阳哥哥 2024-08-17 13:57:43

我非常同意你的观点,蔡斯。我根据 Steve Souders 和 Nicholas Zakas 的工作开发了一些 django 程序。

我的标准做法是:

  • 安装 django-mediagenerator,它是以下功能的基础:
    • 将 JS 和 CSS 与媒体生成器包结合起来
    • 配置 mediagenerator 以使用 google-closure 自动压缩 JS,使用 YUICompressor 自动压缩 CSS
    • 所有静态媒体(未上传)也由媒体生成器处理。这允许您使用远期过期标头,例如 10 年。
  • 尽量减少图像的使用,并尝试仅使用 CSS 来生成网站的大部分内容。
  • 在我必须使用小图像的地方,将它们以 base64 内联在 CSS 文件中(使用 mediagenerator)
  • 在它们是大图像的地方,将它们组合成一个精灵(尽管我很少使用大图像)
  • Nginx 和 Gunicorn 用于部署

Nginx gzip 配置在 nginx.conf 中:

gzip_http_version 1.1;
gzip_vary on;
gzip_comp_level 6;
gzip_proxied any;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_buffers 64 16k;
gzip_disable “MSIE [1-6].(?!.*SV1)”;

Nginx 远期过期标头:

location /media/ {
    alias   /home/domains/example.com/project/_generated_media/;
    expires 10y;
    add_header Cache-Control public;
}   

它们唯一剩下的项目是图像上传,在远期过期后仍然可以使用,因为当图像更改时,文件名也应该更改。

使用这些技术,我创建了具有 3 个 http 请求的网站。页面呈现后,html 文件、页眉中的一个 CSS 请求和页脚中的一个 JS 请求。

I agree very much with you Chase. I've developed some django procedures based a lot on the work of Steve Souders and Nicholas Zakas.

My standard practices are:

  • Install django-mediagenerator which is the base for:
    • Combine JS and CSS with mediagenerator bundles
    • Configure mediagenerator to automatically compress JS with google-closure and CSS with YUICompressor
    • ALL static media (non uploaded) is handeled by mediagenerator too. That allows you to use a far-future expires header like 10 years.
  • Minimize the use of images and attempt to produce most of the site in CSS only.
  • Where I must use small images, base64 inline them in the CSS files (using mediagenerator)
  • Where they are large images, combine them into a sprite (I use large images very infrequently though)
  • Nginx and gunicorn for deployment

Nginx gzip config in nginx.conf:

gzip_http_version 1.1;
gzip_vary on;
gzip_comp_level 6;
gzip_proxied any;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_buffers 64 16k;
gzip_disable “MSIE [1-6].(?!.*SV1)”;

Nginx far futures expires header:

location /media/ {
    alias   /home/domains/example.com/project/_generated_media/;
    expires 10y;
    add_header Cache-Control public;
}   

They only items left over are image uploads which can still be used with far future expires, because when the image changes the filename should change too.

Using those techniques I have produced sites with 3 http requests. The html file, one CSS request in the header, and one JS request in the footer after the page has rendered.

谜兔 2024-08-17 13:57:43

这些都与 Django 无关,因为 YSlow 指的是您网站上的静态资产 - JS、CSS 和图像。通过开发服务器提供这些服务必然会导致成绩不及格,但做得更好将取决于您如何配置最终提供这些服务的真实服务器。

None of these have anything to do with Django, since YSlow is referring to the static assets on your site - JS, CSS and images. Serving these through the development server is bound to lead to failing grades, but doing better will depend on how you configure the real server that ends up serving these.

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