【问题标题】:Setting up Varnish on CentOS 7, Nginx and PHP-FPM with SSL使用 SSL 在 CentOS 7、Nginx 和 PHP-FPM 上设置 Varnish
【发布时间】:2019-02-07 14:07:23
【问题描述】:

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

我找到了很多关于如何在 Centos 7、PHP-FPM 等上设置 Varnish 的文章,但没有一篇文章可以在 CentOS7、Nginx、PHP-FPM AND SSL 上运行。 据我了解,Varnish 不能自然地与 SSL 一起使用,所以你需要做一些 Nginx jiggery-pokery 才能让事情正常工作。 这也是一个多商店 Magento 站点,因此增加了另一层复杂性。

有人有任何信息可以帮助解决这个问题吗?

【问题讨论】:

  • 代替 NGINX(SSL) - Varnish - NGINX 堆栈,您可能希望使用 Hitch for SSL,因为它可以使用 PROXY 协议与 Varnish 通信,从而无缝地向其传递客户端 IP 地址。跨度>

标签: magento nginx centos7 varnish


【解决方案1】:

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

如果有人有更好的配置或建议,我会仔细聆听...我是 Magento 开发人员而不是系统管理员。我有很多关于 Nginx 和 Varnish 的知识。

这里,Varnish 正在监听端口 6081

  1. 我创建了一个 Varnish 代理 来将 HTTPS 请求重定向到 HTTP varnish。在/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 中的虚拟主机:
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. 启用您的虚拟主机
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. 编辑 Varnish 启动配置:

    • 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 设置为清漆缓存

    • 现在点击新的“清漆配置”归档

    • Access listBackend host 设置为localhost。我不知道其他选项是什么。

    • 保存配置更改

    • 根据你的 Varnish 版本点击Export VCL

  2. 上传 Magento VCL

    • 将默认的varnish 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;
  }
...
}

【讨论】:

  • 安托万,非常感谢。我今天要尝试让它运行。您能否也分享您的 Varnish 配置?再次感谢。
  • 我也添加了我的 Varnish 配置。希望对你有帮助
  • 传奇。非常感谢您的帮助。
猜你喜欢
  • 2014-12-02
  • 1970-01-01
  • 2017-09-27
  • 2016-12-02
  • 1970-01-01
  • 2019-03-03
  • 1970-01-01
  • 2012-07-20
  • 2015-01-13
相关资源
最近更新 更多