redirect - SSL for domain, including subdomains -


i have domain, example.com. that, have these:

  • blog.example.com
  • api.example.com
  • books.example.com

i have acquired ssl cert root domain , subdomains.

and want redirections so:

this example.com nginx conf

server {     listen 443 ssl default_server;     root /home/django/khophi;     index index.html index.htm;      server_name example.com;      include /etc/nginx/globalssl.conf; //ssl config }  server {     listen 80 default_server;     server_name example.com;     return 301 https://$host$request_uri; } 

nginx conf blog.example.com

server {         listen 443 ssl; // listens https          root /var/www/html/blog;         index index.php index.html index.htm;          server_name blog.example.com;          #include /etc/nginx/globalssl.conf;          location / {                 try_files $uri $uri/ /index.php?q=$uri&$args;         }          // error pages          location ~ \.php$ {                 //php specific things         } }  server {        listen 80;        server_name blog.example.com;        return 301 https://blog.example.com$request_uri; } 

books.example.com follows similar structure blog.example.com conf

what happens above setup?

  • visit http://example.com redirects https://example.com (just want
  • visit http://blog.example.com redirects https://blog.example.com but, shows page of https://example.com

in case, want:

  • example.com remain default_server
  • http://example.com redirect https://example.com
  • all http:// requested subdomains redirect respective https:// versions
  • if subdomain requested doesn't exist, should redirect default_server (https://example.com)

instead of using return, use rewrite instead on redirection server block on main domain , subdomains, so:

// blog.example.com  server {         listen 80;         server_name blog.example.com;         rewrite ^ https://$server_name$request_uri? permanent; } 

// example.com

server {     listen 80 default_server;     server_name example.com;     rewrite ^ https://$server_name$request_uri? permanent; } 

this link helped


Comments