I am able to resolve cases 1-4, by using NGINX's server matching logic with regex.
Nginx will resolve server names in this order (Note: Nginx is also optimized for this order, with regex the slowest):
- exact match
- leading *
- trailing *
- first regex match
Here's the matching & redirect logic implemented in the code below:
multilevel subdomains ---> to single-level subdomain --> to ssl
Still cannot resolve 5 & 6 without hitting the browser security warning (firefox and safari do not throw these errors), due to the fact that my wildcard ssl cert is only valid for *.example.com. There is simply no way around this.
- http:// sub1.example.com
- http:// sub2.sub1.example.com
- http:// sub3.sub2.sub1.example.com
- https:// sub1.example.com
- https:// sub2.sub1.example.com
- https:// sub3.sub2.sub1.example.com
-------> https:// sub1.example.com
# Default server configuration
server {
listen 80;
listen [::]:80;
listen 443;
listen [::]:443;
server_name ~^(.*).(?<subdomain>w+).example.com$;
return 301 http://$subdomain.example.com$request_uri;
}
server {
listen 80;
listen [::]:80;
server_name ~^(?<subdomain>w+).example.com$;
return 301 https://$subdomain.example.com$request_uri;
}
server {
# SSL configuration
#
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
#
root /var/www/html;
location / { ..... localhost:3000
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…