4

I have an account in Jelastic and I want to force my site to work only over https. I've created environment nginx + php with nginx balancer and enabled Jelastic SSL (as it described here).

Whenever I tried to setup 301 redirect from http to https with no luck. Using mod_rewrite didn't work for me, the only thing I've got is a loop redirect. Google didn't help.

I really need advise. Any additional info will be provided.

Thanks in advance.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
JDF
  • 71
  • 1
  • 6

4 Answers4

8

The accepted solution did not work for my setup. I had to change the if statement to the following:

if ($http_x_forwarded_proto != "https") {
  return 301 https://$host$request_uri;
}

As described in the official nginx documentation https://www.nginx.com/blog/creating-nginx-rewrite-rules#https you should use:

return 301 https://$host$request_uri;

which works as well.

Thomas Rawyler
  • 1,075
  • 7
  • 16
4

Indeed, when you are enabling Jelastic SSL (means you can't use public IP) shared resolvers processing all requests to your server but between resolver and your server requests are not https. So with redirect, you tried to set up, your server redirected incoming http requests to https and sent it back to resolver, as result you've got a loop redirect.

You need to perform a few simple steps to resolve this issue. Login to dashboard, open settings for nginx balancer

enter image description here

Navigate to nginx-jelastic.conf under /conf

enter image description here

and add the code to the configuration file:

# force https-redirects
if ($http_X_Forwarded_Proto = http) {
    return 302 https://$host$request_uri;
}

nginx-jelastic.conf

Do not forget to press the "Save" button and restart the nginx after the configuration was made.

Community
  • 1
  • 1
Virtuozzo
  • 1,993
  • 1
  • 10
  • 13
  • Please check the official nginx documentation https://www.nginx.com/blog/creating-nginx-rewrite-rules#https – Thomas Rawyler Sep 05 '19 at 05:44
  • actually... it's probably better to use 301 (permanently moved)... and for me it had to be lower cased as in the an other answer (below). – zeropaper Nov 30 '19 at 08:04
1

If you're using Jelastic SSL this means you have SSL offload at the shared Jelastic resolvers = all requests to your server are http (not http / https).

You need to check the X-Forwarded-Proto header to identify which requests were originally https.

Damien - Layershift
  • 1,508
  • 8
  • 15
0

Всем привет) With domain ssl this variant works just fine

if ( $scheme = "http" ) { rewrite ^(.*)$    https://$host$1 last; }
rustem.russia
  • 575
  • 5
  • 6