LoginSignup
3
3

More than 1 year has passed since last update.

[Chapter-10]https化によるNginxの設定

Last updated at Posted at 2021-04-25
タイトル
[Chapter-1]VPCの設定
[Chapter-2]EC2の設定
[Chapter-3]EC2のサーバー環境構築
[Chapter-4]EC2にRailsアプリの配置
[Chapter-5]ロードバランサー(ELB)の作成
[Chapter-6]ACM(AWS Certificate Manager)でSSL証明書を取得
[Chapter-7]Route53の設定
[Chapter-8]https化に向けたロードバランサーの設定
[Chapter-9]https化によるRoute53の設定
[Chapter-10]https化によるNginxの設定

はじめに

今回はNginxの設定をしていきます。
※これでhttps化完了です
画像なしで説明しますのであしからず。
少しだけrailsのファイルの設定もしていきます。

Nginxとは

HTTPおよび、HTTPSでのアクセスに使われる軽量サーバーのことでWebサーバーとしての基本的な機能を持っています。

Nginxの設定

以下のファイルを編集します
/etc/nginx/nginx.conf
httpでアクセスされたものをhttpsへリダイレクトする記述へ変更します。

EC2サーバー環境
$ sudo vim /etc/nginx/nginx.conf
server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  www.hogehoge.com; #ドメイン名に変更
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
}

ドメイン名を記述したら:wqで保存します。

次にRailsアプリに移動します。
そして/config/environments/production.rbファイルを編集します

EC2サーバー環境
$ sudo vim /config/environments/production.rb
/config/environments/production.rb
config.force_ssl = true

これをすることで常時SSL化対応となります。

NginxとUnicornの再起動

Nginxの再起動

EC2サーバー環境
$ sudo service nginx restart

次にUnicornを再起動させます。
まずUnicornの起動を確認

EC2サーバー環境
$ ps -ef | grep unicorn | grep -v grep

ryogo    11658     1  0  4月24 ?      00:00:02 unicorn_rails master -c /var/www/rails/travelour/config/unicorn.conf.rb -D -E production
ryogo    11662 11658  0  4月24 ?      00:00:02 unicorn_rails worker[0] -c /var/www/rails/travelour/config/unicorn.conf.rb -D -E production
ryogo    11663 11658  0  4月24 ?      00:00:02 unicorn_rails worker[1] -c /var/www/rails/travelour/config/unicorn.conf.rb -D -E production

表示されたUnicornの番号をkillします。
1行目の11658のみkillすれば良いです

EC2サーバー環境
$ kill 11658

Unicornを再度起動させます。

EC2サーバー環境
$ bundle exec unicorn_rails -c /var/www/rails/アプリ名/config/unicorn.conf.rb -D -E production

これでhttpsでドメインにアクセスできるようになります。

EC2サーバー環境
https://www.hogehoge.com

参考

3
3
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
3
3