配置Magento 2以在CentOS 7上使用Varnish
页面速度或者加载时间对于在线商店的成功至关重要。
加载时间是特定页面上的内容加载所花费的总时间。
加载时间越长,转换率越低。
这也是Google考虑确定搜索引擎排名的最重要因素之一。
在第一篇文章中,我们在CentOS 7机器上安装了Magento 2.
在本系列的第二篇文章中,我们将介绍安装和配置Varnish以使我们的Magento商店超快速。
准备工作
确保我们已按照第一篇文章中的说明进行操作,并且已启用“ EPEL”存储库。
这个怎么运作
Varnish不支持SSL,因此我们需要使用其他服务作为SSL终止代理,在本例中为Nginx。
当访问者通过端口“ 443”上的“ HTTPS”打开时,该请求将由Nginx处理,该代理将作为代理并将请求传递给Varnish(在端口80上)。
Varnish检查请求是否被缓存。
如果已缓存,Varnish会将缓存的数据返回给Nginx,而无需向Magento应用程序发出请求。
如果请求未缓存,Varnish会将请求传递到端口“ 8080”上的Nginx,这将从Magento中提取数据,并且Varnish将缓存响应。
如果访问者在端口“ 80”上打开不带“ SSL”的,那么Varnish会将其重定向到端口“ 443” URL上的“ HTTPS”。
配置Nginx
我们需要编辑在第一篇文章中创建的Nginx服务器块,以处理SSL/TLS终止并作为Varnish的后端。
/etc/nginx/conf.d/example.com.conf
upstream fastcgi_backend { server unix:/run/php-fpm/magento.sock; } server { listen 127.0.0.1:8080; server_name example.com www.example.com; set $MAGE_ROOT /opt/magento/public_html; set $MAGE_MODE developer; # or production include snippets/letsencrypt.conf; include /opt/magento/public_html/nginx.conf.sample; } server { listen 443 ssl http2; server_name www.example.com; ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem; include snippets/ssl.conf; return 301 https://example.com$request_uri; } server { listen 443 ssl http2; server_name example.com; ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem; include snippets/ssl.conf; access_log /var/log/nginx/example.com-access.log; error_log /var/log/nginx/example.com-error.log; location/{ proxy_pass http://127.0.0.1; 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 X-Forwarded-Port 443; } }
我们还需要从“ nginx.conf”文件中删除默认的Nginx服务器块。
注释或者删除以下几行:
/etc/nginx/nginx.conf
... # server { # listen 80 default_server; # listen [::]:80 default_server; # server_name _; # root /usr/share/nginx/html; ## # Load configuration files for the default server block. # include /etc/nginx/default.d/*.conf; ## location/{ # } ## error_page 404 /404.html; # location = /40x.html { # } ## error_page 500 502 503 504 /50x.html; # location = /50x.html { # } # } ...
重新加载Nginx服务以使更改生效:
sudo systemctl reload nginx
安装和配置Varnish
Varnish是一种快速的反向代理HTTP加速器,它位于我们的Web服务器之前,将用作Magento安装的“全页缓存”解决方案。
使用以下命令通过yum安装Varnish:
sudo yum install varnish
要将Magento配置为使用Varnish,请运行:
php /opt/magento/public_html/bin/magento config:set --scope=default --scope-code=0 system/full_page_cache/caching_application 2
接下来,我们需要生成一个Varnish配置文件:
sudo php /opt/magento/public_html/bin/magento varnish:vcl:generate > /etc/varnish/default.vcl
上面的命令需要以root或者具有sudo特权的用户身份运行,它将使用默认值“ localhost”作为后端主机和端口“ 8080”来创建文件“ /etc/varnish/default.vcl”作为后端端口。
默认配置附带的健康检查文件URL错误。
打开“ default.vcl”文件,并从以黄色突出显示的行中删除“/pub”部分:
/etc/varnish/default.vcl
... .probe = { # .url = "/pub/health_check.php"; .url = "/health_check.php"; .timeout = 2s; .interval = 5s; .window = 10; .threshold = 5; } ...
默认情况下,Varnish监听端口'6081',我们需要将其更改为'80':
/etc/varnish/varnish.params
VARNISH_LISTEN_PORT=80
完成修改后,启动并启用Varnish服务:
sudo systemctl enable varnishsudo systemctl start varnish
我们可以使用“ varnishlog”工具查看实时Web请求并调试Varnish。