0

I like to handle different sub domains in my Nginx server, this is my config file (partially)

server {
    listen                              80 default_server;
    listen                              443 default_server ssl;

    include                             ssl/mysite.conf;

    server_name                         default_server;
    rewrite                             ^(.*)$ https://mysite.com$1 permanent;

As you can see all the requests to sub-domain.mysite.com/query are redirected to mysite.com/query always in https. I will like to execute that rewrite only if the sub-domain is different from string1 and string2. So:

string1.mysite.com/query     --->   https://string1.mysite.com/query
foo.mysite.com/query         --->   https://mysite.com/query
DomingoSL
  • 335
  • 1
  • 4
  • 12

1 Answers1

3

You can specify different server blocks to different hostnames like AD7six suggested.

In your case, I would write 2 server blocks, one for string1.mysite.com and string2.mysite.com and another without the server_name filter to match all other requests.

Something like this:

server {
    listen                              80 default_server;
    listen                              443 default_server ssl;

    include                             ssl/mysite.conf;

    rewrite                             ^(.*)$ https://mysite.com$1 permanent;
    ...
}

server {
    listen                              80;
    listen                              443 ssl;

    include                             ssl/mysite.conf;

    server_name                         string1.mysite.com string2.mysite.com;
    ...
}

The server_name directive documentation is very good and can help you with this kind of problem. http://nginx.org/en/docs/http/server_names.html