簡體   English   中英

如何讓Elastic Beanstalk nginx支持的代理服務器從HTTP自動重定向到HTTPS?

[英]How to get Elastic Beanstalk nginx-backed proxy server to auto-redirect from HTTP to HTTPS?

我有一個Node.js支持的網站,我在Amazon Elastic Beanstalk上運行。

我的Node.js應用程序偵聽端口8080,我在我的EB應用程序中使用nginx彈性負載均衡器配置,在端口80和443上偵聽HTTP和HTTPS。

但是,我只想接受通過HTTPS發送的應用中的流量。

我可以在應用程序中安裝一些東西來解決這個問題,但是我有興趣讓負載均衡器通過HTTPS將所有HTTP請求重定向到我的站點。

經過亞馬遜付費支持的幾次虛假啟動后,他們最終確實通過了。 您可以通過以下方式配置環境以響應端口80和443.然后在主Node.js應用程序文件夾中創建一個名為.ebextensions文件夾,並在其中放置一個名為00_nginx_https_rw.config的文件,本文內容如下:

files:
  "/tmp/45_nginx_https_rw.sh":
    owner: root
    group: root
    mode: "000644"
    content: |
      #! /bin/bash

      CONFIGURED=`grep -c "return 301 https" /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf`

      if [ $CONFIGURED = 0 ]
        then
          sed -i '/listen 8080;/a \    if ($http_x_forwarded_proto = "http") { return 301 https://$host$request_uri; }\n' /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf
          logger -t nginx_rw "https rewrite rules added"
          exit 0
        else
          logger -t nginx_rw "https rewrite rules already set"
          exit 0
      fi

container_commands:
  00_appdeploy_rewrite_hook:
    command: cp -v /tmp/45_nginx_https_rw.sh /opt/elasticbeanstalk/hooks/appdeploy/enact
  01_configdeploy_rewrite_hook:
    command: cp -v /tmp/45_nginx_https_rw.sh /opt/elasticbeanstalk/hooks/configdeploy/enact
  02_rewrite_hook_perms:
    command: chmod 755 /opt/elasticbeanstalk/hooks/appdeploy/enact/45_nginx_https_rw.sh /opt/elasticbeanstalk/hooks/configdeploy/enact/45_nginx_https_rw.sh
  03_rewrite_hook_ownership:
    command: chown root:users /opt/elasticbeanstalk/hooks/appdeploy/enact/45_nginx_https_rw.sh /opt/elasticbeanstalk/hooks/configdeploy/enact/45_nginx_https_rw.sh

亞馬遜的支持團隊解釋說:這個配置創建了一個部署鈎子,它將重寫規則添加到/etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf。

(之前他們曾經提供過.config,它將單獨的文件復制到/etc/nginx/conf.d中,但是由於某種原因,這些文件要么沒有效果,要么更糟糕,似乎覆蓋或優先於默認的nginx配置。)

如果您想要撤消此操作,即刪除掛鈎,則需要刪除此ebextension並發出命令以刪除它創建的文件。 您可以手動執行此操作,也可以通過臨時安裝的ebextensions命令執行此操作:

/opt/elasticbeanstalk/hooks/appdeploy/enact/45_nginx_https_rw.sh
/opt/elasticbeanstalk/hooks/configdeploy/enact/45_nginx_https_rw.sh

我沒有試過這個,但可能這樣的東西可以刪除它們並撤消這個改變:

container_commands:
  00_undochange:
    command: rm /opt/elasticbeanstalk/hooks/appdeploy/enact/45_nginx_https_rw.sh
  01_undochange:
    command: rm /opt/elasticbeanstalk/hooks/configdeploy/enact/45_nginx_https_rw.sh

希望這可以在將來幫助別人。

接受的答案對我來說不再適用。 默認端口是另一個端口。 此外,配置文件的位置已更改。 我正在使用Puma設置Ruby On Rails應用程序。

我談到付費支持,我們通過在運行的實例上手動運行命令來解決這個問題。 然后我能夠找出以下解決方案。 只需登錄並重新啟動nginx即可。

files:
  "/tmp/45_nginx_https_rw.sh":
    owner: root
    group: root
    mode: "000644"
    content: |
      #! /bin/bash

      CONFIGURED=`grep -c "return 301 https" /opt/elasticbeanstalk/support/conf/webapp_healthd.conf`

      if [ $CONFIGURED = 0 ]
        then
          sed -i '/listen 80;/a \    if ($http_x_forwarded_proto = "http") { return 301 https://$host$request_uri; }\n' /opt/elasticbeanstalk/support/conf/webapp_healthd.conf
          logger -t nginx_rw "https rewrite rules added"
          exit 0
        else
          logger -t nginx_rw "https rewrite rules already set"
          exit 0
      fi

container_commands:
  00_appdeploy_rewrite_hook:
    command: cp -v /tmp/45_nginx_https_rw.sh /opt/elasticbeanstalk/hooks/appdeploy/enact
  01_configdeploy_rewrite_hook:
    command: cp -v /tmp/45_nginx_https_rw.sh /opt/elasticbeanstalk/hooks/configdeploy/enact
  02_rewrite_hook_perms:
    command: chmod 755 /opt/elasticbeanstalk/hooks/appdeploy/enact/45_nginx_https_rw.sh /opt/elasticbeanstalk/hooks/configdeploy/enact/45_nginx_https_rw.sh
  03_rewrite_hook_ownership:
    command: chown root:users /opt/elasticbeanstalk/hooks/appdeploy/enact/45_nginx_https_rw.sh /opt/elasticbeanstalk/hooks/configdeploy/enact/45_nginx_https_rw.sh

請注意我是如何更改端口號和配置文件的位置的。

您可以通過Node.js應用程序處理重定向。

當客戶端不安全地連接時,Amazon會發送X-Forwarded-Proto標頭,該標頭等於http

應在初始化Express和定義路由之前立即插入以下中間件,以自動將客戶端重定向到相應的HTTPS端點:

// Redirect to HTTPS
app.use(function (req, res, next) {
    // Insecure request?
    if (req.get('x-forwarded-proto') == 'http') {
        // Redirect to https://
        return res.redirect('https://' + req.get('host') + req.url);
    }

    next();
});

我能夠通過一個稍微簡單的解決方案來解決這個問題。

請注意,這是一個部署了SINGLE實例的彈性beanstalk,沒有加載balenced。

這是我添加的ebextension。

files:
  "/etc/nginx/conf.d/000_my_config.conf":
    mode: "000755"
    owner: root
    owner: root
    content: |
      server {
          listen 8080;
          return 301 https://$host$request_uri;
      }

我在AWS Elastic Beanstalk上運行'Ruby2 Puma'環境,可能與上面的配置略有不同。 在我的環境中,我需要使用'listen 80'而不是'listen 8080'。

sslredirect.config基於elloworld111的回答

files:
  "/etc/nginx/conf.d/000_my_config.conf":
    mode: "000755"
    owner: root
    owner: root
    content: |
      server {
          listen 80;
          return 301 https://$host$request_uri;
      }

我正在使用Elastic Beanstalk和Docker,所以采取了一種稍微不同的路徑來為我運行,但很大程度上受到了接受的答案的啟發。 此腳本將所需的配置注入/etc/nginx/sites-available/elasticbeanstalk-nginx-docker-proxy.conf。 (如果有人有更優雅的解決方案,我很樂意看到它)

此腳本還使Beanstalk運行狀況檢查能夠訪問我的運行狀況檢查端點(在我的情況下為api / healthcheck)更好地允許LoadBalancer命中應用程序,而不是終止於Nginx。

files:
  "/tmp/45_nginx_https_rw.sh":
    owner: root
    group: root
    mode: "000755"
    content: |
      #! /bin/bash

      CONFIGURED=`grep -c "return 301 https" /etc/nginx/sites-available/elasticbeanstalk-nginx-docker-proxy.conf`

      if [ $CONFIGURED = 0 ]
        then
          sed -i "/access.log;/a \ \ \ \ \ \ \ \ location /api/health-check { proxy_pass http://docker; }" /etc/nginx/sites-available/elasticbeanstalk-nginx-docker-proxy.conf
          sed -i "/proxy_add_x_forwarded_for;/a \ \ \ \ \ \ \ \ \ \ \ \ if (\$http_x_forwarded_proto != 'https') { return 301 https://\$host\$request_uri; }" /etc/nginx/sites-available/elasticbeanstalk-nginx-docker-proxy.conf
          logger -t nginx_rw "https rewrite rules added"
          exit 0
        else
          logger -t nginx_rw "https rewrite rules already set"
          exit 0
      fi

container_commands:
  00_run_script:
    command: /tmp/45_nginx_https_rw.sh

我能夠以不同的方式使用它。 我更改了負載均衡器以將端口80流量轉發到端口8082,並更改了防火牆規則(實例上的入站,防火牆上的出站)以允許這樣做。 然后在.ebextensions中添加此文件:

files:
  "/etc/nginx/conf.d/50-atd-hotel-http-redirect.conf":
    mode: "000644"
    owner: root
    group: root
    content: |
      server {
        listen   8082;

        return 301 --WHATEVER DESTINATION YOU WANT--;
      }

接受的答案對我不起作用。 經過多次嘗試(以及幾小時的谷歌搜索),我發現了一些對我有用的東西。 我也有一個Node.js支持的網站,我在Elastic Beanstalk上運行。

我使用了這里的腳本: https//adamjstevenson.com/tutorials/2017/02/02/configuring-and-forcing-https-for-aws-elastic-beanstalk.html

我做的唯一修改就是換掉了

/opt/elasticbeanstalk/support/conf/webapp_healthd.conf

通過

/etc/nginx/sites-available/elasticbeanstalk-nginx-docker-proxy.conf

所以它給出了這個:

files:
  "/tmp/45_nginx_https_rw.sh":
    owner: root
    group: root
    mode: "000644"
    content: |
      #! /bin/bash

      CONFIGURED=`grep -c "return 301 https" /etc/nginx/sites-available/elasticbeanstalk-nginx-docker-proxy.conf`

      if [ $CONFIGURED = 0 ]
        then
          sed -i '/listen 80;/a \    if ($http_x_forwarded_proto = "http") { return 301 https://$host$request_uri; }\n' /etc/nginx/sites-available/elasticbeanstalk-nginx-docker-proxy.conf
          logger -t nginx_rw "https rewrite rules added"
          exit 0
        else
          logger -t nginx_rw "https rewrite rules already set"
          exit 0
      fi

container_commands:
  00_appdeploy_rewrite_hook:
    command: cp -v /tmp/45_nginx_https_rw.sh /opt/elasticbeanstalk/hooks/appdeploy/enact
  01_configdeploy_rewrite_hook:
    command: cp -v /tmp/45_nginx_https_rw.sh /opt/elasticbeanstalk/hooks/configdeploy/enact
  02_rewrite_hook_perms:
    command: chmod 755 /opt/elasticbeanstalk/hooks/appdeploy/enact/45_nginx_https_rw.sh /opt/elasticbeanstalk/hooks/configdeploy/enact/45_nginx_https_rw.sh
  03_rewrite_hook_ownership:
    command: chown root:users /opt/elasticbeanstalk/hooks/appdeploy/enact/45_nginx_https_rw.sh /opt/elasticbeanstalk/hooks/configdeploy/enact/45_nginx_https_rw.sh

eb deploy ,只需重新啟動你的nginx sudo service nginx restart就可以了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM