php - 强制使用NGinx,Varnish和专用IP的多个网站从非www到www

标签 php wordpress nginx varnish

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

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

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

这是完整的堆栈:

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

  • Percona数据库(MySQL)
  • HHVM(默认PHP解析器)
  • PHP-FPM(备份PHP解析器)
  • Nginx Varnish(默认运行)
  • Memcached和APC
  • 清洁WordPress安装(最新版本)
  • WP-CLI

  • 一切正常。我检查了 header ,Varnish正常工作。当我尝试在各个站点中使用Varnish将301从非www强制转换为www时,它会进入url重定向循环。当我在一些文档中阅读时,我将此行添加到了Nginx配置中:
    server {
        server_name example.com;
        return 301 $scheme://www.example.com$request_uri;
    }
    

    它不起作用,我认为这是针对Varnish配置的。我遵循了一些文档,但对我没有任何帮助:
  • https://www.tinywp.in/varnish-301-redirection/
  • https://easyengine.io/tutorials/nginx/www-non-www-redirection/
  • https://www.digitalocean.com/community/tutorials/how-to-redirect-www-to-non-www-with-nginx-on-ubuntu-14-04

  • 我希望有人可以照亮我:)

    最佳答案

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

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

    2- 使用以下代码段通过 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);
        }
    }
    

    注意:我今天使用了上面的代码片段,它对我来说工作正常(它将与您问题中提到的剧本中使用的 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 方式。

    关于php - 强制使用NGinx,Varnish和专用IP的多个网站从非www到www,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37859907/

    相关文章:

    javascript - 将按钮添加到 tinymce

    php - 存储图形数据的最有效方法

    WordPress-按优先级从类别中获取帖子

    css - WordPress CSS 困惑

    php - 检查域名是否被注册

    php - 在MySQL中解析多维json字符串

    Facebook 点赞按钮的评论弹出窗口显示一半。

    nginx - 我丢失了nginx.pid,它消失了

    apache - NGINX 和 APACHE 在一起还是只有 NGINX?

    docker - 如何在容器中记录http请求和时间成本?