Magento可以防止Varnish缓存页面

时间:2016-08-08 08:57:53

标签: php magento caching nginx varnish

如果您将标题更改为header('Cache-Control: public, max-age=10');,则完美无瑕。

但Magento 用自己的标题覆盖标题:Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0, s-maxage=86400

导致永不缓存的页面。但是你也不希望Varnish缓存登录的用户......那么我应该如何配置Varnish和/或Magento来缓存缓存所需的内容呢?

清漆4.0 (4.0.3-1)

PHP 5.6 (5.6.24-1)

Nginx 1.0.15 (1.0.15-12)

Magento 1.9 (1.9.2.4)

这是我的HTTP标头:

HTTP/1.1 200 OK
Server: nginx
Date:   Mon, 08 Aug 2016 08:46:28 GMT
Content-Type:   text/html; charset=UTF-8
X-Powered-By:   PHP/5.6.24
X-Frame-Options:    SAMEORIGIN
Cache-Control:  no-store, no-cache, must-revalidate, post-check=0, pre-check=0, s-maxage=86400
Expires:    Mon, 31 Mar 2008 10:00:00 GMT
Pragma: no-cache
Content-Encoding:   gzip
Vary:   Accept-Encoding
X-Varnish:  32770
Age:    0
Via:    1.1 varnish-v4
Content-Length: 0
Connection: keep-alive

我的Varnish vcl配置:

backend default {
  .host = "127.0.0.1";
  .port = "8080";
}
sub vcl_recv {
    unset req.http.cookie;
}
sub vcl_backend_response {
    unset beresp.http.set-cookie;
}
sub vcl_deliver {
 unset resp.http.set-cookie;
}

我的" nginx.conf"构造

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user              nginx;
worker_processes  4;

error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    server_tokens off;
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    fastcgi_read_timeout 300;

    #gzip  on;

    # Load config files from the /etc/nginx/conf.d directory
    # The default server is in conf.d/default.conf
    include /etc/nginx/conf.d/*.conf;

}

我的" default.conf"配置:

server {
    listen 127.0.0.1:8080;
    server_name  <my website, www.example.com>;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
        root   /var/www/public_html;
        index  index.php index.html index.htm;
        try_files $uri $uri/ @handler;
    }

    ## These locations would be hidden by .htaccess normally
   location ^~ /app/                { deny all; }
   location ^~ /includes/           { deny all; }
   location ^~ /lib/                { deny all; }
   location ^~ /media/downloadable/ { deny all; }
   location ^~ /pkginfo/            { deny all; }
   location ^~ /report/config.xml   { deny all; }
   location ^~ /var/                { deny all; }

   location /var/export/ { ## Allow admins only to view export folder
       auth_basic           "Restricted"; ## Message shown in login window
       auth_basic_user_file htpasswd; ## See /etc/nginx/htpassword
       autoindex            on;
   }

   location  /. { ## Disable .htaccess and other hidden files
       return 404;
   }

   location @handler { ## Magento uses a common front handler
       rewrite / /index.php;
   }

  location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
       rewrite ^(.*.php)/ $1 last;
  }

  # location ~ .php$ { ## Execute PHP scripts
  #     if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss
#       root           /var/www/public_html;
#       expires        off; ## Do not cache dynamic content
#       fastcgi_pass   127.0.0.1:9000;
#       fastcgi_index  index.php;
#       fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
#       fastcgi_param  MAGE_RUN_CODE default; ## Store code is defined in administration > Configuration > Manage Stores
#       fastcgi_param  MAGE_RUN_TYPE store;
#       include        fastcgi_params; ## See /etc/nginx/fastcgi_params
 #  }

   location ~ ^(.+\.php)(.*)$ {
    root /var/www/public_html;
    fastcgi_split_path_info ^(.+\.php)(.*)$;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME /var/www/public_html/$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_script_name;
   }

    error_page  404              /404.html;
    location = /404.html {
        root   /usr/share/nginx/html;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           /var/www/public_html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}

}

1 个答案:

答案 0 :(得分:0)

页面上可能有一些不可缓存的块。在* .xml文件中搜索... cacheable =“ false”,并考虑使块可缓存。

确保通过运行启用缓存也很有意义

bin/magento cache:status
相关问题