Dragon Arrow written by Tatsuya Nakaji, all rights reserved animated-dragon-image-0164

MuuMuuDomain + Route53 独自ドメイン取得&IPと紐付け

updated on 2019-06-10

MuuMuuDomain + Route53


現状: EC2にてアプリをデプロイし、http://[ElasticIP] で公開している状態


目的: 独自ドメインを取得してhttp://[ElasticIP] とひもづけることで、独自ドメインでサイトにアクセスできるようにする


ドメイン取得

https://muumuu-domain.com/

検索を押下すると一覧が表示されるのでその中で欲しいものを選択


基本既定値で大丈夫です。
DNSに関しても後で設定するのでそのままでOK

支払いはコンビニやクレジットカードが選択できます。
そのまま進んでいき最後にサマリが出てくるので確認して申し込めば完了です。

以上でドメインが取得できました。



Route 53


ドメインネームを記載(取得したドメイン名 例: example.com)

コメントは適当に作成



ムームードメインの管理画面へ移動

ネームサーバーをRoute53に変更

先ほどの4つのNameServersの情報を記載します。最後の.は要らない


例ではサブドメインをwwwと定義しました。



これでEC2(WEBサーバ)に独自ドメイン(http://www.example.com)で接続できるようになる。

と言いたいところだが、私の場合、「welcome to nginx」のページが表示されてしまい、サイトのページが表示されなかった。


理由は、nginxの設定ファイル /etc/nginx/conf.d/froala-blog.conf (froala-blogは私のアプリ名)

にて
server_name xx.xx.xx.xx; 
ではなく
server_name www.example.com xx.xx.xx.xx; 
というように、サーバー名に独自ドメインを追加した


/etc/nginx/conf.d/froala-blog.conf

# log directory
error_log  /var/www/rails/froala-blog/log/nginx.error.log;
access_log /var/www/rails/froala-blog/log/nginx.access.log;
# max body size
client_max_body_size 2G;
upstream app_server {
  # for UNIX domain socket setups
  server unix:/var/www/rails/froala-blog/tmp/sockets/unicorn.sock fail_timeout=0;
}
server {
  listen 80;
  server_name www.example.com xx.xx.xx.xx; # 独自ドメイン ElasticIP
  # nginx so increasing this is generally safe...
  keepalive_timeout 5;
  # path for static files
  root /var/www/rails/froala-blog/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;
  }
  # Rails error pages
  error_page 500 502 503 504 /500.html;
  location = /500.html {
    root /var/www/rails/froala-blog/public;
  }
}


最後にnginxとunicornを再起動して無事アクセスできました。

sudo service nginx restart(nginx再起動)
sudo kill -HUP `cat /var/www/rails/froala-blog/tmp/pids/unicorn.pid`(unicorn再起動)



完!!