nginxでVirtualHost

やったこと

  • 1つのIPアドレスで複数のドメインを立ち上げる
  • freenomで無料ドメインを取得⇓
  • www.nginx-domain.tk と nginx-domain.tk
  • 設定ファイルの編集、indexファイルを分け、ブラウザでわかるようにした
  • ここを参考に進めた

つまった所

前日に設定ファイルを作成したがブラウザに表示できなかった。

改善策

nginxの設定を変更したら、ストップ、再起動すると設定が反映された。

$ /etc/init.d/nginx stop
[ ok ] Stopping nginx (via systemctl): nginx.service.
$ /etc/init.d/nginx start
[ ok ] Starting nginx (via systemctl): nginx.service.

色々いじってみた

設定ファイル

``` $ cat /etc/nginx/sites-available/nginx-domain.tk

server { listen 80; server_name www.nginx-domain.tk; access_log /home/demo/public_html/nginx-domain.tk/log/access.log; error_log /home/demo/public_html/nginx-domain.tk/log/error.log; location / { root /home/demo/public_html/nginx-domain.tk/public/; index www_index.html index.php; } } server { listen 80; server_name nginx-domain.tk; access_log /home/demo/public_html/nginx-domain.tk/log/access.log; error_log /home/demo/public_html/nginx-domain.tk/log/error.log; location / { root /home/demo/public_html/nginx-domain.tk/public/; index index.html index.php; } } ```

ログも残せた
``` $ cat /home/demo/public_html/nginx-domain.tk/log/access.log

39.111.64.108 - - [23/Jul/2018:23:39:51 +0900] "GET / HTTP/1.1" 200 106 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36" 39.111.64.108 - - [23/Jul/2018:23:39:51 +0900] "GET /favicon.ico HTTP/1.1" 404 200 "http://nginx-domain.tk/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36" 39.111.64.108 - - [23/Jul/2018:23:41:46 +0900] "GET / HTTP/1.1" 200 106 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36" 39.111.64.108 - - [23/Jul/2018:23:41:47 +0900] "GET /favicon.ico HTTP/1.1" 404 200 "http://www.nginx-domain.tk/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36" 39.111.64.108 - - [23/Jul/2018:23:49:41 +0900] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36" 39.111.64.108 - - [23/Jul/2018:23:49:44 +0900] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36" 39.111.64.108 - - [23/Jul/2018:23:49:47 +0900] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36" 39.111.64.108 - - [23/Jul/2018:23:58:27 +0900] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36" 39.111.64.108 - - [23/Jul/2018:23:58:40 +0900] "GET / HTTP/1.1" 200 147 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36" 34.239.107.223 - - [24/Jul/2018:00:00:53 +0900] "GET / HTTP/1.1" 200 147 "-" "Slackbot-LinkExpanding 1.0 (+https://api.slack.com/robots)" 35.153.127.83 - - [24/Jul/2018:00:00:54 +0900] "GET /favicon.ico HTTP/1.1" 404 143 "-" "Slackbot 1.0 (+https://api.slack.com/robots)" 52.71.192.87 - - [24/Jul/2018:00:00:55 +0900] "GET / HTTP/1.1" 200 106 "-" "Slackbot-LinkExpanding 1.0 (+https://api.slack.com/robots)" 52.72.111.29 - - [24/Jul/2018:00:00:56 +0900] "GET /favicon.ico HTTP/1.1" 404 143 "-" "Slackbot 1.0 (+https://api.slack.com/robots)" 39.111.64.108 - - [24/Jul/2018:00:02:47 +0900] "GET / HTTP/1.1" 200 147 "-" "Mozilla/5.0 (iPhone; CPU iPhone OS 11_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.0 Mobile/15E148 Safari/604.1" 39.111.64.108 - - [24/Jul/2018:00:03:04 +0900] "GET / HTTP/1.1" 200 106 "-" "Mozilla/5.0 (iPhone; CPU iPhone OS 11_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.0 Mobile/15E148 Safari/604.1" ```

ドメインで同じIPアドレスかを確認

```

$ nmap nginx-domain.tk

Starting Nmap 7.40 ( https://nmap.org ) at 2018-07-24 00:25 JST Nmap scan report for nginx-domain.tk (160.16.51.37) Host is up (0.0000050s latency). rDNS record for 160.16.51.37: tk2-201-10283.vs.sakura.ne.jp Not shown: 998 closed ports PORT STATE SERVICE 80/tcp open http 1234/tcp open hotline

Nmap done: 1 IP address (1 host up) scanned in 2.64 seconds

$ nmap www.nginx-domain.tk

Starting Nmap 7.40 ( https://nmap.org ) at 2018-07-24 00:26 JST Nmap scan report for www.nginx-domain.tk (160.16.51.37) Host is up (0.0000040s latency). rDNS record for 160.16.51.37: tk2-201-10283.vs.sakura.ne.jp Not shown: 998 closed ports PORT STATE SERVICE 80/tcp open http 1234/tcp open hotline ```

所感

なにかエラーがあるとnginx -tでエラー箇所が特定できて便利だった。 アクセスログをみるとiphoneのosの情報も載ってるのだと知った。 OSをアップデートしてないのも丸わかりだなと思った。