Varnish是一个功能强大且灵活的缓存HTTP反向代理。它可以安装在任何Web服务器的前方来缓存其内容,这将提高速度并减少服务器负载。当客户端请求网页时,Varnish首先尝试从缓存中发送它。如果页面未缓存,Varnish会将请求转发到后端服务器,获取响应,将其存储在缓存中,然后将其传递给客户端。
当通过Varnish请求缓存资源时,请求不会到达Web服务器,也不会涉及PHP或MySQL执行。相反,Varnish从内存中读取它,并在几微秒内转发缓存页面。
Varnish的一个缺点是它不支持SSL加密的流量。您可以使用NGINX作为后端Web服务器以及进行SSL解密来解决此问题。将NGINX用于这两项任务可降低设置的复杂性,从而减少潜在的故障点,降低资源消耗,并减少组件的维护力度。
Varnish和NGINX都是具有多种用途的多功能工具。本指南使用Varnish 4.0,它包含在Debian 8存储库中,并提供了一个基本设置,您可以根据自己的特定需求进行优化。
在本向导中,我们将为两个WordPress站点配置NGINX和Varnish:
www.example-over-http.com
将是一个未加密的,仅限HTTP的网站。www.example-over-https.com
将是一个单独的HTTPS加密站点。对于HTTP流量,Varnish将侦听端口80
。如果在缓存中找到了内容,Varnish将为其提供服务。如果没有,它会将请求传递给 8080
端口上的NGINX。在第二种情况下,NGINX会将请求的内容发送回同一端口上的Varnish,然后Varnish会将获取的内容存储在缓存中并通过通过80
端口将其传送到客户端。
对于HTTPS流量,NGINX将侦听端口443
并将解密的流量发送到端口上的Varnish 80
。如果在缓存中找到内容,Varnish会将未加密的内容从缓存发送回NGINX,NGINX会对其进行加密并将其发送给客户端。如果在缓存中找不到内容,Varnish将从8080
端口上的NGINX去请求数据,将其存储在缓存中,然后将其未加密地发送到前端NGINX,后者将对其进行加密并将其发送到客户端的浏览器。
我们的设置如下图所示。请注意,前端NGINX和后端NGINX是同一台服务器:
本教程假设您拥有对运行Debian 8(Jessie)的Linode的SSH访问权限。在开始之前:
sudo
,该帐户拥有本向导中许多命令的权限。对于本节中的所有步骤,将203.0.113.100
替换为您的Linodes公共IPv4地址,2001:DB8::1234
替换为IPv6地址。
sudo apt-get update sudo apt-get install varnish
/etc/default/varnish
。为了确保Varnish处于启动状态,在Should we start varnishd at boot?
下设置START
为yes
: /etc/default/varnish 1
START=yes
Alternative 2
部分中,进行以下更改为DAEMON_OPTS
:/etc/default/varnish 1 2 3 4 5
DAEMON_OPTS="-a :80 \ -T localhost:6082 \ -f /etc/varnish/custom.vcl \ -S /etc/varnish/secret \ -s malloc,1G"
这将设置Varnish监听80
端口并指示它使用custom.vcl
配置文件。使用自定义配置文件,以便将来对Varnish的更新不会覆盖default.vcl
。
用-s malloc,1G
命令行设置Varnish用于存储内容的最大RAM量。您可以根据服务器的总RAM量以及网站的大小和预期流量,按照您的需要来调整此值。例如,在有4 GB RAM的系统上,您可以为Varnish分配2或3 GB。
进行这些更改后,保存并退出该文件。
sudo touch /etc/varnish/custom.vcl
/etc/varnish/custom.vcl 1
vcl 4.0;
后端默认
指令指定后端(NGINX)监听8080
端口:/etc/varnish/custom.vcl 1 2 3 4
backend default { .host = "localhost"; .port = "8080"; }
acl
指令从localhost获得允许缓存清除请求:/etc/varnish/custom.vcl1 2 3 4 5
acl purger { "localhost"; "203.0.113.100"; "2001:DB8::1234"; }
请记住将您的Linode的实际IP地址替换为示例地址。
sub vcl_recv
例程,该例程会在HTTP客户端发送请求时使用。/etc/varnish/custom.vcl 1 2 3 4
sub vcl_recv { }
在下面的步骤的设置应放在sub vcl_recv
的括号里面:
/etc/varnish/custom.vcl 1 2 3 4
if (client.ip != "127.0.0.1" && req.http.host ~ "example-over-https.com") { set req.http.x-redir = "https://www.example-over-https.com" + req.url; return(synth(850, "")); }
请记住用您自己的域替换示例域。
acl purger
从IP地址中获得允许缓存清除请求。如果清除请求来自不同的IP地址,则会产生错误消息:/etc/varnish/custom.vcl 1 2 3 4 5 6
if (req.method == "PURGE") { if (!client.ip ~ purger) { return(synth(405, "This IP is not allowed to send PURGE requests.")); } return (purge); }
/etc/varnish/custom.vcl 1 2 3 4 5
if (req.restarts == 0) { if (req.http.X-Forwarded-For) { set req.http.X-Forwarded-For = client.ip; } }
/etc/varnish/custom.vcl 1 2 3
if (req.http.Authorization || req.method == "POST") { return (pass); }
/etc/varnish/custom.vcl 1 2 3
if (req.url ~ "/feed") { return (pass); }
/etc/varnish/custom.vcl 1 2 3
if (req.url ~ "wp-admin|wp-login") { return (pass); }
/etc/varnish/custom.vcl 1 2 3 4 5
set req.http.cookie = regsuball(req.http.cookie, "wp-settings-\d+=[^;]+(; )?", ""); set req.http.cookie = regsuball(req.http.cookie, "wp-settings-time-\d+=[^;]+(; )?", ""); if (req.http.cookie == "") { unset req.http.cookie; }
注意 这是放在sub vcl_recv
例程中的最终设置。以下步骤中的所有指令应放在最后一个括号之后。
sub vcl_synth
指令将HTTP重定向到HTTPS :/etc/varnish/custom.vcl 1 2 3 4 5 6 7
sub vcl_synth { if (resp.status == 850) { set resp.http.Location = req.http.x-redir; set resp.status = 302; return (deliver); } }
sub vcl_purge
指令:/etc/varnish/custom.vcl 1 2 3 4 5
sub vcl_purge { set req.method = "GET"; set req.http.X-Purger = "Purged"; return (restart); }
sub vcl_backend_response
指令用于处理与后端服务器NGINX的通信。我们使用它来设置在缓存中保留内容的时间量。我们还可以设置宽限期,它可以决定即使后端服务器关闭,Varnish如何从缓存中提供内容的时间。时间可以以秒(s),分钟(m),小时(h)或天(d)来设定。在这里,我们将缓存时间设置为24小时,将宽限期设置为1小时,但您可以根据需要调整这些设置:/etc/varnish/custom.vcl1 2 3
sub vcl_backend_response { set beresp.ttl = 24h; set beresp.grace = 1h;
vcl_backend_response
块之前,只有在管理页面或WooCommerce特定页面上时才允许设置cookie:/etc/varnish/custom.vcl1 2 3 4
if (bereq.url !~ "wp-admin|wp-login|product|cart|checkout|my-account|/?remove_item=") { unset beresp.http.set-cookie; } }
请记住在上面的系列中包含任何需要使用cookie的页面,例如phpmyadmin|webmail|postfixadmin
,等等。如果您将WordPress将登录页面wp-login.php更改为
其他页面时,请将该新名称添加到此系列中。
注意 “WooCommerce Recent Viewed”小部件可以显示一组最近查看过的产品,使用cookie来存储最近用户特定的操作,此cookie可防止Varnish在访问者浏览产品页面时缓存它们。如果要在仅浏览产品页面时缓存产品页面,则在将产品添加到购物车之前,必须禁用此窗口小部件。
如果您希望Varnish尽可能多地缓存页面,则需在启用使用cookie存储最近特定于用户的活动的小部件时特别注意。
sub vcl_deliver
指令来更改清除请求的标头:/etc/varnish/custom.vcl1 2 3 4 5
sub vcl_deliver { if (req.http.X-Purger) { set resp.http.X-Purger = req.http.X-Purger; } }
这样就完成了custom.vcl配置。您现在可以保存并退出该文件。最终custom.vcl文件将跟此文件相似。 注意 您可以使用上面的链接下载完整的示例配置文件wget。若要这样做,请记住如上所述替换变量。
/lib/systemd/system/varnish.service
文件以使用我们的自定义配置文件。具体来说,我们将告诉它使用自定义配置文件并修改端口号和分配的内存值以匹配我们在/etc/default/varnish
文件中所做的更改。
打开/lib/systemd/system/varnish.service
并找到开头ExecStart
的两行。将它们修改为如下所示:/lib/systemd/system/varnish.service 1 2
ExecStartPre=/usr/sbin/varnishd -C -f /etc/varnish/custom.vcl ExecStart=/usr/sbin/varnishd -a :80 -T localhost:6082 -f /etc/varnish/custom.vcl -S /etc/varnish/secret -s malloc,1G
sudo systemctl daemon-reload
在配置NGINX之前,我们必须安装PHP-FPM。FPM是FastCGI Process Manager的简称,它允许Web服务器充当代理,将带有.php
文件扩展名的所有请求传递给PHP解释器。
sudo apt-get install php5-fpm php5-mysql
/etc/php5/fpm/php.ini
文件。找到指令cgi.fix_pathinfo=
,取消注释并将其设置为0
。如果此参数设置为1
,PHP解释器将尝试处理其路径最接近请求路径的文件; 如果设置为0
,则解释器将仅使用确切路径处理文件,这是一个更安全的选项。/etc/php5/fpm/php.ini 1
cgi.fix_pathinfo=0
完成此更改后,保存并退出该文件。
/etc/php5/fpm/pool.d/www.conf
并确认listen =
指令(指定NGINX用于将请求传递给PHP-FPM的套接字)与以下内容匹配:/etc/php5/fpm/pool.d/www.conf 1
listen = /var/run/php5-fpm.sock
保存并退出该文件。
sudo systemctl restart php5-fpm
/etc/nginx/fastcgi_params
并找到fastcgi_param HTTPS
指令。在它下面,添加以下两行,这是NGINX与FastCGI服务交互所必需的:/ etc / nginx的/ fastcgi_params 1 2
fastcgi_param SCRIPT_FILENAME $request_filename; fastcgi_param PATH_INFO $fastcgi_path_info;
完成后,保存并退出文件。
/etc/nginx/nginx.conf
并注释掉ssl_protocols
和ssl_prefer_server_ciphers
指令。我们将在/etc/nginx/sites-enabled/default
文件中的服务器块中包含这些SSL设置:/etc/nginx/nginx.conf1 2
# ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE # ssl_prefer_server_ciphers on;
access_log
和error_log
指令:/etc/nginx/nginx.conf1 2
# access_log /var/log/nginx/access.log; # error_log /var/log/nginx/error.log;
保存并退出该文件。
www.example-over-http.com
。首先备份默认服务器块(虚拟主机)文件:sudo mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default-backup
/etc/nginx/sites-available/default
文件并添加以下块:/etc/nginx/sites-available/default 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
server { listen 8080; listen [::]:8080; server_name example-over-http.com; return 301 http://www.example-over-http.com$request_uri; } server { listen 8080; listen [::]:8080; server_name www.example-over-http.com; root /var/www/html/example-over-http.com/public_html; port_in_redirect off; index index.php; location / { try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; include fastcgi_params; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass unix:/var/run/php5-fpm.sock; } error_log /var/www/html/example-over-http.com/logs/error.log notice; }
这里有几点需要注意:
example-over-http.com
到www.example-over-http.com
。这假设您要使用www
子域并为其添加了DNS A记录。listen [::]:8080;
如果您希望您的站点也可以通过IPv6访问,则需要。port_in_redirect off;
阻止NGINX将端口号附加到请求的URL。fastcgi
指令用于通过FastCGI协议将PHP代码执行请求代理到PHP-FPM。www.example-over-https.com
),您需要另外两个服务器块。将以下服务器块附加到您的/etc/nginx/sites-available/default
文件:
/etc/nginx/sites-available/default 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
server { listen 443 ssl; listen [::]:443 ssl; server_name www.example-over-https.com; port_in_redirect off; ssl on; ssl_certificate /etc/nginx/ssl/ssl-bundle.crt; ssl_certificate_key /etc/nginx/ssl/example-over-https.com.key; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:20m; ssl_session_timeout 60m; add_header Strict-Transport-Security "max-age=31536000"; add_header X-Content-Type-Options nosniff; location / { proxy_pass http://127.0.0.1:80; proxy_set_header Host $http_host; proxy_set_header X-Forwarded-Host $http_host; 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 HTTPS "on"; access_log /var/www/html/example-over-https.com/logs/access.log; error_log /var/www/html/example-over-https.com/logs/error.log notice; } } server { listen 8080; listen [::]:8080; server_name www.example-over-https.com; root /var/www/html/example-over-https.com/public_html; index index.php; port_in_redirect off; location / { try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; include fastcgi_params; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param HTTPS on; fastcgi_pass unix:/var/run/php5-fpm.sock; } }
对于SSL加密的网站,您需要一个服务器块来接收端口443上的流量,并将解密的流量传递到端口上的Varnish 80
端口,以及另一个服务器块,以便8080
端口在Varnish请求时将未加密的流量提供给端口上的Varnish。
警告 ssl_certificate
指令必须指定SSL证书文件的位置和名称。请查看我们的指向导在NGINX上配置SSL以获取更多信息,并根据需要更新ssl_certificate
和ssl_certificate_key
值。
或者,如果您没有商业签名的SSL证书(由CA颁发),您可以使用openssl发出自签名SSL证书,但这只能用于测试目的。在浏览器中打开时,自签名站点将返回“此连接不受信任”消息。
现在,让我们回顾一下前两个服务器块的关键点:
ssl_session_cache shared:SSL:20m;
创建所有工作进程之间共享的20MB缓存。此缓存用于存储SSL会话参数,以避免并行和后续连接的SSL握手。1MB可以存储大约4000个会话,因此请根据您网站的预期流量调整此缓存大小。ssl_session_timeout 60m;
指定SSL会话高速缓存超时时间。这里设置为60分钟,但可以减少或增加,具体取决于流量和资源。ssl_prefer_server_ciphers on;
表示建立SSL连接时,服务器密码优先于客户端密码。add_header Strict-Transport-Security "max-age=31536000";
告诉Web浏览器,他们应该只使用安全的HTTPS连接与此服务器进行交互。在max-age
中以秒为单位指定哪个时间段该网站是愿意接受HTTPS只连接。add_header X-Content-Type-Options nosniff;
此标头告诉浏览器不要覆盖响应内容的MIME类型。因此,如果服务器说内容是文本,浏览器将把它呈现为文本。proxy_pass http://127.0.0.1:80;
该指令将所有解密的流量代理到Varnish,后者侦听端口80
。proxy_set_header
指令为请求添加特定标头,因此可以识别SSL流量。access_log
并error_log
指出相应类型日志的位置和名称。根据您的设置调整这些位置和名称,并确保www-data
用户有权修改每个日志。fastcgi
最后一个服务器块中存在的指令是通过FastCGI协议将PHP代码执行代理请求代理到PHP-FPM所必需的。/etc/nginx/sites-available/default 1 2 3 4 5 6 7
server { listen 8080 default_server; listen [::]:8080; server_name _; root /var/www/html; index index.html; }
该/var/www/html/index.html
文件可以包含一个简单的消息,如“找不到页面!”
sudo systemctl restart nginx sudo systemctl start varnish
sudo systemctl restart varnish
当您编辑WordPress页面并对其进行更新时,即使刷新浏览器也不会显示修改,因为它将收到页面的缓存版本。要在编辑页面时自动清除缓存页面,必须安装一个名为“Varnish HTTP Purge”的免费WordPress插件。
要安装此插件,请登录您的WordPress网站,然后单击左侧边栏上的插件。选择页面顶部的Add New,然后搜索Varnish HTTP Purge。找到后,单击立即安装,然后单击激活。
wget -SS http://www.example-over-http.com
输出应如下所示:
--2016-11-04 16:48:43-- http://www.example-over-http.com/ Resolving www.example-over-http.com (www.example-over-http.com)... your_server_ip Connecting to www.example-over-http.com (www.example-over-http.com)|your_server_ip|:80... connected. HTTP request sent, awaiting response... HTTP/1.1 200 OK Server: nginx/1.6.2 Date: Sat, 26 Mar 2016 22:25:55 GMT Content-Type: text/html; charset=UTF-8 Link: <http://www.example-over-http.com/wp-json/>; rel="https://api.w.org/" X-Varnish: 360795 360742 Age: 467 Via: 1.1 varnish-v4 Transfer-Encoding: chunked Connection: keep-alive Accept-Ranges: bytes Length: unspecified [text/html] Saving to: \u2018index.html.2\u2019 index.html [ <=> ] 12.17K --.-KB/s in 0s 2016-03-27 00:33:42 (138 MB/s) - \u2018index.html.2\u2019 saved [12459]
第三行指定连接端口号:80
。正确识别后端服务器:Server: nginx/1.6.2
。流量按预期通过Varnish : Via: 1.1 varnish-v4
。让通过Varnish将对象保存在缓存中的时间段也以秒为单位显示:Age: 467
。
wget -SS https://www.example-over-https.com
输出应类似于仅HTTP站点的输出。
注意 如果您在测试时使用自签名证书,请将该--no-check-certificate
选项添加到wget
命令:
wget -SS -no-check-certificate https://www.example-over-https.com
通过将nginx与Varnish结合使用,可以大大提高任何WordPress网站的速度,同时充分利用您的硬件资源。
您可以通过生成自定义Diffie-Hellman(DH)参数来增强SSL连接的安全性,以实现更安全的加密密钥交换过程。
另一个附加配置选项是为普通HTTP网站启用Varnish日志记录,因为现在Varnish将是第一个接收客户端请求的,而NGINX只接收对缓存中找不到的那些页面的请求。对于SSL加密的网站,日志记录应由NGINX完成,因为客户端请求首先通过它。如果使用Fail2ban,Awstats或Webalizer 等日志监控软件,日志记录将变得更加重要。
有关此主题的其他信息,您可能需要参考以下资源。虽然提供这些是希望它们有用,但请注意,我们无法保证外部托管材料的准确性或及时性。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有