7

我知道这个问题已被问过,但似乎没有任何工作对我来说。我试过多种不同的东西,比如在这些问题中描述的答案:Elastic Beanstalk Http重定向到Https

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

他们都不似乎工作。我是aws noob,所以我不完全确定如何编辑配置文件 - 或者如果我做错了什么。

我的设置如下:

我现在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在重定向之前呈现,我想重定向到负载平衡器 - 它应该重定向。

+0

一目了然,你Nginx的配置看起来是正确的。它正在检查x-forward-proto头,如果不是“https”则重定向。它看起来像你也从裸体域重定向到www子域,是否工作?你确定nginx配置实际上是应用到你的beanstalk服务器吗? –

+0

实际上,我刚刚在路由53中将裸域指向www,然后www指向EB。所以这个检查目前是多余的。我将如何检查此文件是否覆盖默认文件? – KDogg

+1

[你做了什么KDogg](https://xkcd.com/979/)?我有同样的问题! –

回答

2

看起来您正在尝试为非WWW和非HTTPS连接执行重定向。您是否尝试过更简单的http:// - > https://?

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

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

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

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