NGINX dynamic subdomain support for existing setup

So I’ve got this setup where NGINX seems to work fine. But the requirement now is to add support for dynamic subdomains. Not sure on how to do it. Help would be greatly appreciated. Also to be able to redirect from www to non www for both domain and subdomain. I’ve searched online but nothing seemed to work for this solution.

What I’m trying to do: Currently I have this setup for normal non subdomains. For example exampleDomain.com or www.exampleDomain.com. Now I want to introduce dynamic subdomains. e.g sub1.exampleDomain.com or sub50.exampleDomain.com. The problem I’m having is that I’m unable to properly redirect to subdomains if one exists. e.g if someone visits sub2.exampleDomain.com it gets redirected to exampleDomain.com. What my question is how can I introduce subdomains to this setup so that www.sub3.exampleDomain.com goes to sub3.exampleDomain.com/ sub3.exampleDomain.com goes to sub3.exampleDomain.com and www.exampleDomain.com goes to exampleDomain.com and exampleDomain.com goes to exampleDomain.com. (If any of this makes sense 🙂 )

server {
        listen 80;
        listen [::]:80;
        server_name exampleDomain.com www.exampleDomain.com;

        location ~ /.well-known/acme-challenge {
                allow all;
                root /var/www/html;
        }

        location / {
                return 301 https://exampleDomain.com$request_uri?;
        }
}

server {
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
        server_name www.exampleDomain.com;

        server_tokens off;

        ssl_certificate /etc/letsencrypt/live/exampleDomain.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/exampleDomain.com/privkey.pem;

        ssl_buffer_size 8k;

        ssl_dhparam /etc/ssl/certs/dhparam-2048.pem;

        ssl_protocols TLSv1.3 TLSv1.2;
        ssl_prefer_server_ciphers on;

        ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:!ADH:!AECDH:!MD5;

        ssl_ecdh_curve secp384r1;
        ssl_session_tickets off;

        ssl_stapling on;
        ssl_stapling_verify on;
        resolver 8.8.8.8;

        return 301 https://exampleDomain.com$request_uri;
}

server {
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
        server_name exampleDomain.com;
        server_tokens off;

        gzip on;
        gzip_disable "MSIE [1-6]\.(?!.*SV1)";
        gzip_min_length  500;
        gzip_buffers  4 32k;
        gzip_types  text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript image/svg+xml image/svg;
        gzip_vary on;

        ssl_certificate /etc/letsencrypt/live/exampleDomain.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/exampleDomain.com/privkey.pem;

        ssl_buffer_size 8k;

        ssl_dhparam /etc/ssl/certs/dhparam-2048.pem;

        ssl_protocols TLSv1.3 TLSv1.2;
        ssl_prefer_server_ciphers on;

        ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:!ADH:!AECDH:!MD5;

        ssl_ecdh_curve secp384r1;
        ssl_session_tickets off;

        ssl_stapling on;
        ssl_stapling_verify on;
        resolver 8.8.8.8;

        location ~*  \.(jpg|jpeg|png|gif|ico)$ {
                expires 365d;
        }

        location / {
                try_files $uri @nodejs;
        }

        location @nodejs {
                proxy_pass http://nodejs:8080;
                add_header X-Frame-Options "SAMEORIGIN" always;
                add_header X-XSS-Protection "1; mode=block" always;
                add_header X-Content-Type-Options "nosniff" always;
                add_header Referrer-Policy "no-referrer-when-downgrade" always;
                add_header Content-Security-Policy "default-src * data: 'unsafe-eval' 'unsafe-inline'" always;
                # add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
                # enable strict transport security only if you understand the implications
        }

        location /graphql {
                proxy_pass http://nodejs:8080;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_set_header Host $host;
        }

        root /var/www/html;
        index index.html index.htm index.nginx-debian.html;
}

Answer

How about an example from the manual:

server {
    server_name   ~^(www\.)?(?<domain>.+)$;

    location / {
        root   /sites/$domain;
    }
}

Just adopt to your environment.

server_name ~^(www\.)?(?<subdom>[^\.]*)\.your-external-domain\.com$;
location / {
    root   /sites/$subdom;
}

This only works for subdomains, ignoring www.

Attribution
Source : Link , Question Author : TSlegaitis , Answer Author : unNamed

Leave a Comment