Linux/보안장비 운용

nginx 에서 가상 호스트 설정하기

GGkeeper 2022. 3. 13. 15:28

실습> nginx 에서 가상호스트 설정하기

1. hosts 파일 편집
테스트이기 때문에 자신의 IP주소에 해당하는 것으로 설정한다.
-- /etc/hosts --
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.108.102 server3.linuxmaster.co.kr
192.168.108.102 sbs.com www.sbs.com
192.168.108.102 kbs.com www.kbs.com
192.168.108.102 mbc.com www.mbc.com
 -- /etc/hosts --

2. 사용자 생성 및 웹 문서 작성
# sed -i 's/USERGROUPS_ENAB/#USERGROUPS_ENAB/' /etc/login.defs

# mkdir -m711  /etc/skel/public_html

사용자를 생성한다.
# useradd sbsuser
# useradd kbsuser
# useradd mbcuser

사용자 디렉터리의 권한을 부여한다. 
각 사용자는 보안상 상대방 디렉터리로 접근할 수 없도록 설정한다.
# chmod o+x /home/{sbs,kbs,mbc}user
# ls -ld /home/{sbs,kbs,mbc}user
drwx-----x. 3 kbsuser users 81  2월 10 21:25 /home/kbsuser
drwx-----x. 3 mbcuser users 81  2월 10 21:25 /home/mbcuser
drwx-----x. 3 sbsuser users 81  2월 10 21:25 /home/sbsuser

사용자의 비밀번호를 설정한다.
# passwd sbsuser
# passwd kbsuser
# passwd mbcuser

index.html 파일을 생성한다.
# su - sbsuser -c 'echo sbsuser > public_html/index.html'
# su - kbsuser -c 'echo kbsuser > public_html/index.html'
# su - mbcuser -c 'echo mbcuser > public_html/index.html'

3. 설정파일 수정
# yum -y install tree
# tree /etc/nginx/
/etc/nginx/
|-- conf.d
|   `-- default.conf
|-- fastcgi_params
|-- koi-utf
|-- koi-win
|-- mime.types
|-- modules -> ../../usr/lib64/nginx/modules
|-- nginx.conf
|-- scgi_params
|-- uwsgi_params
`-- win-utf

2 directories, 9 files

전체 설정파일 구조

-- nginx.conf --
http {

    server_tokens off;

    # conf.d/default.conf
    server {

    }
    
    # conf.d/sbs.com.conf
    # sbs.com
    server {

    }

    # conf.d/kbs.com.conf
    # kbs.com
    server {

    }

    # conf.d/mbc.com.conf
    # mbc.com
    server {

    }
}
-- nginx.conf --
# vi /etc/php.ini
expose_php = Off

# cd /etc/nginx/conf.d/
# vi sbs.com.conf
server {
    listen       80;
    server_name  sbs.com www.sbs.com;

    # 캐릭터셋 변경
    charset utf-8;

    # WEb root 디렉터리 변경
    root   /home/sbsuser/public_html;

    location / {
        index  index.html index.php;
    }

   location ~ \.(php|html)$ {
        fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /home/sbsuser/public_html$fastcgi_script_name;
        include        fastcgi_params;
    }

}

# vi kbs.com.conf
server {
    listen       80;
    server_name  kbs.com www.kbs.com;

    # 캐릭터셋 변경
    charset utf-8;

    # WEb root 디렉터리 변경
    root   /home/kbsuser/public_html;

    location / {
        index  index.html index.php;
    }

   location ~ \.(php|html)$ {
        fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /home/kbsuser/public_html$fastcgi_script_name;
        include        fastcgi_params;
    }

}

# vi mbc.com.conf
server {
    listen       80;
    server_name  mbc.com www.mbc.com;

    # 캐릭터셋 변경
    charset utf-8;

    # WEb root 디렉터리 변경
    root   /home/mbcuser/public_html;

    location / {
        index  index.html index.php;
    }

   location ~ \.(php|html)$ {
        fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /home/mbcuser/public_html$fastcgi_script_name;
        include        fastcgi_params;
    }

}

설정파일을 수정했다면 php-fpm 과 nginx를 재시작한다.
# systemctl restart php-fpm
# systemctl restart nginx

4. 도메인 테스트

# yum -y install lynx

# sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      31

# chcon -R -t httpd_sys_content_t /home/*/public_html

# echo /var/www/html > /var/www/html/index.php
# lynx --dump 192.168.108.102
   /var/www/html

# lynx --dump sbs.com
   sbsuser

# curl -I sbs.com
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 10 Feb 2021 13:25:14 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive

# lynx --dump kbs.com
   kbsuser

# curl -I kbs.com
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 10 Feb 2021 13:25:17 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive

# lynx --dump mbc.com
   mbcuser

# curl -I mbc.com
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 10 Feb 2021 13:25:24 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive