Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Most transactions from the webapp are secured on their own but do not show the lock icon when transactions are happening in the browser. The goal is to have certain pages, like the login pages, be ssl encrypted and show the lock icon in the browser. An example would be http://www.domain.com/pro should be https but if you went to http://www.domain.com it would come in over http. We have a few URIs that would need to be redirected.

My first attempt was trying a rewrite:

rewrite ^/pro$ https://www.domain.com/pro

But that obviously causes a redirect loop so I tried to include the http portion.

rewrite ^http:*./pro$ https://www.domain.com/pro

This also did not work and resulted in a redirect loop as well. My only guess is that the regex in rewrite does not look at the header only the URI itself so it would start with www and not http.

I tried a few things using location instead of rewrite and this is where I'm stuck.

location /pro {
    if ( $scheme = http ){
        rewrite ^ https://www.domain.com/pro;
    }
}

With this in the configuration the rewrite happens but I get a 404 Not Found page from nginx. If I remove it but type in https://www.domain.com/pro then it loads fine. I feel that the location statement is catching the URI again but doesn't know what to do with it since the if statement is no longer true. I'm just not sure what else to include in the location block to get it to work.

So how can I get certain pages to switch from a http connection to a https connection?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

HTTPS and normal HTTP will come in through different ports, so really what you want to do is just redirect any normal HTTP requests that you want to be served over HTTPS to HTTPS. So the following:

server {
  listen                      80;
  server_name                 _;

  location / {
    rewrite ^/(pro)$ https://$host/$1;
  }
}

... should not cause a redirect loop, because it is forwarding only on :80, and it is forwarding TO :443 (which is what HTTPS requests will come in as, by default).

share|improve this answer
    
I had both 80 and 443 in the same server directive. Don't know why I didn't split it up sooner. Using the variables didn't work for me, it returned a 404 error. I had to explicitly say location /pro { rewrite ^ domain.com/pro } and do that for each URI I wanted to secure. Thanks –  Manny T Sep 22 '11 at 19:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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