dounuogong0358 2016-06-16 12:55
浏览 68

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

I'm dealing with this for days and need help fellows! I hope somebody here han solve this:

3 Wordpress websites (3 dedicated IP's)

I've used this Ansible playbook to deploy: https://github.com/zach-adams/hgv-deploy-full

This is the fullstack:

Ubuntu 14.04 (Dedicated Server, no firewall)

  • Percona DB (MySQL)
  • HHVM (Default PHP Parser)
  • PHP-FPM (Backup PHP Parser)
  • Nginx Varnish (Running by default)
  • Memcached and APC
  • Clean WordPress Install (Latest Version)
  • WP-CLI

All working ok. I checked headers and Varnish is working. When I try to force 301 from non-www to www with Varnish in each individual site, it enter in a url redirect loop. I added to the Nginx configuration this line as i read in some documentation:

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

It doesn't work and i think it's for the Varnish configuration. I followed some of this documentation but nothing worked for me:

I hope somebody can illuminate me :)

  • 写回答

1条回答 默认 最新

  • doufu6423 2016-06-25 18:50
    关注

    In order to redirect from non-www to www using Varnish you will neeed to do the following:

    1- Disable any redirect rules that written in nginx configuration.

    2- redirect non-www to www through varnish using the following snippet:

    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);
        }
    }
    

    Note: I used the above snippet today and it worked correctly for me (it will work with Varnish 3 which is used in the playbook that mentioned in your question)

    If you would like to redirect using nginx you can try this:

    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...
    }
    

    Use one method only either varnish or nginx. In case you have nginx as the front end i suggest you to go with nginx way.

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么