【问题标题】:Elastic Beanstalk Http Redirect to HttpsElastic Beanstalk Http 重定向到 Https
【发布时间】:2016-06-19 10:42:34
【问题描述】:

我知道以前有人问过这个问题,但似乎没有什么对我有用。我尝试了多种不同的方法,例如这些问题中描述的答案:

How to get Elastic Beanstalk nginx-backed proxy server to auto-redirect from HTTP to HTTPS? Redirecting EC2 elb from http to https

它们似乎都不起作用。我是个新手,所以我不完全确定编辑配置文件的工作原理——或者我是否做错了什么。

我的设置如下:

我的 .ebextensions 文件夹中的当前 nginx.config 文件(从 this article 获取):

files:
  "/tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf" :
    mode: "000755"
    owner: root
    group: root
    content: |
        upstream nodejs {
            server 127.0.0.1:8081;
            keepalive 256;
        }
        server {
            listen 8080;
            set $fixedWWW '';
            set $needRedir 0;
            # nginx does not allow nested if statements
            # check and decide on adding www prefix
            if ($host !~* ^www(.*)) {
                set $fixedWWW 'www.';
                set $needRedir 1;
            }
            # what about that https? the traffic is all http right now
            # but elastic load balancer tells us about the original scheme
            # using $http_x_forwarded_proto variable
            if ($http_x_forwarded_proto != 'https') {
                set $needRedir 1;
            }
            # ok, so whats the verdict, do we need to redirect?
            if ($needRedir = 1) {
                rewrite ^(.*) https://$fixedWWW$host$1 redirect;
            }
            location / {
                proxy_pass  http://nodejs;
                proxy_set_header   Connection "";
                proxy_http_version 1.1;
                proxy_set_header        Host            $host;
                proxy_set_header        X-Real-IP       $remote_addr;
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            }
            gzip on;
        }

但这似乎没有任何作用。我已经没有想法了。我不确定我是否错过了一个步骤或什么,但我不知道该怎么做。作为一种解决方法,我有我的 angularjs 前端重定向非 https 请求,但这太 hacky 并且一些 DOM 在重定向之前呈现,我想在负载均衡器处重定向 - 它应该重定向。

【问题讨论】:

  • 一目了然,您的 Nginx 配置看起来是正确的。它正在检查 x-forwarded-proto 标头,如果不是“https”则重定向。看起来您也在从裸域重定向到 www 子域,这有效吗?您确定 nginx 配置实际上已应用于您的 beanstalk 服务器吗?
  • 我实际上只是将裸域指向路由 53 中的 www,然后 www 指向 EB。因此,该检查目前是多余的。如何检查此文件是否覆盖默认文件?
  • What did you do KDogg??我也有同样的问题!
  • 我从来没有真正弄清楚该怎么做:P 我只是在我的 html 文件头部的第一个标记中放置了一个 javascript 重定向。它足够快,不会真正影响加载时间。对不起!如果你弄清楚了,我很想知道

标签: amazon-web-services nginx https amazon-elastic-beanstalk amazon-elb


【解决方案1】:

您似乎正在尝试对非 WWW 和非 HTTPS 连接进行重定向。您是否尝试过 http:// -> https:// 的简单情况?

if ($http_x_forwarded_proto = "http") {
    return 301 https://$host$request_uri;
}

有时通过两种重定向更容易处理它,一种从 HTTP 到 HTTPS,另一种从非 WWW 到 WWW。事实上,如果您要通过 HSTS (https-everywhere) 注册您的网站,他们需要这种方法。

编辑:另外,刚刚注意到您的配置的第一行,您可能想尝试直接注入 nginx 文件:

files:
  "/etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf" :

【讨论】:

    【解决方案2】:

    直接更新 /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf" 非常困难。我发现了这个:https://github.com/awsdocs/elastic-beanstalk-samples/blob/master/configuration-files/aws-provided/security-configuration/https-redirect/nodejs/https-redirect-nodejs.config,它可以让你设置重定向,但那样会有更改了我的其他配置文件太多。最好的方法是在您的 .ebextensions 文件夹中创建一个 redirect.config 文件:

    container_commands:
      https_redirect:
        command: |
         sed -i '/location \/ {/i \
                  set $redirect 0;\
                  if ($http_x_forwarded_proto != "https") {\
                    set $redirect 1;\
                  }\
                  if ($http_user_agent ~* "ELB-HealthChecker") {\
                    set $redirect 0;\
                  }\
                  if ($redirect = 1) {\
                    return 301 https://$host$request_uri;\
                  }\
          ' /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf```
    

    【讨论】:

      猜你喜欢
      • 2018-01-01
      • 2021-02-23
      • 2019-09-21
      • 2018-01-07
      • 2019-01-24
      • 2020-02-08
      • 2017-02-27
      • 2014-07-05
      • 2017-02-04
      相关资源
      最近更新 更多