Score:0

How to configure wildcard subdomains together with some fixed names in NGINX?

cn flag

I have example.com and the following use cases:

  • SSL only
  • www. will be redirected to example.com (no www.)
  • example.com will reverse proxy to :3000
  • fix1.example.com will reverse proxy to :3001
  • fix2.example.com will reverse proxy to :3002
  • ...
  • *.example.com will reverse proxy to :4000
  • *.example.com/admin will reverse proxy to :5000
    • example.com/admin, fix1.example.com, fix2.example.com,... will not have /admin and must not reverse proxy to :5000

In my current configuration I have the following files in my sites-available / sites-enabled:

  • exmaple.com
  • fix1.example.com
  • fix2.example.com
  • ...
  • wild.example.com

Each file configures his own part as there is no other file. But I end up with duplicate or conflicting configurations, so I'm thinking, there must be a better approach in one file, that handles the entire domain with all use cases.

The SSL only and no www part is easy:

server {
    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    }
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
}
server {
    if ($host = example.com) {
        return 301 https://$host$request_uri;
    }
    listen 80;
    server_name example.com;
    return 301 http://$server_name$request_uri;
}

But how do I set up the fixed subdomains and the wild card subdomain together with the /admin path?

This is the block I use for the example.com proxy:

server {
        listen 443 ssl http2;
        server_name example.com;

        location / {
                proxy_pass http://localhost:3000;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }
        ssl_certificat #...
        ssl_certificat_key #...
}
Gerard H. Pille avatar
in flag
Can you first read https://nginx.org/en/docs/http/server_names.html and see how far you get?
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.