LoginSignup
4
2

More than 5 years have passed since last update.

Go/Java SE環境のElastic BeanstalkでHTTPをHTTPSにリダイレクトする

Posted at

まとめるまでもなかったのかもしれませんが自分がかなりハマってしまったので書いておきたいと思います。

前提

基本的なSSLのセットアップができている前提で進めていきます。Elastic Beanstalk (EB) の環境にElastic Load Balancer (ELB) が紐付けされており、ELBに対して適切なSSL証明書とHTTPS/ポートの設定されているか今一度ご確認ください。

リダイレクトの設定

ELBは受け取ったリクエストのプロトコルをX-Forwarded-Protoヘッダに格納してあるため以下の設定をnginxのサーバブロック内に読み込ませればHTTP→HTTPSのリダイレクトが実現できます。

if ($http_x_forwarded_proto != 'https') {
    rewrite ^ https://$host$request_uri? permanent;
}

nginxの設定

では肝心のnginxの設定はどうやって上書きするのか。Go/Java SE環境では簡単にnginxの設定をいじくるための仕組みが導入されています。どちらも設定方法は同じです。

.ebextensions/nginx/nginx.confというファイルを作ってEBでデプロイするアプリケーションのルートディレクトリに置いておくとこのnginx.confがEC2上の/etc/nginx/nginx.confに上書きされます。

もう一つ、.ebextensions/nginx/conf.d/*.confにファイルを置いておくとこれらのファイルが/etc/nginx/conf.d/elasticbeanstalkコピーされ、nginx.confを上書きしていない場合はその中のserver{}ブロックの中で、あるいはnginx.confを上書きしている場合はinclude conf.d/elasticbeanstalk/*.conf;と記述すればそこで読み込んでくれますが、なぜか上記のリダイレクト設定を記述したファイルを.ebextensions/nginx/conf.d/02_proxy.confとして置くとnginxの設定ファイル読み込み時にif文が許されず弾かれます。

注意

CodeBuildを噛ましている場合、artifactを出力する際に.ebextensionsを出力するのを忘れてしまうと当然コピーされないのでご注意を。

nginx.confの準備

そんなわけでincludeされるファイルに記述してもうまくいかないので既存のnginxの設定ファイルに上書きしてしまいます。server{}ブロック内に上記のリダイレクト設定を書き込むと以下のようになります。

# Elastic Beanstalk Nginx Configuration File

user                    nginx;
error_log               /var/log/nginx/error.log warn;
pid                     /var/run/nginx.pid;
worker_processes        auto;
worker_rlimit_nofile    33193;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    include       conf.d/*.conf;

    map $http_upgrade $connection_upgrade {
        default     "upgrade";
    }

    server {
        listen        80 default_server;
        access_log    /var/log/nginx/access.log main;

        client_header_timeout 60;
        client_body_timeout   60;
        keepalive_timeout     60;
        gzip                  off;
        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;

        # Redirect to https
        if ($http_x_forwarded_proto != 'https') {
           rewrite ^ https://$host$request_uri? permanent;
        }

        # Include the Elastic Beanstalk generated locations
        include conf.d/elasticbeanstalk/*.conf;
    }
}

上記をコピペして.ebextensions/nginx/nginx.confとして保存してやればHTTP→HTTPSリダイレクトが有効になるはずです。

4
2
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
4
2