OpenResty + Varnish Cache 实现 WP 的高性能加载教程

前言

Varnish Cache 是一款高性能的开源HTTP加速器,挪威最大的在线报纸 Verdens Gang 使用3台 Varnish 代替了原来的 12 台 Squid,性能比以前更好。(超级老的梗了,但是就这么用吧。)

Varnish Cache 在高端 WordPress 托管上也是非常常见的,由其构成的 Web 服务缓存加速解决方案已经非常成熟了,不过由于其开发者在 HTTPS 问题上并不关切所以很多人又抛弃了它采用了其他方案,不过前面也有提到 Varnish Cache 成熟、稳定,所以尽管不支持 HTTPS,我们也可以再加一层 Nginx 来反代实现 HTTPS。

介绍

Varnish Cache 的开发公司 Varnish Software 从 V5 时代开始了全新的命名方式,我们常提到的 Varnish 由 Varnish-Cache 的命名方式代替。

常见的 Varnish Cache 缓存规则从 V4 开始已经不同于 V3 了,不过 V5 依旧向下兼容 V4 的规则。

本教程主要介绍和前面的 https://www.mf8.biz/the-guide-for-wordpress-ubuntu/ 相呼应。

具体的教程可以参考:https://www.mf8.biz/varnish-wordpress-make-fast-1/

安装 Varnish 5

curl -s https://packagecloud.io/install/repositories/varnishcache/varnish5/script.deb.sh | bash
apt-get install varnish

更多系统的安装请看:

https://www.varnish-cache.org/releases/index.html https://packagecloud.io/varnishcache/varnish5/install

设置 Nginx

编辑虚拟主机的配置文件,将之前的 80 端口内容改成 8080 端口,其实就是将 listen 后改成 8080 ,例如:

  server {
  ##运行端口
  listen 8080; 

  ##这里需要改成你的域名
  server_name www.mf8.biz; 

  access_log /data/wwwlogs/access_nginx.log combined; #日志目录
  root /data/wwwroot/build; #网站文件目录
  index index.html index.htm index.php; #首页文件优先级
  include /usr/local/openresty/nginx/conf/rewrite/wordpress.conf;

  ##PHP
  location ~ [^/]\.php(/|$) {
      fastcgi_pass unix:/run/php/php7.1-fpm.sock;
      fastcgi_index index.php;
      include fastcgi.conf;
      fastcgi_param PHP_VALUE "open_basedir=$document_root:/tmp/:/proc/";
    }

  ##下面的都是缓存
  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
    expires 30d;
    access_log off;
    }
  location ~ .*\.(js|css)?$ {
    expires 7d;
    access_log off;
    }
  location ~ /\.ht {
    deny all;
    }
  }

同理,/usr/local/openresty/nginx/conf/nginx.conf 文件也必须将之前的 80 端口改成 8080 端口,其他所有在 Nginx 上打开的 80 端口都需要修改,不然 Varnish 无法启动。

然后将 HTTPS 部分,改成反代:

    server {

  ##开启 HTTPS 和 HTTP/2
  listen 443 ssl http2;

  ssl_certificate /usr/local/openresty/nginx/conf/ssl/www.mf8.biz.crt; #RSA证书
  ssl_certificate_key /usr/local/openresty/nginx/conf/ssl/www.mf8.biz.key; #RSA密钥

  ##SSL增强安全设置部分
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+ECDSA+AES128:EECDH+aRSA+AES128:RSA+AES128:EECDH+ECDSA+AES256:EECDH+aRSA+AES256:RSA+AES256:EECDH+ECDSA+3DES:EECDH+aRSA+3DES:RSA+3DES:!MD5;
  ssl_prefer_server_ciphers on;
  ssl_session_timeout 10m;
  ssl_session_cache builtin:1000 shared:SSL:10m;
  ssl_buffer_size 1400;
  ssl_stapling on;
  ssl_stapling_verify on;

  server_name www.mf8.biz;
  access_log off;

        location / {
            proxy_pass http://127.0.0.1:80;
            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto https;
            proxy_set_header X-Forwarded-Port 443;
            proxy_set_header Host $host;
        }
}

设置 Varnish

cd /etc/varnish/
mv default.vcl default.vclold
git clone https://gitee.com/mf8/varnish-caching-wordpress.git

cd /lib/systemd/system/
mv varnish.service varnish.service.old
wget https://gitee.com/yunvy/codes/d79nhgw2aitm8xky65p4399/raw?blob_name=varnish.service

cd /etc/default
mv varnish varnish.old
wget https://gitee.com/yunvy/codes/folh28bm9ipveagc04ws740/raw?blob_name=varnish

systemctl daemon-reload
service varnish restart

设置 WordPress

不过呢,由于我们是 Nginx HTTPS 443 端口 —— Varnish 80 端口 —— Nginx 80 端口 饶了三层,所以 WP 就会反应不过来,所有的静态资源的加载依旧走的 HTTP ,这里就需要额外设置一下了。

修改 wp-config.php 文件,加入:

if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')  $_SERVER['HTTPS']='on';

因为我们在 Nginx 443 反代的时候加入了 proxy_set_header X-Forwarded-Proto https; ,所以当 WP 检测 HTTP_X_FORWARDED_PROTO 有 https 反馈的时候就会将静态资源的加载自动走 HTTPS 了!

标签:Openresty 发布于:2019-11-05 03:45:14