T
T
thisall2020-11-10 11:07:28
Nginx
thisall, 2020-11-10 11:07:28

How to put down an ssl certificate for dynamic subdomains?

I have a config for dynamic subdomains

server {
    server_name *.example.com;

    listen 443 ssl;
    
    root /var/www/platforms/$host;
    index index.php index.html index.htm;

    location / {
        root /var/www/platforms/$host;
        autoindex off;
    }
}


For normal subdomains, I put down the following code for redirecting to https

server {
    server_name test.example.com;

    if ($host = test.example.com){
      return 301 https://$host$request_uri;
    }

    listen test.example.com:80;
}


And everything works as it should, but if I write this code for dynamic subdomains

server {
    server_name *.example.com;

    if ($host = *.example.com){
      return 301 https://$host$request_uri;
    }

    listen *.example.com:80;
}


Then it throws an error

host not found in "*.example.com:80"

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lynn "Coffee Man", 2020-11-10
@Lynn

In 99% of cases, you just need to write listen 80; without any domains and IP addresses.
It almost never makes sense to specify a domain in listen. More often than not, this does not make any sense and even more likely leads to incomprehensible (for beginners) behavior.
if is not needed here either. The block should look like this:

server {
    listen 80;
    server_name *.example.com;
    return 301 https://$host$request_uri;
}

K
ky0, 2020-11-10
@ky0

Why are you there if? The condition will always be true anyway, because this block is limited in names by server_name`om.
Make location /and paste a redirect into it.
Well, in the sheet you have some kind of crookedness, leave only the port.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question