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" :