我如何利用浏览器缓存和 .htaccess 未被我的 varnish 配置清除?

How can I leverage browser cache and .htaccess are not cleared by my varnish configuration?

我想为以下可缓存资源利用浏览器缓存:JS/TTF/ICO/jpeg/png/css/...

我正在使用 GMETRIX 进行测试,但我在利用浏览器缓存时一直失败。

我认为我的 .htaccess 中的一些规则在我的 Varnish 配置中被取消了。所以我在下面包含了配置。

我已经包含了尽可能多的信息(并且允许在此处 post...)

如何确保我的 .htaccess 中的规则不会被我的清漆配置和静态文件缓存...?

这是对 Javascript 文件的 HTTP 请求:

 Request URL: https://www.domain1.com/media/extendware/ewminify/files/js/0747a032c3b39cac.js
 Request Method: GET
 Status Code: 200  (from memory cache)
 Remote Address: 77.243.239.145:443
 Referrer Policy: no-referrer-when-downgrade
 accept-ranges: bytes
 age: 20
 cache-control: max-age=28800
 content-encoding: gzip
 content-length: 200303
 content-type: application/x-javascript; charset=utf-8
 date: Fri, 04 Jan 2019 12:05:17 GMT
 etag: "b929d-57e158e64a31f-gzip"
 last-modified: Fri, 28 Dec 2018 14:05:31 GMT
 server: Apache
 status: 200
 strict-transport-security: max-age=15768000
 vary: Accept-Encoding
 via: 1.1 varnish-v4
 x-magento-cache-debug: HIT
 x-varnish: 4101659 2173998
 x-varnish-currency: EUR
 x-varnish-esi-access: 
 x-varnish-esi-method: 
 x-varnish-hits: 1
 x-varnish-host: www.domain1.com
 x-varnish-store: 
 x-varnish-url: 
 /media/extendware/ewminify/files/js/0747a032c3b39cac.js
 :authority: www.domain1.com
 :method: GET
 :path: /media/extendware/ewminify/files/js/0747a032c3b39cac.js
 :scheme: https
 accept: */*
 accept-encoding: gzip, deflate, br
 accept-language: en-GB,en;q=0.9,en-US;q=0.8,nl;q=0.7
 cache-control: no-cache
 cookie: __utmz=151xxxxx.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=         
 (none); frontend=xxxxxx; epc-initiated=1; _fbp=fb.1.xxxx.xx; 
  __utmc=xxxx; adminhtml=xxxxx; currency=EUR; 
  __utma=xxxx.xx.xxx.xxxx.xxxx.4; __utmt=1; __utmb=xxx.7.10.xxx
 pragma: no-cache
 referer: https://www.domain1.com/
 user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) 
 AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 
 Safari/537.36

.htacces

## default index file

DirectoryIndex index.php

<IfModule mod_php5.c>

## adjust memory limit

#    php_value memory_limit 64M
php_value memory_limit 512M
php_value max_execution_time 18000

## disable magic quotes for php request vars

php_flag magic_quotes_gpc off

## disable automatic session start
## before autoload was initialized

php_flag session.auto_start off

## enable resulting html compression

php_flag zlib.output_compression off

# disable user agent verification to not break multiple image upload

php_flag suhosin.session.cryptua off

# turn off compatibility with PHP4 when dealing with objects

php_flag zend.ze1_compatibility_mode Off

</IfModule>

<IfModule mod_security.c>
# disable POST processing to not break multiple image upload

SecFilterEngine Off
SecFilterScanPOST Off
 </IfModule>

# GZip compresssion

<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

<IfModule mod_deflate.c>

## enable apache served files compression
## http://developer.yahoo.com/performance/rules.html#gzip

# Insert filter on all content
###SetOutputFilter DEFLATE
# Insert filter on selected content types only
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css 
text/javascript

# Netscape 4.x has some problems...
BrowserMatch ^Mozilla/4 gzip-only-text/html

# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip

# MSIE masquerades as Netscape, but it is fine
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

# Don't compress images
#SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary

# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary

</IfModule>

<IfModule mod_headers.c>
Header set Connection keep-alive
</IfModule>

<IfModule mod_ssl.c>

## make HTTPS env vars available for CGI mode

SSLOptions StdEnvVars

</IfModule>

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/ [R=301,L]

RewriteCond %{HTTPS} on
 RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/ [R=301,L]

 <IfModule mod_rewrite.c>

## enable rewrites

Options +FollowSymLinks
RewriteEngine on

## you can put here your magento root folder
## path relative to web root

#RewriteBase /magento/

## uncomment next line to enable light API calls processing

#    RewriteRule ^api/([a-z][0-9a-z_]+)/?$ api.php?type= [QSA,L]

## rewrite API2 calls to api.php (by now it is REST only)

RewriteRule ^api/rest api.php?type=rest [QSA,L]

## workaround for HTTP authorization
   ## in CGI environment

RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

## TRACE and TRACK HTTP methods disabled to prevent XSS attacks

RewriteCond %{REQUEST_METHOD} ^TRAC[EK]
RewriteRule .* - [L,R=405]

## redirect for mobile user agents

#RewriteCond %{REQUEST_URI} !^/mobiledirectoryhere/.*$
#RewriteCond %{HTTP_USER_AGENT} 
"android|blackberry|ipad|iphone|ipod|iemobile|opera 
mobile|palmos|webos|googlebot-mobile" [NC]
#RewriteRule ^(.*)$ /mobiledirectoryhere/ [L,R=302]

## always send 404 on missing files in these folders

RewriteCond %{REQUEST_URI} !^/(media|skin|js)/

## never rewrite for existing files, directories and links

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

  ## rewrite everything else to index.php

 RewriteRule .* index.php [L]

 </IfModule>


## Prevent character encoding issues from server overrides
## If you still have problems, use the second line instead

AddDefaultCharset Off
#AddDefaultCharset UTF-8

<IfModule mod_expires.c>

## Add default Expires header
## http://developer.yahoo.com/performance/rules.html#expires

ExpiresDefault "access plus 1 year"
ExpiresActive On
ExpiresDefault A600
ExpiresByType image/x-icon A2592000
ExpiresByType application/x-javascript A604800
ExpiresByType text/css A604800
ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType text/plain A86400
ExpiresByType application/x-shockwave-flash A2592000
ExpiresByType video/x-flv A2592000
ExpiresByType application/pdf A2592000
ExpiresByType text/html A600
ExpiresByType text/js "access plus 1 years"
ExpiresByType text/javascript "access plus 1 years"
ExpiresByType application/javascript "access plus 1 years"
ExpiresByType application/x-javascript "access plus 1 years" 
### Expire everything else 1 day from when it's last modified
### (this uses the Alternative syntax)
ExpiresDefault "modification plus 1 day"

# Add correct content-type for fonts
AddType application/vnd.ms-fontobject .eot
AddType application/x-font-ttf .ttf
AddType application/x-font-opentype .otf
AddType application/x-font-woff .woff
AddType image/svg+xml .svg



 # Images
 ExpiresByType image/jpeg "access plus 1 year"
 ExpiresByType image/gif "access plus 1 year"
 ExpiresByType image/png "access plus 1 year"
 ExpiresByType image/webp "access plus 1 year"
 ExpiresByType image/svg+xml "access plus 1 year"
 ExpiresByType image/x-icon "access plus 1 year"

 # Video
 ExpiresByType video/mp4 "access plus 1 year"
 ExpiresByType video/mpeg "access plus 1 year"

 # CSS, JavaScript
 ExpiresByType text/css "access plus 1 month"
 ExpiresByType text/javascript "access plus 1 month"
 ExpiresByType application/javascript "access plus 1 month"

 # Others
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"



# Compress compressible fonts
AddOutputFilterByType DEFLATE application/x-font-ttf application/x- 
font-opentype image/svg+xml


# Add a far future Expires header for fonts
 ExpiresByType application/vnd.ms-fontobject "access plus 1 year"
 ExpiresByType application/x-font-ttf "access plus 1 year"
ExpiresByType application/x-font-opentype "access plus 1 year"
   ExpiresByType application/x-font-woff "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"



</IfModule>

 ## By default allow all access

Order allow,deny
Allow from all

    ## Deny access to release notes to prevent disclosure of the 
 installed Magento version

  <Files RELEASE_NOTES.txt>
    order allow,deny
    deny from all
  </Files>

  ## If running in cluster environment, uncomment this
  ## http://developer.yahoo.com/performance/rules.html#etags

  #FileETag none

/etc/varnish/default.vcl

         if (bereq.url ~ "^(/media/|/skin/|/js/|/)(?:(?:index|litespeed)\.php/)?") {
        unset beresp.http.Vary;
        set beresp.do_gzip = true;
        if (beresp.status != 200 && beresp.status != 404) {
            set beresp.ttl = 15s;
            set beresp.uncacheable = true;
            return (deliver);
        } else {
            if (beresp.http.Set-Cookie) {
                set beresp.http.X-Varnish-Set-Cookie = beresp.http.Set-Cookie;
                unset beresp.http.Set-Cookie;
            }
            //unset beresp.http.Cache-Control;
            //unset beresp.http.Expires;
           // unset beresp.http.Pragma;
           // unset beresp.http.Cache;
           // unset beresp.http.Age;
           // if (beresp.http.X-Turpentine-Esi == "1") {
                set beresp.do_esi = true;
            }
            if (beresp.http.X-Turpentine-Cache == "0") {
                set beresp.ttl = 15s;
                set beresp.uncacheable = true;
                return (deliver);
            } else {
                if (true &&
                        bereq.url ~ ".*\.(?:css|js|jpe?g|png|gif|ico|swf|mp4)(?=\?|&|$)") {
                    //unset beresp.http.set-cookie;
                    set beresp.ttl = 28800s;
                    set beresp.http.Cache-Control = "max-age=28800";
                } elseif (bereq.http.X-Varnish-Esi-Method) {
                    if (bereq.http.X-Varnish-Esi-Access == "private" &&
                            bereq.http.Cookie ~ "frontend=") {
                        set beresp.http.X-Varnish-Session = regsub(bereq.http.Cookie,
                                "^.*?frontend=([^;]*);*.*$", "");
                    }
                    if (bereq.http.X-Varnish-Esi-Method == "ajax" &&
                            bereq.http.X-Varnish-Esi-Access == "public") 
                    {
                        // Public ajax requests
                        set beresp.http.Cache-Control = "max-age=" + regsub(
                                bereq.url, ".*/ttl/(\d+)/.*", "");

                    }
                    set beresp.ttl = std.duration(
                            regsub(
                            bereq.url, ".*/ttl/(\d+)/.*", "s"),
                            300s);
                    if (beresp.ttl == 0s) {
                        set beresp.ttl = 15s;
                        set beresp.uncacheable = true;
                        return (deliver);
                    }
                } else {
                    set beresp.ttl = 3600s;
                }
            }
        }

set beresp.http.Cache-Control = "max-age=28800";

此行覆盖 1 年到期日期并将其设置为 8 小时。 GTmetrix 的文档说资源必须缓存至少 1 个月才能通过浏览器缓存测试。

set beresp.http.Cache-Control = "max-age=31556952"