I already have a working configuration to redirect all non-existent subdomains to a specific domain. Now I want to add a configuration that redirects all HTTP requests to HTTPS on the same domain.
This config file does the subdomain redirects:
# redirect any non-existent subdomain (blah.domain.de -> domain.de)
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
return 301 https://domain.de$request_uri;
}
And this is the config I try to use for HTTP->HTTPS:
# redirect any HTTP request to its HTTPS address (http://domain.de/blub -> https://domain.de/blub)
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name domain.de mail.domain.de admin.domain.de;
return 301 https://$server_name$request_uri;
}
But this doesn't work because the default_server is defined twice. How can combine these two functions?
Do I really have to add the second config to every single subdomain config?
You need to remove
default_server
from the latter configuration and add every domain name to theserver_name
part.Then you need to use
return 301 https://$host$request_uri;
for the redirect statement.Nginx does not support nested if statements (it also doesn't support complex conditions).
I got it working in another way. I added it to every subdomains config via a SSL snippet. Every subdomain config includes the same SSL config. So I added to this config a http check:
And this is included in every subdomain server block config:
It does what I want. Maybe there is another way without including it to every subdomain.