V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
jtsai
V2EX  ›  问与答

nginx rewrite 问题求助

  •  
  •   jtsai · Oct 17, 2018 · 1229 views
    This topic created in 2761 days ago, the information mentioned may be changed or developed.

    一个站点想全站 cdn,想把没带 www 和 http 的都 301 到 https://www.?.com ,写了这下面这条规则,浏览器返回 “重定向次数太多了” 正确的配置应该是怎样的?

    rewrite ^/(.*)$ https://www.?.com/$1 permanent;
    
    Supplement 1  ·  Oct 17, 2018
    我刚刚在 cdn 后台配置找到一项强制 https 了。现在的问题变成要怎么把 “不带 www ” 强制到 “带 www ”
    3 replies    2018-10-17 18:48:21 +08:00
    0ZXYDDu796nVCFxq
        1
    0ZXYDDu796nVCFxq  
       Oct 17, 2018 via Android   ❤️ 2
    正确姿势是加两个,server

    server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://www.example.com$request_uri;
    access_log off;
    }

    server {
    listen 443 ssl;
    server_name example.com;
    return 301 https://www.example.com$request_uri;
    access_log off;
    }
    jtsai
        2
    jtsai  
    OP
       Oct 17, 2018
    @gstqc 我理解的,cdn 过来的时候就一个 ip,我们可以设置他是 http 或者 https,也获取不到他的域名?所有你上面的配置好像不太适合?还是我理解错误了?
    ysc3839
        3
    ysc3839  
       Oct 17, 2018   ❤️ 1
    rewrite 是用来匹配有一定规则的网址的,像这种“匹配任意网址”的操作不应该用 rewrite,用其他的方法性能更好。
    比如这个问题可以用 return 301。像重定向任意网址到 index.php 可以用 try_files。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5354 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 07:54 · PVG 15:54 · LAX 00:54 · JFK 03:54
    ♥ Do have faith in what you're doing.