简体   繁体   English

Jelastic Nginx http到https重定向

[英]Jelastic Nginx http to https redirect

I have an account in Jelastic and I want to force my site to work only over https. 我在Jelastic中有一个帐户,我想强制我的网站只能通过https进行工作。 I've created environment nginx + php with nginx balancer and enabled Jelastic SSL (as it described here ). 我创建环境的nginx + PHP nginx的平衡器和启用SSL Jelastic(因为它描述这里 )。

Whenever I tried to setup 301 redirect from http to https with no luck. 每当我尝试设置301从http重定向到https都没有运气时。 Using mod_rewrite didn't work for me, the only thing I've got is a loop redirect. 使用mod_rewrite对我不起作用,我唯一得到的就是循环重定向。 Google didn't help. Google没有帮助。

I really need advise. 我真的需要劝告。 Any additional info will be provided. 将提供任何其他信息。

Thanks in advance. 提前致谢。

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). 如果您使用的是Jelastic SSL,则意味着您在共享的Jelastic解析器上有SSL卸载=对服务器的所有请求都是http(不是http / https)。

You need to check the X-Forwarded-Proto header to identify which requests were originally https. 您需要检查X-Forwarded-Proto标头,以识别最初是https的请求。

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. 确实,当您启用Jelastic SSL(意味着您不能使用公共IP)共享解析器时,它将处理对服务器的所有请求,但在解析器和服务器之间的请求不是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. 因此,通过重定向,您尝试进行设置,服务器将传入的http请求重定向到https并将其发送回解析器,结果您得到了循环重定向。

You need to perform a few simple steps to resolve this issue. 您需要执行一些简单的步骤来解决此问题。 Login to dashboard, open settings for nginx balancer 登录到仪表板,打开Nginx Balancer的设置

在此处输入图片说明

Navigate to nginx-jelastic.conf under /conf 导航到/ conf下的nginx-jelastic.conf

在此处输入图片说明

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. 完成配置后,请不要忘记按下“保存”按钮并重新启动Nginx。

(Всемпривет)使用域ssl,此变体可以正常工作

if ( $scheme = "http" ) { rewrite ^(.*)$    https://$host$1 last; }

The accepted solution did not work for my setup. 接受的解决方案不适用于我的设置。 I had to change the if statement to the following: 我必须将if语句更改为以下内容:

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: 如官方的Nginx文档https://www.nginx.com/blog/creating-nginx-rewrite-rules#https中所述,您应该使用:

return 301 https://$host$request_uri;

which works as well. 效果也不错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM