1

我是 gzip 压缩和缓存等优化技术的新手。在网上进行了一些研究后,我意识到这可以通过基于一些 apache 处理程序的 .htaccess 来实现。我曾询问我的虚拟主机以了解 mod_deflate 和 mod_headers 库是否存在且可用

我什至通过萤火虫检查。它显示请求标头“Accept-Encoding gzip,deflate”,但响应字段中没有“Content-encoding”。有人可以帮我解决哪里出错了吗?

以下是 .htaccess 文件的代码

            Options -Indexes
            Options +FollowSymlinks
            RewriteEngine on

            RewriteBase /
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule . /index.php [L]

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


            # ----------------------------------------------------------------------
            # Expires headers (for better cache control)
            # ----------------------------------------------------------------------

            # These are pretty far-future expires headers.
            # They assume you control versioning with filename-based cache busting
            # Additionally, consider that outdated proxies may miscache
            # www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/

            # If you don't use filenames to version, lower the CSS and JS to something like
            # "access plus 1 week" or so.

            <IfModule mod_expires.c>
              ExpiresActive on

            # Perhaps better to whitelist expires rules? Perhaps.
              ExpiresDefault "access plus 1 month"

            # cache.appcache needs re-requests in FF 3.6 (thanks Remy ~Introducing HTML5)
              ExpiresByType text/cache-manifest "access plus 0 seconds"

            # Your document html
              ExpiresByType text/html "access plus 0 seconds"

            # Data
              ExpiresByType text/xml "access plus 0 seconds"
              ExpiresByType application/xml "access plus 0 seconds"
              ExpiresByType application/json "access plus 0 seconds"

            # Feed
              ExpiresByType application/rss+xml "access plus 1 hour"
              ExpiresByType application/atom+xml "access plus 1 hour"

            # Favicon (cannot be renamed)
              ExpiresByType image/x-icon "access plus 1 week"

            # Media: images, video, audio
              ExpiresByType image/gif "access plus 1 month"
              ExpiresByType image/png "access plus 1 month"
              ExpiresByType image/jpg "access plus 1 month"
              ExpiresByType image/jpeg "access plus 1 month"
              ExpiresByType video/ogg "access plus 1 month"
              ExpiresByType audio/ogg "access plus 1 month"
              ExpiresByType video/mp4 "access plus 1 month"
              ExpiresByType video/webm "access plus 1 month"

            # HTC files (css3pie)
              ExpiresByType text/x-component "access plus 1 month"

            # Webfonts
              ExpiresByType application/x-font-ttf "access plus 1 month"
              ExpiresByType font/opentype "access plus 1 month"
              ExpiresByType application/x-font-woff "access plus 1 month"
              ExpiresByType image/svg+xml "access plus 1 month"
              ExpiresByType application/vnd.ms-fontobject "access plus 1 month"

            # CSS and JavaScript
              ExpiresByType text/css "access plus 1 year"
              ExpiresByType application/javascript "access plus 1 year"

            </IfModule>
4

1 回答 1

1

您确定您的主机提供商启用了这两个模块吗?您是否检查过托管在同一提供商中的另一个网站?有时小型主机提供商会关闭共享计划上的 mod_deflate 以节省一些 CPU,从而增加共享。

尝试 curl 您网站上的任何 js 资源,例如:

curl -I "http://yslow.org/yslow-bookmarklet.js" -H "Accept-Encoding: gzip, deflate"

你应该得到类似的东西:

HTTP/1.1 200 OK
Server: nginx/1.0.13
Date: Wed, 06 Jun 2012 18:31:25 GMT
Content-Type: application/x-javascript
Last-Modified: Fri, 25 May 2012 16:11:28 GMT
Connection: keep-alive
Expires: Thu, 07 Jun 2012 18:31:25 GMT
Cache-Control: max-age=86400
Content-Encoding: gzip

寻找以Content-Encoding确保mod_deflate是 ONExpires以及mod_expiresCache-Control

于 2012-06-06T18:40:14.727 回答