0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Elastic BeanstalkでヘルスチェックパスへのアクセスをALBのみにする

Last updated at Posted at 2020-12-27

ヘルスチェックパスへのアクセスをALBのみにしたかったので、BeanstalkのデフォルトのNginxの設定を上書きすることで対応しました。

環境

Elastic Beanstalk Node.js 12.19.0
プロキシサーバー Nginx 1.18.0

デフォルトのNginxの設定の上書き

  • デフォルトのNginxの設定を上書きする方法は、公式ドキュメントにあります。 .ebextensionsに上書きしたいファイルをおき、container_commandsでデフォルトの設定を削除します。

  • ドキュメント以外にも、AWSが上書き用のサンプルを提供しています。サンプルでは、httpで来たアクセスをhttpsにリダイレクトさせるための設定がされています。

  • 今回は、サンプルを少し変更して、locationの中で/healthにアクセスがきた場合にALBによるアクセスでなければリダイレクトさせる設定をしました。

.ebextensions/proxy.conf
files:
   /etc/nginx/conf.d/proxy.conf:
     owner: root
     group: root
     mode: "000644"
     content: |
       # Elastic Beanstalk Managed

       # Elastic Beanstalk managed configuration file
       # Some configuration of nginx can be by placing files in /etc/nginx/conf.d
       # using Configuration Files.
       # http://docs.amazonwebservices.com/elasticbeanstalk/latest/dg/customize-containers.html


       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;


           location / {
               set $redirect 0;
               if ($request_uri = "/health") {
                 set $redirect 1;
               }
               if ($http_user_agent ~* "ELB-HealthChecker") {  //ロードバランサー以外のヘルスチェックパスへのアクセスはリダイレクトさせる
                 set $redirect 0;
               }
               if ($redirect = 1) {
                 return 301 https://$host;
               }

               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/html text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

       }

   /opt/elasticbeanstalk/hooks/configdeploy/post/99_kill_default_nginx.sh:
     owner: root
     group: root
     mode: "000755"
     content: |
       #!/bin/bash -xe
       rm -f /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf
       if [[ -e /etc/init/nginx.conf ]] ; then
         echo Using initctl to stop and start nginx
         initctl stop nginx || true
         initctl start nginx
       else
         echo Using service to stop and start nginx
         service nginx stop
         service nginx start
       fi

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"

参考

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?