apache subdomain to port mapping (vhost) - 아파치 포트별 서브도메인 설정(가상호스트사용)

북마크 추가

안녕하세요

서버 한대에서 여러 서비스를 돌리고 있을때 도메인을 포트별로 맵핑 하는 방법입니다.

작성 기준은 CentOS7  +  apache httpd 2.4 입니다.


보통은 서버 한대에서 여러 웹 서버 운영시 포트를 직접 입력해서 들어가야되는데 이를 서브 도메인과 연동하겠습니다.

ex) Web server1 = trandent.com / Web server2 = trandent.com:8081


내부적으로 tomcat을 8080과 8081로 서비스 한다고 가정 하고 80으로 들어오는 요청을 도메인에 따라 각각의 웹 서버로 연동하겠습니다.


trandent.com :80 -> localhost:8080

sub.trandent.com -> localhost:8081


1. Apache httpd가 설치되어 있지 않다면 설치해 줍니다.


# yum install -y httpd


2. 기본적으로 /etc/httpd/conf.d/ 안의 *.conf 파일을 읽어올수 있게 설정되어 있기 때문에 해당 경로에 vhost.conf파일을 추가 합니다.

 - /etc/httpd/conf/httpd.conf 파일 내 IncludeOptionaal conf.d/*.conf  되어있는지 확인

1. vhost.conf 생성
# vi /etc/httpd/conf.d/vhost.conf

2. vhost.conf 내용
NameVirtualHost *:80

<VirtualHost *:80>
    ServerName www.trandent.com
    ServerAlias trandent.com
    ErrorLog "/var/log/httpd/trandent_error_log"
    CustomLog "/var/log/httpd/trandent_access_log" common
    
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:8080/
    ProxyPassReverse / http://127.0.0.1:8080/
    ProxyRequests Off
    AllowEncodedSlashed NoDecode

    <Proxy http://127.0.0.1:8080/*>
        Order deny,allow
        Allow from all
    </Proxy>
</VirtualHost>

<VirtualHost *:80>
    ServerName subDomain.trandent.com
    ErrorLog "/var/log/httpd/subDomain_error_log"
    CustomLog "/var/log/httpd/subDomain_access_log" common
    
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:8081/
    ProxyPassReverse / http://127.0.0.1:8081/
    ProxyRequests Off
    AllowEncodedSlashed NoDecode

    <Proxy http://127.0.0.1:8081/*>
        Order deny,allow
        Allow from all
    </Proxy>
</VirtualHost>


# 80 port를 통해  도메인 www.trandent.com과 trandent.com으로 들어오는 경우
# ErrorLog, CustomLog : 로그파일 설정 (폴더로 나눠도 무관)
# ProxyPreserveHost On : WAS로 host 정보 전달 - 미설정시 referer등이 127.0.0.1로 바뀜
# ProxyPass :요청을 127.0.0.1(8080 or 8081) 으로 전달 # ProxyPassReverse : 응답을 Apache를 통해 클라이언트에 전달 # AllowEncodedSlashed NoDecode : %2F로 인코딩된 슬래쉬를 디코딩 안하도록 설정 # 127.0.0.1(8080 or 8081)에 대한 proxy 요청 허용

3. httpd  시작 또는 재시작

# systemctl restart httpd
AD
관리자
2021-01-13 11:48
SHARE