Ruby on rails HTTPS重定向弹性Beanstalk环境Puma Rails 5

Ruby on rails HTTPS重定向弹性Beanstalk环境Puma Rails 5,ruby-on-rails,amazon-web-services,redirect,amazon-elastic-beanstalk,puma,Ruby On Rails,Amazon Web Services,Redirect,Amazon Elastic Beanstalk,Puma,我无法重定向到。我尝试了各种配置,但没有任何效果 根据研究,我意识到我需要将其添加到nginx配置中 if ($http_x_forwarded_proto != 'https') { rewrite ^ https://$host$request_uri? permanent; } 我在..ebextensions目录中创建了一个新的配置文件,内容如下: upstream my_app { server unix:///var/run/puma/my_

我无法重定向到。我尝试了各种配置,但没有任何效果

根据研究,我意识到我需要将其添加到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;
  }
}
保存它,然后去做

  • 电子商务部署
  • 后藤
  • 我仍然得到“不确定”的信息

    我也使用的内容是从。但这也不起作用

    我错过了什么


    Sunil

    使用EB文件密钥定制nginx配置生成模板。

    创建一个包含以下内容的文件.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强制ssl选项

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

    在您的代码中,我看不到listen to 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;
      }
    }
    

    您还可以通过检查头“X-Forwarded-Proto”@vlad在rails中使用force_ssl,不确定是否需要侦听端口443,因为通常elasticbeanstalk与负载平衡器耦合,负载平衡器处理ssl并将请求路由到具有附加X-Forwarded-Proto头的实例。(没有足够的代表对您的答案发表评论:P)我确实强制了您的ssl选项,并且它起了作用。我在ebextention选项上也有足够的方向来尝试。谢谢我将结束这个问题。我通过EB web控制台在443上配置了HTTPS。