100
102

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

nginxでvirtualhostを設定する

Posted at

nginxにvirtualhost設定をしたときのメモ。

環境


  • VirtualBox 4.3
  • ubuntu 14.04
  • nginx 1.6.2

default.conf設定


/etc/nginx/conf.d/default.conf

server {
    listen       80;
    server_name  hoge1.com;
    
    access_log  /var/log/nginx/log/hoge1.access.log  main;
    
    location / {
        root   /usr/share/nginx/html/hoge1;
        index  index.html index.htm;
    }
}

server {
    listen       80;
    server_name  hoge2.com;
    
    access_log  /var/log/nginx/log/hoge2.access.log  main;
    
    location / {
        root   /usr/share/nginx/html/hoge2;
        index  index.html index.htm;
    } 
}

server {
    listen       80;
    server_name  hoge3.com;
    
    access_log  /var/log/nginx/log/hoge3.access.log  main;
    
    location / {
        root   /usr/share/nginx/html/hoge3;
        index  index.html index.htm;
    }
}            

※hoge1,hoge2,hoge3.comを用意した

configtestをしてみる

$ sudo /etc/init.d/nginx configtest
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

※問題無し

location-root(documentroot)にディレクトリを用意し、index.htmlを配置


$ cd /usr/share/nginx/
$ sudo mkdir hoge1 hoge2 hoge3
$ cd hoge1
$ sudo vim index.html
----
hoge1.com
----
※hoge2,hoge3にも同じ事をします

ポートフォワーディング(windows + virtualbox + ubuntuのみ)


virtualbox + ubuntu tipsの「ubuntuへwindowsからターミナルで接続したい」を参考に、ubuntuへの80へのポートフォワーディング設定をする。
※とりあえずホスト「10000」、ゲスト「80」に設定した

windowsのhostsファイルを設定する(windows + virtualbox + ubuntuのみ)


nginxのconfに設定したドメインを解決させる
C:\Windows\System32\drivers\etc\hosts

127.0.0.1	hoge1.com
127.0.0.1	hoge2.com
127.0.0.1	hoge3.com

nginxを再起動する


$ sudo /etc/init.d/nginx restart

ブラウザで確認


http://hoge1.com:10000/index.html
http://hoge2.com:10000/index.html
http://hoge3.com:10000/index.html

ログ確認


$ cd /var/log/nginx/log
※hoge*.access.logにアクセスログが出力されます
100
102
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
100
102

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?