Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
104
Help us understand the problem. What is going on with this article?
@duke-gonorego

nginxでvirtualhostを設定する

More than 5 years have passed since last update.

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にアクセスログが出力されます
104
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
104
Help us understand the problem. What is going on with this article?