【问题标题】:Nginx url rewrite does not workNginx url重写不起作用
【发布时间】:2017-04-16 18:09:53
【问题描述】:

我想将所有请求从my.domain.de 重定向到my.domain.com,包括将http 重写为https。 重定向仅适用于http://my.domain.de,它被重定向到https://my.domain.com,这是目标。 当我调用https://my.domain.de 时,它不会被重定向。 但是当我尝试访问 my.domain.comhttp://my.domain.com 时,重定向到 https 方案失败。奇怪,因为我在切换到 .com 域之前对 my.domain.de 使用了相同的重写规则,并且它起作用了。

这是我的 nginx.conf 文件:

    # my.domain.de
    server {
            listen 80;
            server_name my.domain.de;
            return 301 https://my.domain.com$request_uri;
    }

    # my.domain.com
    server {
            listen 80;
            listen 443;
            ssl on;

            ssl_certificate /path/to/cert;
            ssl_certificate_key /path/to/key;

            server_name my.domain.com;

             # Url rewrite does not seem to work:
            if ($scheme = http) {
              return 301 https://$server_name$request_uri;
            }
    }

编辑: 以前我写过 .de 域中所有内容的重定向都有效。不幸的是,它只有在我输入 http://my.domain.de 或没有 http:// 时才有效 当我使用https://my.domain.de 时,由于证书无效,它会收到警告。所以my.domain.de的重写规则也有问题。

EDIT2: 现在我为 my.domain.de 重新安装了一个证书,所以我唯一知道的问题是,http://my.domain.com 没有重定向到 https。 编辑 nginx.conf:

  # my.domain.de
server {
        listen 80;
        listen 443 ssl;

        ssl_certificate /path/to/cert.de;
        ssl_certificate_key /path/to/key.de;

        server_name my.domain.de;
        return 301 https://my.domain.com$request_uri;
}

# my.domain.com
server {
        listen 80;
        listen 443 ssl;

        ssl_certificate /path/to/cert.com;
        ssl_certificate_key /path/to/key.com;

        server_name my.domain.com;

         # Url rewrite does not seem to work:
        if ($scheme = http) {
          return 301 https://$server_name$request_uri;
        }
}

【问题讨论】:

  • 您有my.domain.de 的证书吗?因为没有它,您将无法真正设置来自https://my.domain.de 的重定向。
  • 不,因为我的 my.domain.de 证书即将到期,我想将其重定向到 my,domain.com,因为我有一个(通配符)。是不是可以在 https 握手之前做重定向?

标签: nginx https url-rewriting


【解决方案1】:

您已为端口 80 和端口 443 启用 SSL。不推荐使用 ssl on;,请改用 listen 指令的 ssl 选项。

使用显式默认服务器作为“包罗万象”,将不是my.domain.com 和任何http 地址的所有内容重定向到https://my.domain.com

server {
    listen 80 default_server;
    listen 443 default_server ssl;

    ssl_certificate /path/to/domain.de/cert;
    ssl_certificate_key /path/to/domain.de/key;

    return 301 https://my.domain.com$request_uri;
}

server {
    listen 443 ssl;
    server_name my.domain.com;

    ssl_certificate /path/to/domain.com/cert;
    ssl_certificate_key /path/to/domain.com/key;

    ...
}

请注意,一个服务器块使用旧证书,一个服务器块使用新证书。

请参阅this document 了解更多信息。

【讨论】:

  • 谢谢。在文档中,使用两个不同的 IP 可能会更容易。顺便说一句:我不小心删除了我以前的评论,因为我无法再编辑它了……查看我原来帖子的编辑。我按照您的建议使用 listen 443 ssl 删除了 ssl。问题仍然存在......
  • 很遗憾,出现证书错误是不可接受的。无论如何,谢谢你。
  • 我想再试一次 :-)
  • 好吧,正如我在上面编辑过的问题中所写的那样,我试了一下,但使用了新证书。奇怪的是,从 my.domain.com 重写为my.domain.com 仍然不起作用。
【解决方案2】:

由于重写对我不起作用,并且由于机器上的其他服务器,我无法设置默认服务器块,我终于通过添加两台服务器来解决问题,一台用于端口 80,另一台用于端口 443 .domain.com。我现在不认为这是可能的。所以这是我的新 nginx.conf:

  # my.domain.de
  server {
    listen 80;
    listen 443 ssl;
    ssl_certificate /path/to/cert.de;
    ssl_certificate_key /path/to/key.de;
    server_name my.domain.de;
    return 301 https://my.domain.com$request_uri;
  }

  # my.domain.com http
  server {
    listen 80;
    server_name my.domain.com;
    return 301 https://my.domain.com$request_uri;
   }

   # my.domain.com https
   server {
    listen 443 ssl;
    ssl_certificate /path/to/cert.com;
    ssl_certificate_key /path/to/key.com;
    server_name my.domain.com;
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    • 2018-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多