0

I'm using nginx and I want to know if it's possible to do this.

Now to manage my vhost, I create them separately in /etc/nginx/sites-available (activate them after)

mydomain.com
subdomain.mydomain.com
subdomain1.mydomain.com
anotherdomain.com

So to redirect http to https I create a redirect vhost, that contain :

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name _;
        return 301 https://$host$request_uri;
}

mydomain.com and all other subdomain are under https.

But the problem is anotherdomain.com, I don't want to redirect it to https. And here I don't know how to do this. I've got an idea :

Create only on vhost, that contain all the config, and do this :

# Config of anotherdomain.com

# Redirect all domain/subdomain after this part to https
server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name _;
        return 301 https://$host$request_uri;
}

# Config of mydomain.com
# Config of subdomain.com
# Config of subdomain1.com

This will work as a kind of override, like in CSS ?

Thanks for your feedback.

migrated from stackoverflow.com Aug 21 '17 at 19:46

This question came from our site for professional and enthusiast programmers.

1

Typically you will have one forwarding block for each domain / subdomain. Redirection using the default_server isn't something I've seen done before, though in most cases it's probably ok.

So instead of using default server you define a block as follows, either for just the domain in question or for each domain

server {
  listen 80;
  server_name anotherdomain.com;

  location / {
    // whatever
  }
}

This will override the default_server block. Personally I'd define forwarding blocks for each website by domain, just because it seems neater.

server {
  listen 80;
  server_name example.com;
  return 301 https://example.com$request_uri;
}
2

default_server is the virtual host used when the request does not match any other virtual host.

So, therefore you need to define a virtual host that matches anotherdomain.com on port 80. nginx will then use that configuration instead of your default_server virtual host.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.