[Apache] 가상 호스트(VirtualHost) 설정

배세훈·2022년 10월 21일
0

webserver

목록 보기
2/2

도메인 가상 호스트

  • 여러 도메인을 어떤 호스트에 액세스할 것인지를 결정한다.
# VirtualHost를 사용하여 ServerName마다 설정
<VirtualHost IP주소:포트번호>
	ServerName www.dev1.com
    ...
</VirtualHost>
<VirtualHost IP주소:포트번호>
	ServerName www.dev2.com
    ...
</VirtualHost>

요청(request)에 포함된 Hosts 헤더를 참조하여 "ServerName"와 일치하는 블록을 찾는다.
일치하는 블록이 발견되면 그 블록의 설정이 적용된다.

좀 더 구체적으로 설명을 하자면 "www.dev1.com"과 "www.dev2.com" 두 호스트를 1개 이상의 IP 주소에 할당하고, 각각에 대한 액세스에 대해 다른 동작을 하게 하려면 다음과 같이 설정한다.

<VirtualHost *:80>
	ServerName		www.dev1.com
    ServerAdmin		dev@dev.com
    DocumentRoot	"${SRVROOT}/htdocs-dev1"
    CustomLog		logs/dev1.accesslog		common
    ErrorLog		logs/dev1.error.log
</VirtualHost>

<VirtualHost *:80>
	ServerName		www.dev2.com
    ServerAdmin		dev2@dev2.com
    DocumentRoot	"${SRVROOT}/htdocs-dev2"
    CustomLog		logs/dev2.access.log	common
    ErrorLog		logs/dev2.error.log
</VirtualHost>
  • 위의 "*:80"이라고 지정되어 있는 부분은 "192.168.1.10:80"와 같이 명시적으로 IP주소를 지정해도 된다.

실습

  • 먼저 2개의 호스트에 대한 DNS 등록. 여기서는 DNS 대신에 "hosts" 파일에 다음과 같이 작성한다. (Windows의 경우 "hosts" 파일은 "C:\Windows\System32\drivers\etc"에 위치한다.
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#	127.0.0.1       localhost
#	::1             localhost
127.0.0.1 localhost
127.0.0.1 www.dev1.com
127.0.0.1 www.dev2.com
profile
성장형 인간

0개의 댓글