【问题标题】:HTTPS redirect Elastic Beanstalk Environment Puma Rails 5HTTPS 重定向 Elastic Beanstalk 环境 Puma Rails 5
【发布时间】:2018-05-02 15:28:39
【问题描述】:

我无法将http://example.com 重定向到https://example.com。我尝试了各种配置,但没有任何效果。

根据研究,我意识到我需要将其添加到 nginx 配置中。

if ($http_x_forwarded_proto != 'https') {
          rewrite ^ https://$host$request_uri? permanent;
        }

我在 ..ebextensions 目录中新建一个配置文件,内容如下,

upstream my_app {
  server unix:///var/run/puma/my_app.sock;
}

log_format healthd '$msec"$uri"'
                '$status"$request_time"$upstream_response_time"'
                '$http_x_forwarded_for';

server {
  listen 80;
  server_name _ localhost; # need to listen to localhost for worker tier

  if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
    set $year $1;
    set $month $2;
    set $day $3;
    set $hour $4;
  }

  access_log  /var/log/nginx/access.log  main;
  access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;

  location / {
    proxy_pass http://my_app; # match the name of upstream directive which is defined above
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }

  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;
  }
}

保存并执行

  1. eb 部署
  2. 转到http://example.com

我仍然收到“不确定”消息。

我还使用了来自this 的内容。但这也不起作用。

我错过了什么?

苏尼尔

【问题讨论】:

    标签: ruby-on-rails amazon-web-services redirect amazon-elastic-beanstalk puma


    【解决方案1】:

    使用 EB 文件密钥自定义 nginx conf 生成模板。 http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html

    创建一个文件.ebextensions/01_puma_nginx.conf(配置文件按字母顺序执行,所以根据附加要求调整名称前缀),内容如下。

    files:
      "/opt/elasticbeanstalk/support/conf/nginx_config.erb":
        mode: "000644"
        owner: root
        group: root
        content: |
    Paste your custom nginx configuration template content here....
    

    与其构建您自己的模板并破坏某些东西,不如检查当前实例中的现有模板并仅调整所需的东西(这里只是您的 http 重定向部分)。

    好吧,如果这看起来有点复杂,您可以通过检查“X-Forwarded-Proto”标头来使用 rails force_ssl 选项

      force_ssl if: :ssl_required?
    
      def ssl_required?
        if request.headers["X-Forwarded-Proto"]!="https"
          true
        else
          false
        end
      end
    

    【讨论】:

    • 你也可以通过检查标题'X-Forwarded-Proto'在rails中使用force_ssl
    • @vlad,不确定是否需要监听端口 443,因为通常 elasticbeanstalk 与负载均衡器结合使用,负载均衡器处理 ssl 并将请求路由到具有额外 X-Forwarded-Proto 标头的实例。 (没有足够的代表评论您的答案:P)
    • 我做了 force_ssl 选项,它起作用了。我在 ebextention 选项上得到了足够的指导,也可以尝试一下。谢谢。我会结束这个问题。
    【解决方案2】:

    在您的代码中,我没有看到 443 是 HTTPS。

    这是我的 SSL 脚本。

    upstream puma_production {
      server unix:/home/deploy/games.directory/shared/tmp/sockets/puma.sock fail_timeout=0;
    }
    
    server {
      listen 80;
    
      location / {
        return 301 https://$host$request_uri;
      }
    }
    
    server {
      listen 443;
      server_name games.directory;
      root /home/deploy/games.directory/current/public;
      try_files $uri/index.html $uri @puma_production;
    
      ssl on;
      ssl_certificate '';
      ssl_certificate_key '';
      ssl_session_timeout 1d;
      ssl_session_cache shared:SSL:50m;
      ssl_session_tickets off;
    
      ssl_protocols TLSv1.1 TLSv1.2;
      ssl_ciphers ''
      ssl_prefer_server_ciphers on;
    
      # HSTS (ngx_http_headers_module is required) (15768000 seconds = 6 months)
      add_header Strict-Transport-Security max-age=15768000;
    
      # OCSP Stapling ---
      # fetch OCSP records from URL in ssl_certificate and cache them
      ssl_stapling on;
      ssl_stapling_verify on;
      ssl_trusted_certificate /etc/letsencrypt/live/games.directory/chain.pem;
    
      error_page 500 502 503 504 /500.html;
      client_max_body_size 4G;
      keepalive_timeout 10;
    
      location @puma_production {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Forwarded-Proto https;
        proxy_pass http://puma_production;
    
        access_log /home/deploy/games.directory/shared/log/nginx.access.log;
        error_log /home/deploy/games.directory/shared/log/nginx.error.log;
      }
    
      location ^~ /assets/ {
         gzip_static on;
        expires max;
        add_header Cache-Control public;
      }
    
      if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
        return 405;
      }
    }
    

    【讨论】:

    • 我通过 EB Web 控制台在 443 上配置了 HTTPS。
    【解决方案3】:

    将 HTTP 流量重定向到 HTTPS 是一个非常常见的需要解决的问题。因此,AWS 在这里记录了解决方案,涵盖了他们所有不同的 Elastic Beanstalk 平台: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/configuring-https-httpredirect.html

    【讨论】:

      猜你喜欢
      • 2020-12-08
      • 2016-06-19
      • 2019-03-03
      • 2016-08-25
      • 2018-01-01
      • 2014-07-05
      • 2021-02-23
      • 2015-05-18
      • 2018-08-27
      相关资源
      最近更新 更多