LoginSignup
10
11

More than 5 years have passed since last update.

AWS Elastic Beanstalk、Nginx + Passenger (standalone)の環境設定

Last updated at Posted at 2016-02-08

経緯

AWSのElasticBeanstalk(以下EB)を使って、Railsをデプロイする。
Nginxの設定を追加しようと思い、nginx.confファイルを変更してみても全然うまくいかない。どころか、まったく反映されず、エラーも出ない。ハマってしまったので、nginxの設定をする場合の方法をまとめてみた。

環境

$ eb initで色々と初期設定をしますが、選んだのはNginx + Passengerです。
Ruby 2.2 (Passenger Standalone)

すっかり見逃してたのですが、Passengerはstandaloneのものでした。

失敗

/etc/nginx/nginx.confを変更して、Nginxをリスタートすれば良いだろうと思っていたのですが、これではうまく行きません。

なぜなら、standaloneだから…。(これに気付くまで、何度も.ebextentionsにファイルを書いてデプロイして、かなり無駄な時間を費やしました)

Passenger (Standalone)の設定方法を調べる

standaloneを調べます。

概念図

ソース:https://www.phusionpassenger.com/library/indepth/integration_modes.html

Nginxはリバースプロキシだけなのかと思いきや、Passenger側がコントロールしてる(?)ようです。
nginxのconfファイルを指定してpassengerを起動できます。

$ passenger start --nginx-config-template nginx.conf.erb

で、このpassenger用のconfig.erbファイルがあるのですが、AWS-EBが使用するのは別のファイルでした。

AWS-EBのPassenger (standalone)用のnginx_configファイル

調べたところ、このnginx_config_healthd.erb を使用しているようです。

# etc/init.d/passenger
if [ -d /etc/healthd ]; then
    STARTOPTS="--nginx-version $EB_NGINX_VERSION --nginx-config-template $EB_SUPPORT_DIR/conf/nginx_config_healthd.erb"
else
    STARTOPTS="--nginx-version $EB_NGINX_VERSION --nginx-config-template $EB_SUPPORT_DIR/conf/nginx_config.erb"
fi

実際の場所は、/opt/elasticbeanstalk/support/conf/nginx_config_healthd.erbにあります。

なので、このファイルを.ebextensionsで変更するようにして上げればうまく行きました。

.ebextensions の設定例

.ebextensions内に.configファイルを作成してあげます。

files:
    "/opt/elasticbeanstalk/hooks/appdeploy/post/99_restart_app_server.sh" :
        mode: "000777"
        owner: root
        group: root
        content: |
            #!/usr/bin/env bash
            service passenger restart
    "/opt/elasticbeanstalk/support/conf/nginx_config_healthd.erb" :
        mode: "000644"
        owner: root
        group: root
        content: |
            (nginxの設定内容を記述)

上の方のファイルは、passengerの再起動をさせるファイルです。appdeployのpost(後)に実行されます。
下のファイルがnginxの設定ファイルです。中身は元のnginx_config_healthd.erbを参考にしてください。

これで$ eb deployすれば、うまく行くと思います!
(ほかに良い方法があればコメントもらえると嬉しいです)

10
11
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
10
11