자습서-NGINX Unit Route 구성해보기, 2부
NGINX Unit Route, 마이크로서비스를 위한 다중언어 앱 서버 NGINX Unit 자습서 시리즈 2부를 시작합니다.
지난 1부에서는 NGINX Unit의 강력한 기능 중 하나인 동적 재구성의 간단한 예제를 진행했습니다.
NGINX Unit에서는 서비스 중단 없이 Restful API로 앱 서버를 재구성하는 것이 얼마나 쉽다는 것을 알게 되셨을거라고 생각합니다.
오늘은 NGINX Unit 앱 서버 자체에서 애플리케이션별로 Route를 적용하는 예제에 대해 살펴 볼 것입니다.
목차
1. NGINX Unit Route 개요
2. NGINX Unit Route 맛보기
2-1. 예제1. Route Basic
2-2. 예제2. Named Route
3. 결론
1. NGINX Unit Route 개요
Route 객체(Object)는 Listener 객체(Object)로부터 요청(Request)를 수신하고 애플리케이션에서 직접 처리하거나 외부 서버로 프록시하거나 외부 서버 간에 로드 밸런싱하거나 정적 콘텐츠를 제공하거나 임의의 상태 코드로 응답하거나 리디렉션(Redirect)할 조건 세트를 통해 필터링을 제공합니다.
해당 포스트에서는 애플리케이션에서 직접 처리하는 예제를 다룹니다.
2. NGINX Unit Route 맛보기
2-1. 예제1. Route Basic
NGINX Unit 구성에 앞서 아래 디렉토리와 index.php
파일을 만들어줍니다.
/www/blog/index.php
/www/helloworld/index.php
/www/helloworld/admin/index.php
index.php
파일은 파란색으로 표시한 Hello 영역 문자 내용만 바꿔서 사용합니다.
<?php echo "Hello, PHP on Unit!\n"; ?>
첫 번째 예제에서 구성할 JSON 파일을 다음과 같이 만듭니다. (route_basic_config.json
)
JSON 파일에는 Listener 객체, Route 객체, Application 객체가 있습니다.
{
"listeners": {
"*:8080": {
"pass": "routes"
}
},
"routes": [
{
"match": {
"host": "example.com"
},
"action": {
"pass": "applications/blog"
}
},
{
"action": {
"pass": "applications/helloworld"
}
}
],
"applications": {
"helloworld": {
"type": "php",
"root": "/www/helloworld/"
},
"blog": {
"type": "php",
"root": "/www/blog/"
}
}
}
NGINX Unit 구성 명세를 curl 명령어로 반영합니다. Restful API로 통해 무중단으로 앱 서버가 재구성됩니다.
$ sudo curl -X PUT --data-binary @route_basic_config.json --unix-socket /var/run/control.unit.sock 'http://localhost/config/'
구성이 잘 반영되었으면 아래와 같은 “success” 응답이 표시됩니다.
{
"success": "Reconfiguration done."
}
반영된 구성 전체를 확인해보겠습니다.
$ sudo curl --unix-socket /var/run/control.unit.sock http://localhost/config
{
"listeners": {
"*:8080": {
"pass": "routes"
}
},
"routes": [
{
"match": {
"host": "example.com"
},
"action": {
"pass": "applications/blog"
}
},
{
"action": {
"pass": "applications/helloworld"
}
}
],
"applications": {
"helloworld": {
"type": "php",
"root": "/www/helloworld/"
},
"blog": {
"type": "php",
"root": "/www/blog/"
}
}
}
구성을 통해 테스트하고자 하는 구성 아래 그림과 같습니다.

이제 route 객체의 의한 애플리케이션 라우팅을 테스트해보도록 하겠습니다.
먼저 example.com으로 요청해보도록 하겠습니다.
/etc/hosts 파일에 두 개의 도메인을 추가합니다.
127.0.0.1 example.com www.example.com
Route 객체의 example.com 호스트명(host)과 match되어 Blog 애플리케이션이 응답합니다.
$ curl http://example.com:8080
Blog, PHP on Unit!
호스트명이 아닌 127.0.0.1로 요청하겠습니다. 디폴트(Default) Route로 helloworld 애플리케이션으로 전달합니다.
$ curl http://127.0.0.1:8080
Hello, PHP on Unit!
2-2. 예제2. Named Route
이번엔 라우터 객체에 이름 주고, 조금 더 복잡한 구성을 해보겠습니다. route_name_config.json
구성 파일 내용은 아래와 같습니다.
라우터 객체의 이름은 “name”이고, host에 “www.example.com” 서브 도메인 하나를 더 추가하였습니다.
아래 scheme는 “http” 또는 “https”만 지정할 수 있습니다. 해당 예제에서는 아직 https 구성을 안해본 관계로 http로만 지정합니다.
{
"listeners": {
"*:8080": {
"pass": "routes/name"
}
},
"routes": {
"name": [
{
"match": {
"host": [ "example.com", "www.example.com" ]
},
"action": {
"pass": "applications/blog"
}
},
{
"match": {
"uri": "/admin/*",
"scheme": "http"
},
"action": {
"pass": "applications/helloworld"
}
}
]
},
"applications": {
"helloworld": {
"type": "php",
"root": "/www/helloworld/"
},
"blog": {
"type": "php",
"root": "/www/blog/"
}
}
}
위의 구성으로 Restful API를 통해 동적 재구성을 해보도록 하겠습니다.
sudo curl -X PUT --data-binary @route_name_config.json --unix-socket /var/run/control.unit.sock 'http://localhost/config/'
구성이 변경되었으면 성공 메시지가 출력됩니다.
{
"success": "Reconfiguration done."
}
적용된 구성 확인해보겠습니다.
$ sudo curl --unix-socket /var/run/control.unit.sock http://localhost/config
{
"listeners": {
"*:8080": {
"pass": "routes/name"
}
},
"routes": {
"name": [
{
"match": {
"host": [
"example.com",
"www.example.com"
]
},
"action": {
"pass": "applications/blog"
}
},
{
"match": {
"uri": "/admin/*",
"scheme": "http"
},
"action": {
"pass": "applications/helloworld"
}
}
]
},
"applications": {
"helloworld": {
"type": "php",
"root": "/www/helloworld/"
},
"blog": {
"type": "php",
"root": "/www/blog/"
}
}
}
구성도를 보면 다음과 같습니다.

추가한 서브 도메인으로 요청을 해보겠습니다. blog 애플리케이션으로 라우팅하여 응답하는 것을 확인 하실 수 있습니다.
$ curl http://www.example.com:8080
Blog, PHP on Unit!
/admin/으로 요청해보겠습니다.
$ curl http://127.0.0.1:8080/admin/
Admin, PHP on Unit!
3. 결론
NGINX Unit 자습서 2부에서는 NGINX Unit 앱 서버 자체에서 제공하는 Route 객체에 대해 다뤘습니다.
Route 기능을 통해 NGINX Unit 앱 서버로 구동된 다양한 애플리케이션으로 라우팅을 처리할 수 있습니다.
NGINX Unit 자습서 3부 시리즈에서는 NGINX Unit의 프록시 기능에 대해 다룰 것입니다.
NGINX Unit 공식 커뮤니티(슬랙 채널)는 NGINX STORE – Unit Community 메뉴를 통해 가입 가능합니다.
NGINX Unit에 대한 최신 포스트와 소식을 빠르게 받으시고 싶으시면 아래 뉴스레터를 구독하세요.
댓글을 달려면 로그인해야 합니다.