How can I use regex and nginx together so that these will all redirect to the same endpoint?
- 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
---- all resolve to---> https:// sub1.example.com
My current configuration (only works for cases 2, 3 & 4):
# Default server configuration
server {
listen 80 default_server;
listen [::]:80 default_server;
#NOT WORKING: regex to limit subdomains:
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;
server_name *.example.com;
location / { ..... localhost:3000
then in another file:
server {
# Redirect all http traffic to https
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
server {
# SSL configuration
listen 443 ssl;
listen [::]:443 ssl;
#
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
#
root /var/www/html;
server_name example.com www.example.com ;
if ($http_x_forwarded_proto = "http") {
return 301 https://$server_name$request_uri;
}
location / { .... localhost:8000