如何使用aws弹性负载均衡器强行将http请求重定向到独立乘客的https?

Vis*_*hal 13 ruby-on-rails passenger nginx amazon-web-services elastic-load-balancer

我在我的应用程序中使用了独立乘客.目前我的应用程序在http和https上运行.我想将所有http请求重定向到https.我在我的应用程序中使用了负载均衡器.我读过这篇文章

https://aws.amazon.com/premiumsupport/knowledge-center/redirect-http-https-elb/

https://www.phusionpassenger.com/library/config/standalone/intro.html#nginx-configuration-template

http://code.eklund.io/blog/2015/03/17/managing-rewrites-for-a-rails-app-on-heroku-with-nginx-plus-phusion-passenger/

我尝试了这两种方法

1)

 if ($http_x_forwarded_proto = "http") { 
            return 301 https://$host$request_uri; 
        }
Run Code Online (Sandbox Code Playgroud)

2)

if ($http_x_forwarded_proto != "https") {
      rewrite ^(.*)$ https://$server_name$REQUEST_URI permanent;
  }
Run Code Online (Sandbox Code Playgroud)

我以同样的方式尝试了所有过程.但每次进入无限循环时,在我开始乘客之前,实例会因为请求超时太多而终止自身并创建新实例.

我无法弄清楚,无论是弹性负载平衡器还是乘客配置问题.我想当我停止乘客和用户尝试访问应用程序.生成请求超时以及由于创建了新实例.我不确定.

提前致谢 :)

Dan*_*orf 1

您可以在代理级别或应用程序级别执行此操作。要在应用程序级别执行此操作:

# config/environments/production.rb
...
config.force_ssl = true
...
Run Code Online (Sandbox Code Playgroud)

  • 我第一次尝试过,但是由于负载均衡器,它陷入了无限循环。 (2认同)