はじめに
先日、現在開発中のアプリケーションのステージング環境の構築を行ないました。
ステージング環境はこのブログのサーバと相乗りさせています。
今回はその際に行ったNginxの設定とSSL化を中心に紹介します。
デプロイ設定は下記をご確認ください。
https://re-engines.com/2017/07/14/capistrano3/
前提条件
CentOS 7.3
nginx 1.10.2
ruby 2.4.1
rails 5.1.0
unicorn 5.3.0
サブドメインを設定する
お名前.comでサブドメインを設定しました。
サブドメインの取得は「DNS関連機能の設定」でドメインを選択後、「DNSレコード設定を利用する」から設定できます。
今回は仮に「my-app.re.enines.com」としておきます。
Nginxの設定
ステージング環境用の設定
/etc/nginx に sites_enabled というディレクトリを作成し、そこにステージング環境用のNginxの設定ファイルを作成します。
注意点として、unicornのpidの場所は /tmp 以外のディレクトリにします。
http://blog.tnantoka.com/posts/49/versions/current
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | server{ listen80; listen[::]:80; server_name my-app.re-engines.com; root /home/engines/www/my-app-server/current/public; proxy_buffers6416k; proxy_max_temp_file_size1024m; proxy_temp_path/dev/shm/nginx_proxy_temp; proxy_connect_timeout5s; proxy_send_timeout10s; proxy_read_timeout10s; location~^/assets/{ root/home/engines/www/my-app-server/shared/public; } location/{ try_files$uri@app; } location@app{ proxy_set_header Host$host; proxy_set_headerX-Forwarded-Host $host; proxy_set_headerX-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_headerX-Forwarded-Proto$scheme; # NginxとUnicorn間で/tmpを共有できない proxy_pass http://unix:/home/engines/www/my-app-server/shared/tmp/sockets/unicorn.sock; } } |
ステージング環境の設定を読み込ませる
設定ファイルを作成後、既存のNginxの設定ファイルに作成した設定ファイルを読み込むようにします。
1 2 3 4 | http{ # 省略 include sites-enabled/*.conf; } |
Nginxの再起動
設定後、設定ファイルの確認を行います。
1 2 3 4 | $sudo nginx-t-c/etc/nginx/nginx.conf [sudo]password forengines: nginx:the configuration file/etc/nginx/nginx.conf syntax isok nginx:configuration file/etc/nginx/nginx.conf test issuccessful |
問題がなければNginxを再起動します。
1 | $sudo systemctl restart nginx.service |
SSL化する
ブログサーバはすでにLet’s Encryptでサーバ証明書を取得しています。
ステージング環境用のサーバ証明書を取得することもできますが、今回はブログとステージング環境を一つの証明書でまかないます。
ルーティングの追加
ステージング環境用のNginxの設定ファイルを修正します。
Let’s Encryptは指定されたパスに認証ファイルを作成し、それをLet’s EncryptのサーバからGETリクエストで取得することで認証を行なっていそうなので、
ルートディレクトリの設定を行います。
http://qiita.com/kamemory/items/958fdbc220359c341a23
1 2 3 4 5 6 | server{ # 追加 location^~/.well-known/acme-challenge/{ root/home/engines/www/my-app-server/; } } |
設定後、Nginxを再起動します。
サーバ証明書の発行
下記のコマンドを実行し、証明書を発行します(事前にLet’s Encryptをインストールしておきます)
1 | $/home/engines/tools/letsencrypt/certbot-auto certonly--webroot-w/home/engines/www/re-engines-dre-engines.com-w/home/engines/www/my-app-server-dmy-app.re-engines.com |
ステージング環境をSSL化
ステージング環境用のNginxの設定ファイルを修正します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | # 追加 server{ listen80; liste[::]:80; server_name my-app.re-engines.com; rewrite^(.*)https://my-app.re-engines.com$1 permanent; } server{ # 修正 listen443ssl; listen[::]:443ssl; server_name my-app.re-engines.com; root /home/engines/www/my-app-server/current/public; # 追加 ssl_certificate"/etc/letsencrypt/live/re-engines.com/fullchain.pem"; ssl_certificate_key"/etc/letsencrypt/live/re-engines.com/privkey.pem"; ssl_session_cache shared:SSL:1m; ssl_session_timeout 10m; ssl_protocols TLSv1 TLSv1.1TLSv1.2; ssl_ciphers HIGH:SEED:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!RSAPSK:!aDH:!aECDH:!EDH-DSS-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA:!SRP; ssl_prefer_server_ciphers on; # 省略 } |
修正後、Nginxを再起動します。
これでステージング環境にhttpsでアクセスできるようになります。
さいごに
WordPressとRailsアプリケーションを相乗りさせ、SSL化しました。