首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HTTPS重定向弹性豆柄环境美洲狮Rails 5

HTTPS重定向弹性豆柄环境美洲狮Rails 5
EN

Stack Overflow用户
提问于 2017-11-19 07:27:13
回答 3查看 773关注 0票数 0

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

基于这项研究,我意识到我需要将它添加到nginx配置中。

代码语言:javascript
复制
if ($http_x_forwarded_proto != 'https') {
          rewrite ^ https://$host$request_uri? permanent;
        }

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

代码语言:javascript
复制
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. Goto http://example.com

我还是收到了“不确定”的信息。

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

我遗漏了什么?

苏尼尔

EN

回答 3

Stack Overflow用户

发布于 2017-11-19 08:31:09

在您的代码中,我没有看到侦听443,即HTTPS。

这是我的SSL脚本。

代码语言:javascript
复制
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;
  }
}
票数 1
EN

Stack Overflow用户

发布于 2017-11-19 08:24:52

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

创建一个文件.eb扩展名/01_puma_nginx.conf(配置文件按字母顺序执行,因此根据其他要求调整名称前缀),内容如下。

代码语言:javascript
复制
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转发-Proto”头来使用rails force_ssl选项。

代码语言:javascript
复制
  force_ssl if: :ssl_required?

  def ssl_required?
    if request.headers["X-Forwarded-Proto"]!="https"
      true
    else
      false
    end
  end
票数 0
EN

Stack Overflow用户

发布于 2021-07-11 12:27:45

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47374610

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档