라즈베리파이

라즈베리파이4 설정(3) - NGINX에서 다중 도메인 홈페이지 운영하기(Virtual Host)

꼬망토끼 2020. 4. 19. 00:38

웹 서버를 운영하다보면, 한 웹서버에 여러 홈페이지를 운영할 경우가 있다.

aaa.domain.com, bbb.domain.com 와 같이 서브 도메인만 다른 경우가 있을 수도 있고, www.domain1.com, www.domain2.com 처럼 아예 다른 도메인을 운영할 수 도 있다.

 

NGINX 웹서버는 Virtual Host라는 기능을 이용하여 여러 홈페이지를 운영하는 방법을 제공한다.

 

1. Virtual Host 설정

Virtual Host를 설정하는 방법은 매우 간단하다. "/etc/nginx/sites-enabled" 폴더 아래에 도메인별로 설정파일을 만들어 저장해 놓기만 하면 된다.

 

예를 들어 "www.domain.com"이라는 홈페이지와 "blog.domain.com"이라는 홈페이지를 라즈베리파이에 운영하고 싶다고 가정하자. 아까 얘기했던 폴더 아래에 "www.domain.com.conf" 파일과 "blog.domain.com.conf"라는 파일을 각각 만들어 주면 된다.

사실, 도메인 이름과 파일명이 반드시 같을 필요는 없지만 관리 편리성을 생각한다면 파일명만 가지고도 어떤 도메인에 대한 설정 파일인지 알 수 있도록 만들어 주는 것이 좋다.

 

설정 파일 내용은 아래처럼 만들어 주면 된다.

server {
    listen 80;
    server_name www.domain.com;
    root /var/www/html/www.domain.com;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.3-fpm.sock;
    }
}

server {
    listen 443 ssl;
    server_name www.domain.com;
    root /var/www/html/www.domain.com;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

   ​ ssl on;
    ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;

   ​ location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.3-fpm.sock;
    }
}

여기서 각각의 파일 내용중 파란색 부분 즉, 서버이름(server_name) 및 HTML 문서 경로(root) 부분은 도메인 별로 각각 설정해 주면 된다.

 

2. 접속 테스트

도메인별로 설정 파일을 만들어 주었다면 아래 명령을 이용하여 NGINX 서버를 재시작 한다.

# /etc/init.d/nginx restart

이제 각각의 도메인별 문서경로 부분에 index.html(또는 index.php 등) 파일을 만들고 정상적으로 접속이 접속이 되는지 확인해 보도록 하자.