LoginSignup
2
4

More than 5 years have passed since last update.

Elastic Beanstalk(Nodeプラットフォーム)でhttpアクセスをhttpsアクセスにリダイレクトする

Posted at

前提

  • Beanstalkを使っている
  • Nodeプラットフォームを選択している
  • Beanstalkのソフトウェア設定内でプロキシサーバにnginxを設定している image.png
  • Beanstalkのロードバランサー設定でhttp / httpsともにポート開放しており、httpsについては証明書設定も完了している(容量設定で環境タイプを負荷分散にしないといけないかも?)
    • httpアクセスもhttpsアクセスも普通にできる状態ができていること image.png

設定内容

nginx設定を変更するため、プロジェクトのルートに.ebebextensionsディレクトリを作成し、その中に configファイルを用意します。

ebextensions/proxy.config
files:
  /etc/nginx/conf.d/proxy.conf:
    mode: "000644"
    owner: root
    group: root
    content: |
      upstream nodejs {
        server 127.0.0.1:8081;
        keepalive 256;
      }

      server {
        listen 8080;


        if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
          set $year $1;
          set $month $2;
          set $day $3;
          set $hour $4;
        }
        access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;
        access_log  /var/log/nginx/access.log  main;


        if ($http_x_forwarded_proto != 'https') {
          rewrite ^(.*) https://$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;
        gzip_comp_level 4;
        gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
      }

  /opt/elasticbeanstalk/hooks/configdeploy/post/99_kill_default_nginx.sh:
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/bin/bash -xe
      rm -f /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf
      service nginx stop 
      service nginx start

container_commands:
 removeconfig:
    command: "rm -f /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf"

あとはBeanstalkでデプロイすればOK!
httpへのアクセスがhttpsにリライトされるはずです。

詳細解説

BeanstalkにデプロイしたアプリケーションはEC2インスタンスに展開されます。
基本的にはEC2インスタンスにログインし、/etc/nginx/conf.dあたりを直接見るとデフォルトの設定状況などがよくわかるかと思います。

今回の設定内容の詳細は以下のとおりです。Beanstalkの基本的な設定としてfiles:で書かれたファイルがデプロイ時に作成されることは理解しておきましょう。

  1. proxy.configを作成してEC2の/etc/nginx/conf.d以下に配置する
  2. デフォルトの設定である00_elastic_beanstalk_proxy.confを削除 → rm -f /etc/nginx/conf.d/00_elastic_beanstalk_proxy.confremoveconfigあたり
  3. nginxの再起動 → /opt/elasticbeanstalk/hooks/configdeploy/postはデプロイ時のフック処理という意味です

proxy.configの中身の大半は実際には00_elastic_beanstalk_proxy.confであり、以下の数行を追加したのみです。

if ($http_x_forwarded_proto != 'https') {
  rewrite ^(.*) https://$host$1 redirect;
}

どうやって試したか

上記設定を確認するためにいちいち.ebebextensions内のファイルを編集してデプロイしていたのでは時間がかかりすぎてしまいます。(1デプロイあたりで10分とか?)

そのため自分は、対象のEC2インスタンスに直接sshログインをして直接ファイル編集とnginxの再起動をして動作確認を行い、その内容を.ebebextensions内に反映するという方法を取りました。

# EC2へログイン
$ ssh -i my_ec2_key.pem ec2-user@XX.XX.XX.XX

# EC2上
$ sudo vim /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf
# ↑`rewrite`設定を直書きしてみる
$ sudo nginx -s reload
# アクセスしてみる!できた!→プロジェクトの`.ebebextensions`を変更してデプロイ

この方法が正しいのかどうかはわかりませんのでご参考まで...

参考資料

実はものすごくハマってしまったのですが、最終的にはAWSの公式ドキュメントとnginxの基本的な設定だけ把握していれば解決できました。

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4