1
votes

I only have an SSL certificate for example.com and want to redirect both http://example.com and http://*.example.com to https://example.com using nginx. I'm aware of it being impossible to redirect subdomains via SSL without a certificate including all the subdomains, but at least, I should be able to redirect users who are typing www.example.com (port 80) to the SSL homepage.

My current nginx config starts as follows:

server {
        # This should catch all non-HTTPS requests to example.com and *.example.com
        listen 80;
        server_name example.com *.example.com;
        access_log off;
        return 301 https://example.com$request_uri;
}

server {
        listen 443 ssl;
        # Actual server config starts here...

Requesting http://example.com will be redirected properly to https://example.com, whereas http://www.example.com leads to https://www.example.com (and of course, the browser is showing a certificate error). I think it has something to do with the processing order of the server_name values, but I haven't found any information about how to enforce a certain order.

2

2 Answers

0
votes

Try to add another server {}

server {
        listen 80;
        server_name www.example.com
        access_log off;
        return 301 https://example.com$request_uri;
}
0
votes

Try:

server_name  example.com  www.example.com  *.example.com;

Taken directly from the Nginx docs.