I'm trying to redirect only certain requests to use ssl and not my whole site but I can't figure how to escape this redirect loop. Here's my server block where I tried using a location block to redirect traffic:

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

server {
    listen 80;
    listen 443 ssl;
    ssl_certificate /srv/ssl/sslchain.crt;
    ssl_certificate_key /srv/ssl/example.com.key;
    server_name www.example.com;

    location / {
        root /usr/local/bin/rails/example/public;
        index index.html index.htm;
        passenger_enabled on;
    }

    location /users/sign_up {
        rewrite ^ https://www.example.com/users/sign_up break;
    }

    location /users/sign_in {
        rewrite ^ https://www.example.com/users/sign_in break;
    }

    if ($host !~* ^(example.com|www.example.com)$){
        return 502;
    }

    location ~* ^.+\.(jpg|jpeg|png|gif|css|js)$ {
        root /usr/local/bin/rails/example/public;
        passenger_enabled on;
        expires 31d;
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root html;
    }
}

I've also tried 'if' blocks....

if ($request_uri ~* /users/sign_up ) {
    rewrite ^ https://www.example.com/users/sign_up break;
}

And I've tried it without the 'break' argument. How can I redirect without getting stuck in a redirect loop? Thanks.

link|improve this question
Slightly off-topic, but you may be interested in these observations: stackoverflow.com/a/8765067/372643 – Bruno Jan 6 at 22:42
feedback

1 Answer

You'll need to avoid redirecting when the user's already connected via SSL.

Something like this:

location /users/sign_up {
    if ( $server_port = 80 ) {
        rewrite ^ https://www.example.com/users/sign_up break;
    }
}

location /users/sign_in {
    if ( $server_port = 80 ) {
        rewrite ^ https://www.example.com/users/sign_in break;
    }
}
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.