前回capistranoで2台のproductionサーバーにdeployした続きの記事です。
メモ書き程度に書いておきます。 とりあえず、ELBを用意しました。ELBでEC2の2台を繋げてあげて出来るかなと思ったのですが挙動が変でした。(heathチェックを/healthにしてました) (Nginx + Unicorn + Rails)
それで結論からいうと、最終的には以下のようなconfになりました。
/etc/nginx/conf.d/horoyoi.conf
# log directory error_log /var/www/rails/horoyoi/current/log/nginx.error.log; access_log /var/www/rails/horoyoi/current/log/nginx.access.log; # max body size client_max_body_size 2G; server_names_hash_bucket_size 128; upstream app_server { # for UNIX domain socket setups server unix:/var/www/rails/horoyoi/current/tmp/sockets/.unicorn.sock fail_timeout=0; } server { listen 80; server_name horoyoi.com 52.222.23.22 sakamichi-app-elb-20323346271.ap-northeast-1.elb.amazonaws.com 10.0.*; # nginx so increasing this is generally safe... keepalive_timeout 5; # path for static files root /var/www/rails/horoyoi/current/public; # page cache loading try_files $uri/index.html $uri.html $uri @app; location @app { # HTTP headers proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://app_server; } location = /health { # HTTP headers proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://app_server; access_log off; } # Rails error pages error_page 500 502 503 504 /500.html; location = /500.html { root /var/www/rails/horoyoi/current/public; } }
/health
のところはヘルスチェック用ですが、もうちょっと綺麗に書けると思うので良い書き方教えて下さい笑
それで今回ハマったのは
server_name horoyoi.com 52.222.23.22 sakamichi-app-elb-20323346271.ap-northeast-1.elb.amazonaws.com 10.0.*;
server_nameです。はじめは 10.0.*という記述はなかったのですが、つけるとうまくいきました。
うまくいかなくて以下のようにしてlogを見てたら、10.0. ... というローカルIPからのアクセスがあってよくなさそうな感じだったのでserver_nameに 10.0. * とか入れてあげるとうまく動きました。
sudo tail -f /var/log/nginx/access.log
healthチェックは下記の記事が参考になりました。(unicornが落ちたら表示してほしくなかったので) http://blog.tnantoka.com/posts/75
僕はcontrollerで
def health head :ok end
みたいにしてstatus 200を返しています。 さらった書いてしまったので、もっと丁寧に時間があるとき書こうかなとか思ったり。