配置Magento 2以在CentOS 7上使用Varnish
頁面速度或加載時間對於在線商店的成功至關重要。 加載時間是特定頁面上的內容加載所花費的總時間。 加載時間越長,轉換率越低。 這也是Google考慮確定搜索引擎排名的最重要因素之一。
在第一篇文章中,我們在CentOS 7機器上安裝了Magento 2。 在本系列的第二篇文章中,我們將介紹安裝和配置Varnish以使我們的Magento商店超快速。
先決條件
確保您已按照第一篇文章中的說明進行操作, EPEL
存儲庫已啟用。
這個怎麼運作 #
Varnish不支持SSL,因此我們需要使用其他服務作為SSL終止代理,在本例中為Nginx。
當訪問者通過 HTTPS
在港口 443
該請求將由Nginx處理,後者充當代理並將請求傳遞給Varnish(在端口80上)。 Varnish檢查是否緩存了請求。 如果已緩存,Varnish會將緩存的數據返回給Nginx,而無需請求Magento應用程序。 如果請求未緩存,Varnish會將請求傳遞給端口上的Nginx 8080
從Magento中提取數據,Varnish將緩存響應。
如果訪客打開您的網站而沒有 SSL
在港口 80
然後他將被重定向到 HTTPS
在港口 443
網址由Varnish提供。
配置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#
Varnish是一種快速的反向代理HTTP加速器,它位於我們的網絡服務器之前,將用作 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或具有sudo特權的用戶身份運行,它將創建一個文件 /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_LISTEN_PORT=80
完成修改後,啟動並啟用Varnish服務:
sudo systemctl enable varnish
sudo systemctl start varnish
您可以使用 varnishlog
查看實時Web請求和調試Varnish的工具。
結論#
在本教程中,我們向您展示了如何通過將Varnish實現為全頁緩存來加速Magento實例。
如果您遇到任何問題,請在下面發表評論。
magento電子商務centos清漆
這篇文章是如何在CentOS 7系列上安裝和配置Magento 2的一部分。本系列的其他文章:
•在CentOS 7上安裝Magento 2 2018年3月15日•在Magentto 2上配置在CentOS 7上使用Varnish 2018年3月27日