우분투 리눅스 17.10 환경에서 Virtual Hosts를 설정해보려 한다. Nginx에서는 Server Blocks라는 이름으로 부르는 것 같은데, 아파치웹서버가 득세하던 시절에 virtual host라고 불렀던 까닭에 virtual host라고 하는게 좀 더 익숙하다.
(2018년 2월)현재 최신 버전인 우분투리눅스 17.10을 기준으로 한다.
기본 설정
테스트가 목적이니 실 도메인을 가지고 설정하지는 않을 것이다. example.com과 test.com두 개의 도메인을 가지고 테스트 할 것이다. 하나의 nginx 서버에서 example.com과 test.com을 모두 서비스하는게 목적이다.
DocumentRoot 디렉터리 설정하기
Nginx의 기본 문서 디렉터리는 /var/www/html이다. example.com과 test.com은 각 서비스를 위한 HTML, CSS, Javascript 문서들을 가지고 있어야 하므로, 이들 서비스를 위한 문서 디렉토리를 따로 설정해야 한다. 나는 아래와 같이 문서파일 디렉토리를 만들기로 했다.
# 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;
}
}
Contents
Nginx server로 Virtual Hosts(Server Blocks) 설정하기
기본 설정
DocumentRoot 디렉터리 설정하기
운영체제 테스트 환경 만들기
Nginx 설정
결론
Recent Posts
Archive Posts
Tags