![](/img/trans.png)
[英]Magento 2 CentOS 7 nginx -> varnish -> apache -> php-fpm redirect loop
[英]Setting up Varnish on CentOS 7, Nginx and PHP-FPM with SSL
我以前没有使用过 Varnish,但我需要在我们的 Magento 站点上安装它以帮助加快速度。
我找到了很多关于如何在 Centos 7、PHP-FPM 等上设置 Varnish 的文章,但没有一篇文章能在 CentOS7、Nginx、PHP-FPM和SSL 上运行。 据我了解,Varnish 不能自然地与 SSL 一起使用,因此您需要做一些 Nginx jiggery-pokery 才能使事情正常工作。 这也是一个多商店的 Magento 站点,因此增加了另一层复杂性。
有没有人有任何信息可以帮助解决这个问题?
我将向您展示我自己的 Nginx 配置文件以使其有效。 这是 Debian 9 而不是 Centos 7,但 Nginx 应该以相同的方式工作。
如果有人有更好的配置或建议,我会仔细聆听...我是 Magento 开发人员而不是系统管理员。 我有很多关于 Nginx 和 Varnish 的东西要学习。
这里,Varnish 正在侦听端口 6081 。
/etc/nginx/sites-available/proxy.website.com
:## HTTPS termination & Varnish proxy
server {
server_name en.website.com fr.website.com es.website.com de.website.com;
listen 443 ssl http2;
access_log /var/www/log/varnish-proxy.log;
error_log /var/www/log/varnish-proxy.error.log;
include /etc/nginx/conf/ssl.conf;
keepalive_timeout 300s;
location / {
#BYPASS VARNISH
#proxy_pass http://127.0.0.1:611;
#VARNISH ENABLED
proxy_pass http://127.0.0.1:6081;
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 $scheme;
proxy_set_header X-Forwarded-Port 443;
proxy_set_header X-Secure on;
proxy_set_header X-Magento-Debug 1;
}
}
/etc/nginx/sites-available/website.com
虚拟主机:upstream fastcgi_backend { # USE YOUR OWN CONFIG HERE
# use tcp connection
# server 127.0.0.1:9000;
# or socket
server unix:/var/run/php7.1-fpm.sock;
}
map $http_host $MAGE_RUN_CODE_GLOBAL { # USE YOUR OWN CONFIG HERE
en.website.com en;
fr.website.com fr;
es.website.com es;
de.website.com de;
}
# Redirect to https
server {
server_name en.website.com fr.website.com es.website.com de.website.com;
listen 80;
location ~ /.well-known {
allow all;
}
return 301 https://$http_host$request_uri;
}
# Redirect to https
server {
server_name _;
listen 611;
set $MAGE_ROOT /var/www/magento;
set $MAGE_MODE developer;
set $MAGE_RUN_TYPE store;
set $MAGE_RUN_CODE $MAGE_RUN_CODE_GLOBAL;
set $HTTPS_FORWARD on;
set $FPM_USER www-data;
access_log /var/www/log/website.com.access.log;
error_log /var/www/log/website.com.error.log error;
include /var/www/magento/nginx.conf.sample;
}
sudo ln -s /etc/nginx/sites-available/proxy.website.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/website.com /etc/nginx/sites-enabled/
-t
将测试您的配置文件, -s reload
将重新加载 Nginx 配置而不中断服务:nginx -t && nginx -s reload
编辑 :
编辑清漆启动配置:
CentOS 6: /etc/sysconfig/varnish
CentOS 7: /etc/varnish/varnish.params
Debian/Ubuntu: /etc/default/varnish
...
## Alternative 2, Configuration with VCL
DAEMON_OPTS="-a :6081 \
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-S /etc/varnish/secret \
-s malloc,1024m \
-p workspace_backend=256 \
-p http_resp_hdr_len=42000"
...
在 Magento 管理员中:
设置Stores > Configuration > Advanced > System > Full Page Cache > Caching Application
to Varnish Cache
现在点击新的“清漆配置”归档
将Access list
和Backend host
为localhost 。 我不知道其他选项是什么。
保存配置更改
Clic 根据您的 Varnish 版本Export VCL
上传 Magento VCL
将默认清漆 VCL /etc/varnish/default.vcl
备份到/etc/varnish/default.vcl.bkp
将 magento VCL 放在一个新的/etc/varnish/default.vcl
文件中。
编辑第一行:
vcl 4.0; import std;
backend default {
.host = "127.0.0.1";
.port = "404";
}
backend mywebsite {
.host = "127.0.0.1";
.port = "611";
}
acl purge {
"localhost";
}
sub vcl_recv {
if (req.http.host ~ "website.com") {
set req.backend_hint = mywebsite;
} else {
set req.backend_hint = default;
}
...
有时,您必须处理特殊情况,例如为某些 URL 禁用 Varnish。
转到您的/etc/varnish/default.vcl
并根据需要编辑它。 第一次看到 VCL 的时候还挺晦涩的,但到最后也不难理解。
或者以这种方式编辑您的清漆代理:
## HTTPS termination & Varnish proxy
server {
...
location ^~ /sitemap {
#BYPASS VARNISH
proxy_pass http://127.0.0.1:611;
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 $scheme;
proxy_set_header X-Forwarded-Port 443;
proxy_set_header X-Secure on;
}
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.