dimanche 1 août 2021

How to remove unconfigured subdomains without hardcode with nginx?

I have multiple apps running on my vps each with a subdomain, office.domain.com drive.domain.com etc and a static website for the main domain.

I have a wildward certificate for https and so i'm trying to configure my nginx to be as easy as possible to add new subdomains for my projects.

Meanwhile i want to redirect every subdomain wich is not configured to the main domain without www.

I'm also trying to hardcode the least stuff possible especially the main domain.

For now my configuration looks like this :

# Main configuration
# Sets https and serve static website
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        server_name _;

        add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

        return 301 https://$host$request_uri;
}

server {
        listen 443 ssl default_server;
        listen [::]:443 ssl default_server;

        server_name     _ *._;

        add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

        ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;        
        include /etc/letsencrypt/options-ssl-nginx.conf;
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

        root /home/hentaishiroyuki/domain.com/www;
        index index.html;

        location / {

        }
}

and for each app, I create a new configuration file as :

server {
    server_name code.*;

    listen 443 ssl;

    location / {
        proxy_pass http://localhost:8080/;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection upgrade;
        proxy_set_header Accept-Encoding gzip;
    }
}

Right now, it redirects every unconfigured first level subdomain to the main domain but without removing the subdomain and it doesn't redirect 2+ levels subdomains.

Thanks for your help !




Aucun commentaire:

Enregistrer un commentaire