【问题标题】:Pages aren't caching right, response headers seem wrong... how to fix?页面缓存不正确,响应标头似乎错误...如何解决?
【发布时间】:2011-06-07 18:06:22
【问题描述】:

好吧,我真的不知道如何提出这个问题,但我的问题或者至少我认为问题是这样的:

我正在处理的博客加载速度非常慢,所以我做了一些正常的事情来加速它,例如添加 gzip 和禁用所有插件等,但它没有帮助。因此,我查看了在 Firebug 中发送的标头,并注意到该页面的原始请求一直在等待,而其余请求正常加载。

这是给定页面的响应/请求标头:

请求标头

Host:               dev.mydomain.com
User-Agent:         Mozilla/5.0... ...Firefox/3.6.17
Accept:             text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language:    en-us,en;q=0.5
Accept-Encoding:    gzip,deflate
Accept-Charset:     ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive:         115
Connection:         keep-alive
Referer:            http://dev.mydomain.com/blog/2011/05/06/hello-world/
Cookie:             Cookie data...

响应标头

Date:               Tue, 07 Jun 2011 17:37:42 GMT
Server:             Apache
X-Pingback:         http://dev.mydomains.com/blog/xmlrpc.php
Expires:            Thu, 19 Nov 1981 08:52:00 GMT
Last-Modified:      Tue, 07 Jun 2011 17:37:59 GMT
Cache-Control:      no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma:             no-cache
Vary:               Accept-Encoding
Content-Encoding:   gzip
Content-Length:     4981
Connection:         close
Content-Type:       text/html; charset=UTF-8

显然这里出了点问题,因为没有缓存,连接设置为关闭,过期时间是 30 年前。

这是我设置过期标头等的 .htaccess 文件。

.htaccess

# compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/x-javascript

# Or, compress certain file types by extension:
<Files *.html|*.php>
SetOutputFilter DEFLATE
</Files>

ExpiresActive On
ExpiresDefault A0
ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/jpg A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/ico A2592000
ExpiresByType text/css A2592000
ExpiresByType text/javascript A2592000
ExpiresByType application/javascript A2592000
ExpiresByType application/javascript A2592000

FileETag none

AddType text/x-component .htc
AddType image/x-icon .ico 

AddHandler application/x-httpd-php .php .html

DirectoryIndex index.php
allow from all
RewriteEngine on

RewriteBase /  
RewriteCond %{REQUEST_URI} ^/blog/
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.+)$ /blog/index.php [L]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d

## Ignore CSS, Scripts and Images
RewriteRule !\.(xml|swf|js|ico|gif|jpg|png|css|htc)$ index.php [L]

如果非常感谢任何帮助。

顺便说一句,我在 CentOS 5 上的 PHP 5.2.10/MySQL 5.1 上运行 WordPress 3.1.1。*

【问题讨论】:

    标签: .htaccess http-headers mod-expires


    【解决方案1】:

    我正在与同样的问题作斗争,我发现一件事是您的 .htaccess 文件仅与您的网络主机提供的权限一样好。您是在托管自己的网站还是在网络托管公司?

    如果您托管自己的网站,则需要确保“允许覆盖”指令设置正确,以便 .htaccess 规则起作用。更好的是,设置 httpd.config 文件来为您完成这项工作。

    但是,如果您在网络托管公司工作,那么您需要询问他们可以使用哪些指令(您编写的那些 .htaccess 规则)?如果您的虚拟主机将 Allow Override 设置为 NONE,那么您的 .htaccess 文件将被忽略。

    我搜索过的所有地方都表明,在遥远的将来设置 Expires 标头是最好的:当它设置在过去(1981 年 11 月)时,它会强制浏览器每次获取一个新页面/图像。

    我建议的最后一件事是仔细检查您的 .htaccess 规则,这里有一组可能对您有用的起点:

    #BEGIN htaccess
    #Protect the htaccess file
    <Files .htaccess>
    Order Allow,Deny
    Deny from all
    </Files>
    
    #Protect the htaccess file
    <Files wp-config.php>
    Order Allow,Deny
    Deny from all
    </Files>
    
    # Disable directory browsing
    Options All -Indexes
    
    # Enable the following of symlinks
    Options +FollowSymLinks
    
    <IfModule mod_headers.c>
    # No ETags, No Pragma
    Header unset Pragma
    Header unset ETag
    # Make sure proxies deliver correct content
    Header append Vary User-Agent env=!dont-vary
    # Ensure proxies deliver compressed content correctly
    Header append Vary Accept-Encoding
    </IfModule>
    
    # Set up Cache Control headers
    ExpiresActive On
    # Default - Set http header to expire everything 1 week from last access, set must-revalidate
    ExpiresDefault A604800
    Header append Cache-Control: "max-age=3600, must-revalidate"
    # Apply a customized Cache-Control header to frequently-updated files
    <FilesMatch "^(test¦eval)\.html$">
    ExpiresDefault A1
    Header unset Cache-Control:
    Header append Cache-Control: "no-cache, must-revalidate"
    </FilesMatch>
    <FilesMatch "^robots\.txt$">
    ExpiresDefault A7200
    </FilesMatch>
    
    ExpiresByType image/x-icon A14515200
    
    # Set up caching on media files for 1 month
    <FilesMatch "\.(gif|jpg|JPG|jpeg|png|PNG|swf)$">
      ExpiresDefault A2419200
    </FilesMatch>
    
    # Set up caching on commonly updated files 1 month
    <FilesMatch "\.(xml|txt|html|js|css)$">
      ExpiresDefault A2419200
    </FilesMatch>
    
    <FilesMatch "\.(ico|gif|jpg|JPG|jpeg|png|PNG|css|js|html?|xml|txt)$">
    FileETag None
    </FilesMatch>
    
    <IfModule mod_deflate.c>
    <FilesMatch "\.(js|css|text|html)$">
    SetOutputFilter DEFLATE
    </FilesMatch>
    </IfModule>
    

    希望这对你有所帮助——祝你好运!

    光脚

    【讨论】:

    • 我会在接下来的几天内查看并通知您。感谢您的帮助。
    猜你喜欢
    • 2016-09-06
    • 2014-07-22
    • 2014-07-05
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 2020-02-24
    • 2014-06-30
    • 2015-12-13
    相关资源
    最近更新 更多