up vote 20 down vote favorite
12
share [g+] share [fb]

I want to rewrite all http requests on my web server to be https requests, I started with the following:

server {
    listen      80;

    location / {
      rewrite     ^(.*)   https://mysite.com$1 permanent;
    }
...


One Problem is that this strips away any subdomain information (e.g., node1.mysite.com/folder), how could I rewrite the above to reroute everything to https and maintain the sub-domain?

link|improve this question

feedback

3 Answers

up vote 3 down vote accepted

This might help

http://forum.slicehost.com/comments.php?DiscussionID=1520

link|improve this answer
Wasn't able to get that working for my version of nginx... so put an alternative below. – Michael Neale Aug 17 '10 at 3:07
feedback

I have been using nginx 0.8.39 and above, and used the following:

 server {
       listen 80;
       rewrite ^(.*) https://$host$1 permanent;
 }

Sends a permanent redirect to the client.

link|improve this answer
You should also have "443" on the listen line. – gWaldo Dec 6 '11 at 20:51
I think it should be 80 - as this is listening for http and then telling the client to come back as https (443). – Michael Neale Dec 8 '11 at 22:30
feedback

The mothod you guys are using is actually one of the nginx pitfalls:

http://wiki.nginx.org/Pitfalls#Taxing_Rewrites

The correct code would be:

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

server {
       listen         443;
       server_name    my.domain.com;

       ssl            on;

       [....]
}

I know it is a small change, but I thought it was useful to add.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.