Manipulate .conf file to rewrite https properly

mstoldt asked:

I’m having a nginx server and a domain, Each customer has his own subdomain, like 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. works, but redirects me to https://*

What am I doing wrong?

server {
        listen *:80;
        listen *:443 ssl;
        server_name *;

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

        ssl_certificate         ssl/;
        ssl_certificate_key     ssl/;

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

        location ~ \.php$ {
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;

        location /resources {
                root /var/www/;

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.