0

I am not sure, if I built myself a terrible foot-gun of some sort here with my approach of redirecting any traffic from non-www/www to a subdomain (say main), which is the landing page of the service.

Why I don't have the landing page and subsequent links on the plain domain is another (and valid) question, and for now, I would prefer to keep the database schemas like this. I use django-tenants and keep the public schema empty. The subdomain to which traffic is redirected, should remain the main entry-point to all subsequent services/tenants.

I browsed through plenty of nginx docs for weeks now, deferred the issue again and again and as of now cannot think of anything other than this very inelegant, but working (for now) solution:

server {
    listen 80;
    listen [::]:80;

    server_name .example.com;

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

server {

    listen 443 ssl;
    listen [::]:443 ssl http2;

    server_name example.com www.example.com;

    location / {
        return 301 https://main.example.com$request_uri;
    }
}


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

    server_name main.example.com sub1.example.com sub2.example.com sub3.example.com;

    location / {
        root   /usr/share/nginx/html;
        index  index.html;
        try_files $uri $uri/ /index.html;
    }
}

This works for now, but the service is going to have N subdomains/tenants in the future, so line

server_name main.example.com sub1.example.com sub2.example.com sub3.example.com;

will eventually be preferable like

server_name .example.com;

I tried with e.g.

server_name main.example.com .example.com;

but this yields a cyclic behavior, resolving the address to always main.example.com.

I am thinking of constraining the subdomains to a regex-able pattern, on the other hand, there has to be something cleaner.

I am out of good ideas on this one and appreciate any feedback on how to untie this.

other
  • 93
  • 1
  • 7
  • Have you tried `*.example.com`? See [Server Names document](http://nginx.org/en/docs/http/server_names.html). And don't forget the `;` at the end of the line. – Richard Smith Dec 05 '22 at 16:19
  • Reading section `Wildcard names` of the docs you mention, which I had read too, indeed, it says, ```A special wildcard name in the form “.example.org” can be used to match both the exact name “example.org” and the wildcard name “*.example.org”.``` So I took this as a shortcut to omit the asterisk version, even though section `Optimization` considers it not to be the fasted method. From my understanding, `.example.com` already includes `*.example.com`, but is a tad bit slower due to hash table priorities. Thanks, got the `;` in the actual file ;) – other Dec 05 '22 at 19:38
  • Server block 1 uses `.example.com` as it matches anything on port 80. Server block 2 uses `example.com` and `www.example.com` - two exact matches on port 443. You want to use `*.example.com` on server block 3 to match anything else on post 443. You do not want to use `.example.com` as the exact match `example.com` is already represented in server block 2. – Richard Smith Dec 05 '22 at 20:49
  • Yes, @RichardSmith, that works. Thank you for your explanation, makes sense now! As clean as it can get. Thanks for unblocking me there! – other Dec 06 '22 at 15:00

0 Answers0