Score:0

Nginx redirects .com and .cloud to .com when trying to use dynamic cert folder name

ca flag

I have a small server on which I want to dynamically load ssl certs for multiple domains.

The problem is that the 2 domains that I currently have (domain.com and domain.cloud - "domain" is identical) are redirecting me to the same domain.com. What should I change?

    listen 80;
    server_name domain.com domain.cloud;

    access_log /var/log/nginx/root/access.log;
    error_log /var/log/nginx/root/error.log warn;

    # Redirect all http to https
    location / {
        return 301 https://$server_name$request_uri;
    }
}

server {
    listen 443 ssl;
    server_name domain.com domain.cloud;

    ssl_certificate /etc/ssl/$cert/cert.pem;
    ssl_certificate_key /etc/ssl/$cert/privkey.pem;

    access_log /var/log/nginx/root/access.log;
    error_log /var/log/nginx/root/error.log warn;

    root /var/www/root/pub;
    index index.php;

    location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
        access_log off;
        expires max;
        log_not_found off;
    }

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

map $ssl_server_name $cert {
   
    domain.com domain.com;
    domain.cloud domain.cloud;
}

Thank you!

Melody avatar
ca flag
@RichardSmith not I get "redirecting too many times" :(
Richard Smith avatar
jp flag
Use `curl -I` to test your server. What URL is being redirected to `domain.com`? Is it http or https. Edit your question and add the output of `curl -I` and any relevant entries in the access and error logs.
us flag
Rob
It is fairly common for web applications to be configured with a base URL and the redirect may be happening at the application level rather than in your web server configuration. (I don't see anything obvious there) Note that permanent redirects (301) are cached by your bowser and any changes won't be picked up when you test from your desktop webbrowser. - For more about testing/debugging https://serverfault.com/q/1092950/960939
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.