Recommanded Free YOUTUBE Lecture: <% selectedImage[1] %>

Contents

Nginx server로 Virtual Hosts(Server Blocks) 설정하기

우분투 리눅스 17.10 환경에서 Virtual Hosts를 설정해보려 한다. Nginx에서는 Server Blocks라는 이름으로 부르는 것 같은데, 아파치웹서버가 득세하던 시절에 virtual host라고 불렀던 까닭에 virtual host라고 하는게 좀 더 익숙하다.

(2018년 2월)현재 최신 버전인 우분투리눅스 17.10을 기준으로 한다.

기본 설정

테스트가 목적이니 실 도메인을 가지고 설정하지는 않을 것이다. example.comtest.com두 개의 도메인을 가지고 테스트 할 것이다. 하나의 nginx 서버에서 example.com과 test.com을 모두 서비스하는게 목적이다.

DocumentRoot 디렉터리 설정하기

Nginx의 기본 문서 디렉터리는 /var/www/html이다. example.com과 test.com은 각 서비스를 위한 HTML, CSS, Javascript 문서들을 가지고 있어야 하므로, 이들 서비스를 위한 문서 디렉토리를 따로 설정해야 한다. 나는 아래와 같이 문서파일 디렉토리를 만들기로 했다.
  • example.com : /var/www/example.com/html
  • test.com : /var/www/test.com/html
이제 HTML 문서를 위한 디렉토리를 만들었다.
$ sudo mkdir -p /var/www/example.com/html
$ sudo mkdir -p /var/www/test.com/html
그리고 각각의 디렉토리 밑에 테스트용 index.html 파일을 만들었다.
# cat /var/www/test.com/html/index.html
<h1>test.com</h1>
Hello test.com 

# cat /var/www/example.com/html/index.html
<h1>example.com</h1>
Hello example.com 

운영체제 테스트 환경 만들기

브라우저로 테스트하면 (인터넷에 실제 존재하는)example.com과 test.com을 룩업하게 된다. 로컬호스트(127.0.0.1)을 룩업하도록 /etc/hosts를 설정했다.
# cat /etc/hosts
127.0.0.1 	www.example.com example.com
127.0.0.1   www.test.com test.com
ping으로 확인해보자.
# ping www.test.com
PING www.test.com (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.064 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.050 ms
127.0.0.1이 잡히지 않고, www.test.com의 인터넷 public IP가 룩업된다면 /etc/nsswitch.conf를 아래와 같이 수정하자.
hosts:          files mdns4_minimal [NOTFOUND=return] dns mdns4

# 아래 설정이 기본일 것이다.
# hosts 파일 보다 dns를 먼저 참조하고 있다.
# hosts:        dns files

Nginx 설정

Nginx 설정파일은 "/etc/nginx" 디렉터리 밑에 있다. 여기에 sites-available와 site-enabled 두 개의 디렉터리가 있는데, 이 디렉터리로 virtualhost를 설정 할 수 있다.
  • sites-available : virtualhost 설정 파일들이 있다.
  • sites-enabled : sites-available에 있다고 해서 설정이 적용되지는 않는다. 이 디렉터리에 있어야 비로서 설정이 적용된다. sites-available에서 적용할 설정파일을 링크걸어서 사용한다.
sites-available 디렉터리에 example.com 파일을 만들었다.
server {
    listen 80;
    listen [::]:80;

    root /var/www/example.com/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name example.com www.example.com;

    location / {
            try_files $uri $uri/ =404;
    }
}
아래는 test.com 파일이다.
server {
	listen 80;
	listen [::]:80;

	root /var/www/test.com/html;

	# Add index.php to the list if you are using PHP
	index index.html index.htm index.nginx-debian.html;

	server_name test.com www.test.com;

	location / {
        	try_files $uri $uri/ =404;
	}
}
이제 site-enabled에 링크를 걸어주자.
# cd /etc/nginx/sites-enable
# ln -s ../sites-available/example.com ./
# ln -s ../sites-available/test.com 
Nginx 서버를 재시작한다.
# service nginx restart

curl로 테스트해보자.
# curl www.example.com
<h1>example.com</h1>
Hello example.com 

# curl www.test.com
<h1>test.com</h1>
Hello test.com 

sites-available 디렉터리에는 "default"이름의 설정파일이 하나 있을 것이다. 이 설정 파일은 다른 virtualhost 파일과는 약간의 차이가 있다.
server {
    listen 80 default_server;
    listen [::]:80 default_server;
다른 설정들과는 다르게 "default_server"가 있는 걸 확인 할 수 있을 것이다. default_server는 해당 포트에 바인드하는 기본 서버로 전체 virtualhost 설정에서 오직 하나만 있을 수 있다.

결론

이제 하나의 nginx 서버에서 여러 도메인을 서비스 할 수 있게 됐다. 하드웨어가 트래픽을 처리 할 수 있는 한 만들 수 있는 virtualhost의 갯수에는 제한이 없다.