Rewrite all locations to https except one

Hi Everyone,

i have this problem with rewrite. As subject says i want to rewrite all
locations to https except one, that should remain http. But that one
with
http isn’t redirecting properly. I’m using nginx 1.2.2 version. Here’s
my
conf:

server {
client_max_body_size 500M;
listen 80;
server_name alis.am.lt;
#rewrite ^(.*) https://$host$1 permanent;
#rewrite ^ https://$server_name$request_uri? permanent;

location / {
rewrite ^(.*) https://$host$1 permanent;
proxy_pass http://www_serveriai_80;
proxy_set_header Host $http_host;
}

location /SomeService {
rewrite ^(.*) http://$host$1 permanent;
proxy_method POST;
proxy_pass http://10.255.6.120:8080/SomeService;
proxy_set_header Host $http_host;
#proxy_redirect default;
#proxy_set_header Host $host;
#proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

I really appreciate any help. Thanks!

Regards,
Karolis

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,231948,231948#msg-231948

Hello!

On Thu, Oct 18, 2012 at 05:17:27AM -0400, karolis wrote:

server_name alis.am.lt;
#rewrite ^(.*) https://$host$1 permanent;
#rewrite ^ https://$server_name$request_uri? permanent;

location / {
rewrite ^(.*) https://$host$1 permanent;
proxy_pass http://www_serveriai_80;
proxy_set_header Host $http_host;
}

Just a side note: it doesn’t make sense to write proxy_pass here.
Using

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

would be enough. Or, better,

location / {
    return 301 https://$host$request_uri;
}

location /SomeService {
rewrite ^(.*) http://$host$1 permanent;

This will create infinite loop, as you try to redirect back to the
same address. Just remove this rewrite.

I really appreciate any help. Thanks!

Maxim D.
http://nginx.com/support.html

Hello!

On Thu, Oct 18, 2012 at 07:32:03AM -0400, karolis wrote:

proxy_set_header Host $http_host;

to stay always on http.
Thanks

The server{} block you provided is http-only, not https. On the
other hand, what you ask about needs correct configuration of two
server blocks, one for http, and another one for https.

Here is an example:

server {
    listen 80;
    server_name www.example.com;

    location / {
        # we are in http server, but want https for normal
        # requests - redirect to https

        return 301 https://$host$request_uri;
    }

    location /http_only_service {
        ... do real work here ...
    }
}

server {
    listen 443 ssl;
    server_name www.example.com;

    ssl_certificate ...
    ssl_certificate_key ...

    location / {
        ... do real work here ...
    }

    location /http_only_service {
        # this is http only service, but we are in https
        # server - redirect to http

        return 301 http://$host$request_uri;
    }
}

Hope this helps.


Maxim D.
http://nginx.com/support.html

Thanks Maxim for your help. But then where to write this proxy_pass:

proxy_pass http://www_serveriai_80;

And still, going further in my site, https drops off and i’m being left
only
with http. How to permanently stay on https, but in this location:

location /SomeService {
proxy_method POST;
proxy_pass http://10.255.6.120:8080/SomeService;
proxy_set_header Host $http_host;

to stay always on http.
Thanks

Regards,
Karolis

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,231948,231967#msg-231967

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs