Manipulate .conf file to rewrite https properly

mstoldt asked:

I’m having a nginx server and a domain, example.com. Each customer has his own subdomain, like wsb.example.com etc. Their content changes via PHP, but all subdomains get redirected to the same folder on the web server.

The only problem I’m having, is with accessing the domain without https. https://whatever.example.com/ works, but whatever.example.com redirects me to https://*.example.com/

What am I doing wrong?

server {
        listen *:80;
        listen *:443 ssl;
        server_name *.alterament.de;

        index index.php;
        root /var/www/webapp.alterament.de/public;

        ssl_certificate         ssl/alterament.de.crt;
        ssl_certificate_key     ssl/alterament.de.key;

        if ($ssl_protocol = "") {
                rewrite ^/(.*) https://$server_name/$1 permanent;
        }

        location ~ .php$ {
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
        }

        location /resources {
                root /var/www/webapp.alterament.de;
        }
}

My answer:


You used $server_name in your redirect, which causes the content of the server_name directive to be used. This is not what you want.

Instead, you should replace it with $host.


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.