0

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;
        }
}
2
  • Try replacing $server_name with $http_host in the rewrite field Jul 23, 2015 at 7:33
  • It hasn't changed anything...
    – mstoldt
    Jul 23, 2015 at 13:30

2 Answers 2

1

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.

3
  • That results in the same, no matter if I replace it with $host or $http_host.
    – mstoldt
    Jul 23, 2015 at 13:28
  • Did you actually reload nginx? Jul 23, 2015 at 13:35
  • Yes sure, the same way I reloaded nginx after commenting the ssl session, and that worked out.
    – mstoldt
    Jul 23, 2015 at 14:21
0

This configuration checks if the protocol is not https and force to use https:

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

Try to comment out the whole block and restart nginx

1
  • That works, but the rewrite makes sense so is there a way to add a https without changing the domain to *.example.com?
    – mstoldt
    Jul 23, 2015 at 13:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .