簡體   English   中英

使用導軌和彈性豆莖從 http 轉發到 https 時,如何防止重定向循環?

[英]How do I prevent a redirect loop when forwarding from http to https using rails and elastic beanstalk?

我剛剛在我的 Rails 應用程序中安裝了 ssl 證書,現在我正在嘗試將所有 http 流量重定向到 https。 它托管在 AWS 彈性 beantalk 上。 我已按照說明調整 nginx 配置以服務 ssl。 問題是它仍然允許 http 流量。 我發現的每個解決方案要么完全失敗,要么導致重定向循環。 (將新的服務器塊添加到端口 80 的配置中以處理重定向;添加 if 指令以在協議類型為 http 時進行重定向;等等)

下面是來自.ebextensions的我的附加 nginx 配置文件:

files:
  /etc/nginx/conf.d/https.conf:
    content: |
      # HTTPS server

      server {
          listen       443;
          server_name  localhost;

          ssl                  on;
          ssl_certificate      /etc/pki/tls/certs/server.crt;
          ssl_certificate_key  /etc/pki/tls/certs/server.key;

          ssl_session_timeout  5m;

          ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
          ssl_prefer_server_ciphers   on;

          location / {
              proxy_pass  http://my_app.com;
              proxy_set_header        Host            $host;
              proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
              proxy_set_header        X-Forwarded-Proto https;
          }

          location /assets {
            alias /var/app/current/public/assets;
            gzip_static on;
            gzip on;
            expires max;
            add_header Cache-Control public;
          }

          location /public {
            alias /var/app/current/public;
            gzip_static on;
            gzip on;
            expires max;
            add_header Cache-Control public;
          }
      }

  /etc/pki/tls/certs/server.crt:
    content: |
      -----BEGIN CERTIFICATE----- 
      xxxxxxxxxxxxxxxxxxxxxxxxx
      REDACTED CERTIFICATE HERE
      xxxxxxxxxxxxxxxxxxxxxxxxx
      -----END CERTIFICATE----- 

container_commands:
  01restart_nginx:
    command: "service nginx restart"

此配置有效,但我需要重定向策略。 我懷疑是http://my_app.com的配置代理導致了循環,但我不知道將它發送到哪里讓 Rails 接收請求。 我究竟做錯了什么? 如何在不創建循環的情況下將所有 http 流量重定向到 https? 順便說一句,我絕對不想涉及負載均衡器。

任何幫助表示贊賞。

所以答案比我想象的要簡單。 我仍然不確定如何防止重定向循環,但我可以使用 Rails 設置:

config.force_ssl = true

完成我想做的事情並路由所有 http 流量以使用 https。

暫無
暫無

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

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