【问题标题】:Nginx conditional rewriteNginx 条件重写
【发布时间】:2014-10-16 04:05:43
【问题描述】:

我正在尝试基于 http/https 将 domain1.com 重定向到 domain2.com。

我当前的 Nginx vhost 文件设置为:

server {
    listen 80;
    listen 443 ssl;

    ssl_certificate /etc/nginx/xxx.crt;
    ssl_certificate_key /etc/nginx/xxx.key;

    server_name *.domain1.com;
    if ( $scheme = "https" ) {
        rewrite ^ https://domain2.com$request_uri? permanent;
    }
    rewrite ^ http://domain2.com$request_uri? permanent;
}

但是访问https://domain1.com 只是转到http://domain2.com。我似乎无法检测到 https。我究竟做错了什么?谢谢。

编辑: 我已将我的虚拟主机编辑为以下内容,但出现了同样的问题。:

server {
    listen 80;
    listen 443 ssl;

    ssl_certificate /etc/nginx/xxx.crt;
    ssl_certificate_key /etc/nginx/xxx.key;

    server_name *.domain1.com domain1.com;
    return 301 $scheme://domain2.com$request_uri;
}

【问题讨论】:

  • 顺便说一句,server_name *.domain1.com; 不匹配 domain.com。
  • @AlexeyTen,我想要server_name *.domain1.com domain1.com 以匹配所有 www 和 domain1.com 的变体对吗?
  • 是的。或者只是.domain.com。

标签: nginx https dns rewrite


【解决方案1】:

没有必要使用if和rewrite

return 301 $scheme://domain2.com$request_uri;

【讨论】:

  • 我已经尝试了上述方法,但似乎发生了同样的问题,domain1.com 重定向到domain2.com。
  • 你确定不是浏览器缓存重定向?你试过 curl 或 wget 吗?
  • 是的,我已经尝试清除我的缓存并且似乎发生了相同的行为。
猜你喜欢
  • 1970-01-01
  • 2016-03-27
  • 2018-01-13
  • 1970-01-01
  • 1970-01-01
  • 2012-09-15
  • 1970-01-01
  • 2014-11-10
  • 2012-02-25
相关资源
最近更新 更多