簡體   English   中英

如何使用aws彈性負載均衡器強行將http請求重定向到獨立乘客的https?

[英]How can i forcefully redirect http request to https in passenger standalone with aws 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; 
        }

2)

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

我以同樣的方式嘗試了所有過程。 但每次進入無限循環時,在我開始乘客之前,實例會因為請求超時太多而終止自身並創建新實例。

我無法弄清楚,無論是彈性負載平衡器還是乘客配置問題。 我想當我停止乘客和用戶嘗試訪問應用程序。 生成請求超時以及由於創建了新實例。 我不確定。

提前致謝 :)

您可以在代理級別或應用級別執行此操作。 要在應用級別執行此操作:

# config/environments/production.rb
...
config.force_ssl = true
...

我希望您的所有證書都正確安裝並指向正確的路徑。 如果沒有,請檢查以下配置

Passenger.json

{
  "environment": "production",
  "instance_registry_dir": "/var/run/passenger-instreg",
  "daemonize": true,
  "user": "myappuser",
  "port": 443,
  "ssl": true,
  "ssl_certificate": "/path/to/ssl_cert.pem",
  "ssl_certificate_key": "/path/to/ssl_key.pem",
  "nginx_config_template": "/path/to/nginx.conf.erb"
}

您需要使用與nginx相同的配置來從http重定向到https

http {
     server_tokens off;
        server {
            listen 80 default_server;
            listen [::]:80 default_server;

            # Redirect all HTTP requests to HTTPS with a 301 Moved Permanently response.
             return 301 https://$host$request_uri;
     }

最新鏈接
以下是從http重定向到https latest_link的獨立乘客的配置

請參閱此鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM