Nginx将子子域重写为子域 [英] nginx rewrite for subsubdomains to subdomains

查看:105
本文介绍了Nginx将子子域重写为子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We have an application where we use subdomains for each of our customer's installations. so we have customer1.ourapp.com, customer2.ourapp.com, customer3.ourapp.com and so on.

由于安全原因,我们希望将所有http重定向到https,因为我们有通配符SSL证书. 另外,有些客户并不精通技术,并将www添加到他们的域名中,因此您会得到如下信息: http://www.customer1.ourapp.com https://www.customer1.ourapp.com .在这种情况下,由于子子网域,SSL证书无效.

Because of security we want to redirect all http to https, since we have a wildcard SSL certificate. Also, some customers are not that tech savvy and add www to their domain name, so then you get things like: http://www.customer1.ourapp.com or https://www.customer1.ourapp.com. In those cases the SSL certificate isn't valid because of the subsubdomain.

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

I'm trying to write the vhost config for nginx to make the correct redirect in these two cases. I got the http to https redirect to work with:

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
}

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

Made an attempt for the subsub domains, but it's not matching:

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

有什么想法可以使它正常工作吗?

Any idea how to get this working?

推荐答案

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

Wildcarded server takes precedence over regexp'ed one and matches 'www...' too. You can use one definition for both cases:

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

这篇关于Nginx将子子域重写为子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆