使用SSL在CentOS 7、Nginx和PHP-FPM上设置Varnish



我以前没有使用过Varnish,但我需要将它安装在我们的Magento网站上,以帮助加快速度。

我发现了很多关于如何在Centos 7、PHP-FPM等上设置Varnish的文章,但没有一篇能与Centos、Nginx、PHP-FPMANDSSL一起运行。据我所知,Varnish不适合使用SSL,所以你需要做一些Nginx的小把戏来让事情正常工作。这也是一个多商店的Magento网站,因此增加了另一层复杂性。

有人有什么信息可以帮忙吗?

我将向您展示我自己的Nginx配置文件,以使其发挥作用。这是Debian 9而不是Centos 7,但Nginx应该以同样的方式工作。

如果有人有更好的配置或建议,我会认真倾听。。。我是一个Magento开发人员而不是系统管理员。关于Nginx&清漆

此处,Varnish正在侦听端口6081

  1. 我创建了一个Varnish代理来将HTTPS请求重定向到HTTP清漆。在/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;
}
}
  1. 然后,我在/etc/nginx/sites-available/website.com中的vhost:
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;
}
  1. 启用您的vhosts
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/
  1. 重新启动nginx。-t将测试您的配置文件,-s reload将在不中断服务的情况下重新加载Nginx配置:
nginx -t && nginx -s reload

编辑:

  1. 编辑清漆启动配置:

    • 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"
...
  1. 在Magento管理:

    • Stores > Configuration > Advanced > System > Full Page Cache > Caching Application设置为清漆缓存

    • 现在clic上新的"清漆配置"文件

    • Access listBackend host设置为localhost。我不知道还有什么选择。

    • 保存配置更改

    • ClicExport VCL根据您的Varnish版本

  2. 上传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;
}
...
  1. 有时,您将不得不处理特殊情况,如禁用某些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;
}
...
}

最新更新