web-dev-qa-db-ja.com

Elastic Beanstalk nginx-backedプロキシサーバーを取得してHTTPからHTTPSに自動リダイレクトする方法は?

Amazon Elastic Beanstalkで実行しているNode.jsを使用したサイトがあります。

Node.jsアプリはポート8080でリッスンし、EBアプリでnginxエラスティックロードバランサー構成を使用して、HTTPおよびHTTPSのポート80および443でリッスンしています。

ただし、HTTPS経由で送信されたトラフィックのみをアプリで受け入れたいです。

これに対処するためにアプリに何かを作り上げることもできますが、ロードバランサーがすべてのHTTPリクエストをHTTPS経由でサイトにリダイレクトする方法に興味があります。

58
Mason G. Zhwiti

アマゾンの有料サポートからのアイデアでいくつかの誤ったスタートをした後、彼らは最終的に成功しました。これを機能させる方法は、ポート80と443の両方に応答するように環境を設定することです。次に、.ebextensionsという名前のNode.jsアプリフォルダーにフォルダーを作成し、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

Amazonのサポートチームは説明しました:この設定は、/ etc/nginx/conf.d/00_elastic_beanstalk_proxy.confに書き換えルールを追加するデプロイメントフックを作成します。

(以前は、別のファイルを/etc/nginx/conf.dにコピーする.configを提供していましたが、それらは効果がないか、さらに悪いことに、何らかの理由でデフォルトの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

これが将来他の誰かに役立つことを願っています。

80
Mason G. Zhwiti

受け入れられた答えは私にとってはもはや機能しませんでした。デフォルトのポートは別のものでした。また、構成ファイルの場所が変更されました。私はRuby on RailsアプリケーションをPumaで設定しています。

有料サポートと話をしましたが、実行中のインスタンスで手動でコマンドを実行するだけで解決できました。次に、以下の解決策を見つけることができました。ログインして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

ポート番号と構成ファイルの場所を変更したことに注意してください。

19
Hendrik

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();
});
9
Elad Nava

少し簡単なソリューションでこれを機能させることができました。

これは、Elastic Beanstalkが展開したSINGLEインスタンスであり、負荷分散されていないことに注意してください。

これは私が追加した拡張機能でした。

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;
      }
6
elloworld111

AWS Elastic Beanstalkで「Ruby2 Puma」環境を実行していますが、これは上記とわずかに異なる設定を持つ場合があります。私の環境では、「listen 8080」の代わりに「listen 80」を使用する必要がありました。

elloworld111's answer に基づくsslredirect.config:

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;
      }
5
Adam D

私は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
4
Iain Hunter

これを別の方法で動作させることができました。ポート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--;
      }
2
RobO

受け入れられた答えは私にはうまくいきませんでした。何回も(そして何時間もグーグルで)試してみたところ、私にとってうまくいった何かを見つけました。私も、Elastic Beanstalkで実行しているNode.jsベースのサイトを持っています。

ここからスクリプトを使用しました: 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を再起動するだけで設定が完了します。

0
Matija Martic