AWS(ELB)+nginx+unicornの設定の際にはまったポイント。
構成
構成としてはこんな感じ。外からポートhttps443で受けて、中はhttp80で。
permissionとか、csrfのActionController::InvalidAuthenticityToken errorエラーが出たりと結構はまりました。
- まずはログの確認。どこまで通信できてますか。エラーは出てませんか。
/var/log/nginx/access.log
/var/log/nginx/error.log
rails_home/log/unicorn.log
rails_home/log/production.log または rails_home/log/development.log
1. nginxでpermission denyエラーがでている。
permissionエラーの場合。nginxでよくでたりします。
この場合、ファイル・フォルダに権限を与えましょう。
chmod 755 /home/rails_home/
下位フォルダだけ権限を与えていて、上位フォルダの権限がない可能性もあるので、しっかりと確認を。
2. nginxでsocketがないと言われている
nginxとunicorn.rbの設定を再度確認しましょう。
unicorn.rbで設定した
listen "/tmp/unicorn.sock"
とnginx.confで設定した下記パスが一致してますか。
upstream unicorn { server unix:/home/unicorn_sample/tmp/unicorn.sock; }
この例の場合違っていますので、「/tmp/unicorn.sock」に修正しましょう。
設定変更後は各プロセスの再起動を忘れずにしましょう。
3. csrfのActionController::InvalidAuthenticityToken error
offにするという手もあります。日本語の多くのページでオフにする記載をみかけますが、セキュリティ上よくありません。
nginxの設定を見直しましょう。
root設定はあってますか。locationのheader設定はあってますか。
http { upstream unicornapp { server unix:/tmp/unicorn.sock; } server { listen 80 default_server; listen [::]:80 default_server; server_name localhost; root /home/rails_home/public; location / { proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_pass http://unicornapp; } }
4. ELBでInServiceにならない
ヘルスチェックでこけています。
まずはnginxでヘルスチェックのアクセスログがのっかてきているかを確認してみましょう。
/var/log/nginx/access.log
のっていない場合はAWSのセキュリティポリシーで対象のポートを閉じてませんか?
EC2上からcurl/wgetコマンドでヘルスチェック先へのアクセスを確認してみましょう。ページがない可能性があります。
$curl http://localhost/
おまけ1
empty gifでヘルスチェック(nginxまでのヘルスチェック)
location = /healthcheck.html { access_log off; empty_gif; }
health_check用のコントローラーを準備
上の設定だと裏が死んでいても気づかないので、こちらの方が良いかと。
rails g controller health_check
class HealthCheckController < ApplicationController def index render :text => "Alive\n", :status =>200 end protected def allow_http? true end end
route.rb
get "health_check" => "health_check#index"
おまけ2
サーバー設定を隠す。
レスポンスのヘッダーにサーバー設定がのっかてきます。
このままだとセキュリティ上よろしくないので、見えないようにすることをおすすめします。
Server: nginx/1.8.1
nginx.confに下記を追加して再起動してください。
バージョン情報はのってこなくなります。
http { server_tokens off; }
書き換えてnginx自体の表示もなくせますが、それはまたの機会に。
Linkedin oauthでこける
invalid redirect_uri. This value must match a URL registered with the API Key.
急にこんなエラーでこける。それもproduction環境で。
linkedinのdeveloperページで承認リダイレクトURLを設定。
設定先はエラーが出ている画面の箇所
&redirect_uri=https%3A%2F%2Fwww.*************.com%2Fauth%2Flinkedin%2Fcallback&
この場合だと
https://www.*********.com/auth/linkedin/callback
この設定でとりあえずこのエラーは回避。
あ。最後にLinkedinのDeveloperページの下部にある更新ボタンで更新を忘れずに。
devise 確認メールのあれこれ
確認メールのurlアドレスをhttpsに変更
app/views/devise/mailer/confirmation_instructions.html.erb
confirmation_url(@resource, :confirmation_token => @token, protocol: "https")
確認メールの開発用と公開用の設定
ここでsmtpとかドメイン設定とかを忘れずに
config/environments/development.rb
config/environments/producation.rb
# General Settings config.app_domain = 'www.hogehoge.com' # Email config.action_mailer.delivery_method = :smtp config.action_mailer.perform_deliveries = true config.action_mailer.default_url_options = { host: config.app_domain } config.action_mailer.smtp_settings = { address: 'smtp.hogehoge.com', port: '587', enable_starttls_auto: true, user_name: 'hogehoge', password: ENV['MAIL_KEY'], authentication: :plain, domain: 'hogehoge' }
パスワードは環境変数へ
$export MAIL_KEY=foofoo