Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting a subdomain with a regular expression in nginx

Tags:

regex

nginx

The nginx documentation says that the server_name directive supports regular expressions. I've been banging my head against the wall trying to get even a trivial regex working.

I want http://subdomain.mydomain.com to redirect to http://mydomain.com/subdomain

Here is my code.

server {
  server_name "~^subdomain\.mydomain\.com$";
  rewrite ^ http://mydomain.com/subdomain;
}

Also, potentially noteworthy. Further down in the nginx config file there is a rule:

server {
  server_name *.mydomain.com
  ...
}

What am I doing wrong?

UPDATE:

It has been suggested that I not use regex for this... to offer a little more clarity: the trivial regex was simply for purposes of troubleshooting. The real regex will look more like...

server {
  server_name "~^.*(cvg|cincinnati)\.fakeairport(app)?\.(org|com)$";
  rewrite ^ http://fakeairport.com/cincinnati;
}

server {
  server_name "~^.*(lex|lexington)\.fakeairport(app)?\.(org|com)$";
  rewrite ^ http://fakeairport.com/lexington;
}

So it would be preferable to use regex.

like image 705
danott Avatar asked Mar 06 '12 05:03

danott


People also ask

What is servername in nginx?

If no server_name is defined in a server block then nginx uses the empty name as the server name. nginx versions up to 0.8. 48 used the machine's hostname as the server name in this case. If a server name is defined as “ $hostname ” (0.9. 4), the machine's hostname is used.

How do I redirect a domain to nginx?

Nginx Hosting Multiple Domains conf file, which is usually located in /etc/nginx . Or, to redirect the www URL to the non-www address for all domains, add this snippet inside the http directive in your nginx. conf file, which is usually located in /etc/nginx .


2 Answers

To answer an old question to help others

using nginx 1.1.19 you can do the following:

server {
    server_name     ~^(?<subdomain>\w+)\.domainA\.com$;

    location / {
            rewrite ^ https://$subdomain.domainB.com$request_uri permanent;
    }
}

The subdomain before domainA.com is matched and stored in variable $subdomain which then can be used in the rewrite. This rewrites url like xxx.domainA.com to xxx.domainB.com with only one server directive.

like image 99
Bart Avatar answered Sep 28 '22 01:09

Bart


Gotta love regex with NGINX!

As I often work with multiple domain-names and I like to keep my configs as clean and rock solid as possible I almost always use regex with nginx.

In this case I've solved it with the following regex:

server {
    listen 80;
    server_name ~^((?<subdomain>.*)\.)(?<domain>[^.]+)\.(?<tld>[^.]+)$;
    return 301 $scheme://${domain}.${tld};
}

What this does is the following: every subdomain.domain-name.tld that points to this server (ip address) is automatically redirected to domain-name.tld.

So for instance www.myexampledomain.com is redirected to myexampledomain.com.

To answer the question, what you could also do is the following:

server {
    listen 80;
    server_name ~^((?<subdomain>.*)\.)(?<domain>[^.]+)\.(?<tld>[^.]+)$;
    return 301 $scheme://${domain}.${tld}/${subdomain};
}

Now mysubdomain.myexampledomain.com is converted into myexampledomain.com/mysubdomain.

Above regex is great as you can throw anything at it that you like and it will convert it for you.

like image 43
Ottonet Avatar answered Sep 28 '22 03:09

Ottonet