【问题标题】:nginx rewrite for subsubdomains to subdomainsnginx 将子域重写为子域
【发布时间】:2015-03-04 02:33:53
【问题描述】:

我们有一个应用程序,我们为每个客户的安装使用子域。所以我们有 customer1.ourapp.com、customer2.ourapp.com、customer3.ourapp.com 等等。

出于安全考虑,我们希望将所有 http 重定向到 https,因为我们有一个通配符 SSL 证书。 此外,有些客户不是那么精通技术并将 www 添加到他们的域名中,因此您会得到类似:http://www.customer1.ourapp.comhttps://www.customer1.ourapp.com。在这些情况下,由于子子域,SSL 证书无效。

我正在尝试为 nginx 编写 vhost 配置,以便在这两种情况下进行正确的重定向。我得到了 http 到 https 的重定向来使用:

server {
    listen      80;
    server_name *.ourapp.com;

     #Rewrite all nonssl requests to ssl.
     return 301 https://$host$request_uri$is_args$args;
}

正确使用网址:

server {
    listen      443;
    server_name *.ourapp.com;

     #Rest of config
}

尝试了子子域,但不匹配:

server {
        server_name "~^(.*)\.(.*)\.ourapp\.com$";
        return 301 https://$2.ourapp.com$request_uri;
}

知道如何让它工作吗?

【问题讨论】:

    标签: nginx rewrite


    【解决方案1】:

    通配符服务器优先于正则表达式,并且也匹配“www...”。 您可以对这两种情况使用一个定义:

    server_name ~ ^(?:.*\.)?(.*)\.ourapp\.com$;
    return 301 https://$1.ourapp.com$request_uri;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-30
      • 1970-01-01
      • 2012-03-05
      • 1970-01-01
      • 1970-01-01
      • 2012-01-28
      • 2017-11-13
      相关资源
      最近更新 更多