【问题标题】:Force multiple websites with NGinx, Varnish and dedicated IP's from non-www to www强制使用 NGinx、Varnish 和专用 IP 的多个网站从非 www 到 www
【发布时间】:2016-10-18 00:18:54
【问题描述】:

我处理这个问题好几天了,需要帮助的伙伴们!我希望这里有人能解决这个问题:

3 个 Wordpress 网站(3 个专用 IP)

我已使用此 Ansible 剧本进行部署:https://github.com/zach-adams/hgv-deploy-full

这是全栈:

Ubuntu 14.04(专用服务器,无防火墙)

  • Percona DB (MySQL)
  • HHVM(默认 PHP 解析器)
  • PHP-FPM(备份 PHP 解析器)
  • Nginx Varnish(默认运行)
  • Memcached 和 APC
  • 干净的 WordPress 安装(最新版本)
  • WP-CLI

一切正常。我检查了标题并且 Varnish 正在工作。当我尝试在每个单独的站点中使用 Varnish 将 301 从非 www 强制为 www 时,它会进入 url 重定向循环。当我在一些文档中阅读时,我在 Nginx 配置中添加了这一行:

server {
    server_name example.com;
    return 301 $scheme://www.example.com$request_uri;
}

它不起作用,我认为它适用于 Varnish 配置。我遵循了其中的一些文档,但对我没有任何帮助:

我希望有人能照亮我:)

【问题讨论】:

    标签: php wordpress nginx varnish


    【解决方案1】:

    为了使用 Varnish 从非 www 重定向到 www,您需要执行以下操作:

    1- 禁用在 nginx 配置中编写的任何重定向规则。

    2- 使用以下 sn-p 通过 varnish 将非 www 重定向到 www:

    sub vcl_recv {
        if (req.http.host == "example.com") {
            set req.http.host = "www.example.com";
            error 750 "http://" + req.http.host + req.url;
        }
    }
    
    sub vcl_error {
        if (obj.status == 750) {
            set obj.http.Location = obj.response;
            set obj.status = 301;
            return(deliver);
        }
    }
    

    注意:我今天使用了上面的 sn-p,它对我来说工作正常(它适用于你问题中提到的剧本中使用的 Varnish 3)

    如果你想使用 nginx 重定向,你可以试试这个:

    server {
        listen 8080; # modify this with your current nginx port
        server_name  example.com;
        return 301 $scheme://www.example.com$request_uri;
    }
    server {
        listen 8080; # modify this with your current nginx port
        server_name www.example.com;
        root /path/to/example/files/;
        # ...the rest of your config file goes here...
    }
    

    仅使用 varnish 或 nginx 一种方法。如果您有 nginx 作为前端,我建议您使用 nginx 方式。

    【讨论】:

      猜你喜欢
      • 2011-12-18
      • 2011-05-01
      • 2014-11-26
      • 2013-03-28
      • 1970-01-01
      • 1970-01-01
      • 2013-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多