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.

I'm using a Rackspace load balancer which enables me to set up my ssl key/pem inside of the admin panel. Everything works fine, I can use both http and https protocols. But if I try to redirect http to https using:

server{
  listen *:80;
  server_name mydomain.com www.mydomain.com; 
  rewrite ^ https://mydomain.com$request_uri? permanent;

...I get a redirect loop. I realize I'm not listening to port 443 but that's because the load balancer handled that for me. I also tried wrapping the rewrite in if ($scheme ~* http){to no avail.

The other part of my question is that I'd like to remove www from the url, can I do this with a single rewrite? Shouldn't the above rewrite take care of this as well?

Thanks for your help!

share|improve this question
    
The load balancer should be sending some indication to you of whether the connection was HTTPS. Ask Rackspace. (Oh, and you probably don't want to get rid of www...) –  Michael Hampton Apr 26 '13 at 0:56
    
Interesting, I'll look into that. Why do you think I shouldn't get rid of www? –  jwerre Apr 26 '13 at 2:54

2 Answers 2

up vote 3 down vote accepted

sciurus is correct in that Rackspace's Cloud Load Balancers set the X-Forwarded-Proto to https when SSL is offloaded at the load balancer. In order to avoid a redirect loop in nginx, you should be able to add the following to the location section in the vhost configuration:

if ($http_x_forwarded_proto = "http") {
            rewrite  ^/(.*)$  https://mydomain.com/$1 permanent;
}

This should avoid the infinite redirect loop while redirecting non-https requests to https.

share|improve this answer
    
This is exactly what I needed. Thanks slade! –  jwerre Apr 27 '13 at 5:21

The load balancer always talks to you over http. What is happening is

  1. The browser makes a request to port 80 on the load balancer
  2. The load balancer makes a request to port 80 on your web server
  3. Your web server sends a redirect to the user
  4. The user makes a request to port 443 on the load balancer

Steps 2-4 keep repeating until the browser detect the redirect loop and gives up.

EDIT: To resolve this, only perform the rewrite when the X-Forwarded-Proto header is set to http. That header is how Rackspace's load balancer tells your web server the protocol via which it received the request.

share|improve this answer
    
I guess that would explain why $server_protocol always returns HTTP –  jwerre Apr 26 '13 at 16:43
    
So you answered why this is happening... any suggestions on how to fix? –  jwerre Apr 26 '13 at 16:44

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.