页面速度或加载时间对于在线商店的成功至关重要。加载时间是加载特定页面上的内容所需的总时间。加载时间越长,转化率越低。这也是谷歌考虑确定搜索引擎排名的最重要因素之一。
在第一篇文章中,我们在 CentOS 7 机器上安装了 Magento 2。在这篇文章的第二篇文章中series,我们将介绍安装和配置 Varnish 以使我们的 Magento 商店速度超快。
先决条件#
确保您已遵循第一篇文章的说明你有EPEL
存储库已启用。
怎么运行的#
Varnish 不支持 SSL,因此我们需要使用另一个服务作为 SSL 终止代理,在我们的例子中是Nginx
.
当访问者打开您的网站时HTTPS
在港口443
该请求将由 Nginx 处理,Nginx 作为代理并将请求传递给 Varnish(在端口 80 上)。 Varnish 检查请求是否被缓存。如果被缓存,Varnish 会将缓存的数据返回到 Nginx,而不需要向 Magento 应用程序发出请求。如果请求没有被缓存,Varnish 会将请求传递到端口上的 Nginx8080
它将从 Magento 中提取数据,而 Varnish 将缓存响应。
如果访问者打开您的网站时没有SSL
在港口80
然后他将被重定向到HTTPS
在港口443
清漆的网址。
配置 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 服务器块中删除nginx.conf
文件。注释或删除以下行:
/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 是一个快速反向代理 HTTP 加速器,它将位于我们的 Web 服务器前面,并将用作Full Page Cache
我们的 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 或用户身份运行须藤权限它会创建一个文件/etc/varnish/default.vcl
使用默认值localhost
作为后端主机和端口8080
作为后端端口。
默认配置带有错误的运行状况检查文件 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 服务:
sudo systemctl enable varnish
sudo systemctl start varnish
您可以使用varnishlog
用于查看实时 Web 请求和调试 Varnish 的工具。
结论#
在本教程中,我们向您展示了如何通过将 Varnish 实现为全页缓存来加速 Magento 实例。
如果您遇到任何问题,请在下面发表评论。