자습서-NGINX Unit Proxy 구성해보기, 3부
NGINX Unit Proxy, 이번 자습서에서 다룰 주제입니다.
NGINX Unit 자습서 시리즈 1부에서는 NGINX Unit 동적 구성에 대한 맛보기, 2부에서는 NGINX Unit의 Route 객체 구성하여 애플리케이션 라우팅 처리를 해봤습니다.
이번 3부에서는 NGINX Unit의 Proxy 기능을 구성하여 리버스 프록시로서의 동작을 확인해볼 것입니다.
NGINX Unit 자습서 시리즈의 실습 환경은 모두 Ubuntu 22.04.1 LTS x86_64 환경에서 진행합니다.
목차
1. NGINX Unit Proxy 구성
2. NGINX 설치
3. NGINX Unit Proxy 동작 확인
4. 결론
1. NGINX Unit Proxy 구성
NGINX Unit의 동적 구성을 위하여 proxy_config.json
파일을 작성합니다. JSON 파일에는 Listener, Route, Application 객체가 있습니다. Proxy 서버는 같은 로컬 호스트 아이피의 NGINX 인스턴스입니다.
{
"listeners": {
"*:8080": {
"pass": "routes"
}
},
"routes": [
{
"match": {
"host": "example.com"
},
"action": {
"proxy": "http://192.168.250.228:80"
}
},
{
"action": {
"pass": "applications/my-app"
}
}
],
"applications": {
"blog": {
"type": "php",
"root": "/www/blog/"
}
}
}
JSON으로 작성한 구성 명세로 NGINX Unit에서 제공하는 Restful API를 통해 동적 재구성을 진행합니다.
구성이 성공되면 “success” 메시지로 응답이 출력됩니다.
$ sudo curl -X PUT --data-binary @proxy_config.json --unix-socket /var/run/control.unit.sock http://localhost/config
{
"success": "Reconfiguration done."
}
재구성된 NGINX Unit의 config 정보를 JSON으로 출력해봅니다. 아래와 같다면 NGINX Unit의 구성 준비는 다 되었습니다.
$ sudo curl --unix-socket /var/run/control.unit.sock http://localhost/config
{
"listeners": {
"*:8080": {
"pass": "routes"
}
},
"routes": [
{
"match": {
"host": "example.com"
},
"action": {
"proxy": "http://192.168.250.228:80"
}
},
{
"action": {
"pass": "applications/blog"
}
}
],
"applications": {
"blog": {
"type": "php",
"root": "/www/blog/"
}
}
}
2. NGINX 설치
리버스 프록시 동작 확인을 위한 로컬 호스트에 NGINX OSS 인스턴스를 설치합니다. NGINX STORE – NGINX 공식 문서에서 설치 매뉴얼을 참고하셔서 설치를 진행해주세요. (아래 명령어는 앞단계를 생략하였습니다.)
해당 테스트를 위해 메인라인(mainline) 버전을 설치하였습니다.
$ sudo apt update
$ sudo apt install nginx
NGINX의 버전은 다음과 같습니다.
$ nginx -v
nginx version: nginx/1.23.1
NGINX 인스턴스를 구동하고 상태를 확인합니다.
$ sudo systemctl start nginx
$ sudo systemctl status nginx
● nginx.service - nginx - high performance web server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2022-10-13 10:28:58 KST; 24s ago
Docs: https://nginx.org/en/docs/
Process: 1742 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 1743 (nginx)
Tasks: 3 (limit: 1029)
Memory: 2.5M
CPU: 8ms
CGroup: /system.slice/nginx.service
├─1743 "nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf"
├─1744 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
└─1745 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
Oct 13 10:28:58 nginxstore.com systemd[1]: Starting nginx - high performance web server...
Oct 13 10:28:58 nginxstore.com systemd[1]: Started nginx - high performance web server.
example.com 호스트 도메인 매칭으로 NGINX로 프록시 되는 것을 확인하기 위해 /etc/hosts 파일에 아래를 추가합니다.
127.0.1.1 example.com
3. NGINX Unit Proxy 동작 확인
첫 번째는 127.0.0.1 아이피로 요청을 합니다. route 로직을 통해 match되는 Host가 없어서 default route인 blog 애플리케이션(application)으로 라우팅되어 전달됩니다. blog 애플리케이션의 디렉토리 구조 및 index.php 파일은 NGINX Unit 자습서 시리즈 2부를 참고해주세요.
$ curl http://127.0.0.1:8080
Blog, PHP on Unit!
아래 명령어를 실행하면, 이제 example.com 호스트 도메인 macth로 인해 NGINX 인스턴스로 proxy되는 결과를 확인해보실 수 있습니다.
$ curl http://example.com:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
4. 결론
NGINX Unit 앱 서버는 HTTP 프록시를 지원합니다. 프록시 대상에 UNIX, IPv4, IPv6 Socket Address를 사용할 수 있습니다. HTTPS로의 프록시는 아직 지원하지 않습니다.
NGINX Unit 자습서 시리즈 4부에서는 NGINX Unit이 애플리케이션 서버 역할 뿐만 아니라 웹 서버의 핵심 기능 중 하나인 정적인 파일(Static Files)을 제공하는 기능에 대해 다룰 예정입니다.
NGINX Unit 공식 커뮤니티(슬랙 채널)는 NGINX STORE – Unit Community 메뉴를 통해 가입 가능합니다.
NGINX Unit에 대한 최신 포스트와 소식을 빠르게 받으시고 싶으시면 아래 뉴스레터를 구독하세요.