【问题标题】:How do I prevent a redirect loop when forwarding from http to https using rails and elastic beanstalk?使用rails和弹性beantalk从http转发到https时如何防止重定向循环?
【发布时间】:2020-02-19 04:22:19
【问题描述】:

我刚刚在我的 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?顺便说一句,我绝对不想涉及负载均衡器。

感谢任何帮助。

【问题讨论】:

  • 我会选择应用程序 LB 而不是 nginx。 ALB 将重定向到 https 而不会在实例级别公开端口。

标签: ruby-on-rails amazon-web-services ssl nginx


【解决方案1】:

所以答案比我想象的要简单。我仍然不确定如何防止重定向循环,但我可以使用 Rails 设置:

config.force_ssl = true

完成我想做的事情并将所有 http 流量路由到使用 https。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    相关资源
    最近更新 更多