【问题标题】:Redirect http traffic to https using Nginx on ELB在 ELB 上使用 Nginx 将 http 流量重定向到 https
【发布时间】:2017-10-16 06:10:37
【问题描述】:

我在尝试将我的 http 流量发送到 https 时遇到了一些麻烦。所以我使用 AWS 的 Elastic Beanstalk 来部署 Rails 5 应用程序,名为 Eightysixpad.me。我已经配置了 SSL,所以当你转到 https://www.eightysixpad.me 时,它说安全,我很高兴;但是,当您访问 http:// 时,它显示不安全,我无法弄清楚如何重定向流量。

我对 Nginx 和 Web 应用程序非常陌生,因此我们将不胜感激!我已经 ssh 进入我的 EC2 实例,并尝试使用以下配置设置配置 /etc/nginx/nginx.conf 中的 Nginx 配置文件。

server {
    listen         80;
    server_name   eightysixpad.me;
    if ($http_x_forwarded_proto != "https") {
      rewrite ^(.*)$ https://$server_name$1 permanent;
    }
    root         /usr/share/nginx/html;
    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

但是运气不好 :( 我很确定我在正确的文件中,但是如果我做出任何我认为会破坏网站并且无法正常加载的更改,那么该网站仍然有效。所以我的第一个问题是,这是正确的文件 /etc/nginx/nginx.conf 如果不是 nginx 配置保存在哪里?其次,如果它是正确的文件,我做错了什么?

任何其他问题或更多信息请告诉我!

提前感谢您!

【问题讨论】:

    标签: ruby-on-rails amazon-web-services nginx amazon-ec2 amazon-elastic-beanstalk


    【解决方案1】:

    您怀疑/etc/nginx/nginx.conf 不是进行更改的正确位置是正确的。 ElasticBeanstalk 使用单独的位置来存储 nginx 配置文件,即/opt/elasticbeanstalk/support/conf。在该目录中,您会找到名为 nginx_config.erbnginx_config_healthd.erb 的文件,它们是用于在运行时动态生成 nginx 配置的模板。

    话虽如此,您根本不需要处理配置文件。如果您在production.rb 中设置config.force_ssl = true,则所有http 连接都应重定向到https。详情请见the documentation

    【讨论】:

      【解决方案2】:

      Scott Bradley 在以下链接中的博客很好地总结了该问题以及如何在 ELB 后面的 nginx 上解决该问题:

      Always-On HTTPS With Nginx Behind an ELB

      这个解决方案有两个主要组成部分:

      运行状况检查 URL 的特定位置指令 执行任何 HTTPS 强制。 X-Forwarded-Proto 的重定向:https 标头不存在。为了获得最佳实践,我们可以添加 HTTP Strict 此处也使用 add_header 指令传输安全性。下面是一个 演示这些的简化 nginx 配置文件示例。

      upstream unicorn {
        server localhost:3000;
      }
      
      server {
        listen 90;
        server_name example.com;
        root /var/www/html;
      
        # 1) Special, somewhat redundant location to always proxy
        #    the health check to the upstream server, without checking
        #    if the request came in over HTTP or HTTPS.
        location /health_check {
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $http_host;
          proxy_redirect off;
          proxy_next_upstream error;
          proxy_pass http://unicorn;
          break;
        }
      
        # Our main location to proxy everything else to the upstream
        # server, but with the added logic for enforcing HTTPS.
        location / {
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $http_host;
          proxy_redirect off;
          proxy_next_upstream error;
      
          # 2) Any request that did not originally come in to the ELB
          #    over HTTPS gets redirected.
          if ($http_x_forwarded_proto != "https") {
            rewrite ^(.*)$ https://$server_name$1 permanent;
          }
      
          proxy_pass http://unicorn;
      
          # Add HTTP Strict Transport Security for good measure.
          add_header Strict-Transport-Security "max-age=31536000; includeSubDomains;";
        }
      }
      

      【讨论】:

      • 您能否从该链接中为您的答案添加相关摘录?链接往往会随着时间的推移而消失。
      猜你喜欢
      • 2018-08-18
      • 1970-01-01
      • 2016-07-27
      • 1970-01-01
      • 2019-08-26
      • 2018-09-30
      • 1970-01-01
      • 2015-12-06
      • 2011-03-29
      相关资源
      最近更新 更多