跳至正文
  • 79 views
  • 3 min read

站点页面缓存开启方法

新浪微博 豆瓣 QQ 百度贴吧 QQ空间

wordpress 在站点健康检查的时候发现没开启缓存,具体信息如下:

 服务器响应时间的中位数是 807 毫秒,其应当小于推荐的 600 毫秒临界值。
未检测到客户端缓存响应标头。
未检测到页面缓存插件。

所以就想着把站点的缓存功能打开,但是本人又不喜欢安装一堆插件,所以就想着以修改配置的方法来开启。

要给网站开启缓存功能,首先需要确定自己使用的是 nginx 还是 apache,具体开启方法如下:

apache 环境的开启方法

修改根目录下的.htaccess 文件,添加如下内容:

# Enables browser caching
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
</IfModule>
nginx环境的开启方法

需要确定是否已经安装 ngx_cache_purge模块,检查方法如下:

root@debian:~# nginx -V
nginx version: nginx/1.22.0
built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
built with OpenSSL 1.1.1w 11 Sep 2023
TLS SNI support enabled
configure arguments: --prefix=/usr/lib/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=www --group=www --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-stream --with-stream=dynamic --with-stream_ssl_module --with-stream_realip_module --with-stream_ssl_preread_module --with-compat --with-pcre --with-pcre-jit --with-zlib-asm=CPU --with-debug --with-cc-opt='-O2 -g -pipe -fexceptions -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-E' --add-module=../ngx_brotli --add-module=../headers-more-nginx-module --add-module=../ngx_cache_purge

如果以上内容里面有 类似于 –add-module=../ngx_cache_purge的信息,就说明已经安装了,如果没有,需先加载这个模块。

然后在 nginx 配置文件中添加如下内容:

http {
  #http段添加如下内容
  fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=WORDPRESS:250m inactive=1d max_size=1G;
  fastcgi_temp_path /var/cache/nginx/temp;
  fastcgi_cache_key "$scheme$request_method$host$request_uri";
  fastcgi_cache_use_stale error timeout invalid_header http_500;
  fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
  server {
      #server 段添加如下内容
      set $skip_cache 0;
      if ($request_method = POST) {
          set $skip_cache 1;
      }
      if ($query_string != "") {
          set $skip_cache 1;
      }
      if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
          set $skip_cache 1;
      }
      if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
          set $skip_cache 1;
      }
      location ~ [^/]\.php(/|$) {
          #php段添加如下内容
          try_files $fastcgi_script_name =404;
          fastcgi_cache_bypass $skip_cache;
          fastcgi_no_cache $skip_cache;
          add_header X-Cache "$upstream_cache_status From $host";
          add_header Cache-Control max-age=0;
          add_header Nginx-Cache "$upstream_cache_status";
          add_header Last-Modified $date_gmt;
          add_header X-Frame-Options SAMEORIGIN;
          add_header X-Content-Type-Options nosniff;
          add_header X-XSS-Protection "1; mode=block";
          etag on;
          fastcgi_cache WORDPRESS;
          fastcgi_cache_valid 200 301 302 1d;
      }
  }
}

再次检查站点健康,发现缓存已成功启用

 服务器响应时间的中位数是 28 毫秒,小于推荐的 600 毫秒临界值。
检测到 1 个客户端缓存响应标头: last-modified.

发表回复