클라우드 + DevOps/Docker

Docker 네트워크 실습 :: NginX 리버스 프록시 구성

gamjadori 2024. 2. 21. 17:10
728x90

<실습3 :: NginX 리버스 프록시 구성>

1. NignX 설치 및 확인

ubuntu@host1:~$ sudo apt -y install nginx
>> 엔진엑스 설치
ubuntu@host1:~$ nginx -v
nginx version: nginx/1.18.0 (Ubuntu)
ubuntu@host1:~$ sudo systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: **active (running)** since Tue 2024-01-16 12:08:30 KST; 1min 36s ago
       Docs: man:nginx(8)
    Process: 12657 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 12658 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 12752 (nginx)
      Tasks: 5 (limit: 4597)
     Memory: 11.1M
        CPU: 45ms
     CGroup: /system.slice/nginx.service
             ├─12752 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             ├─12754 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
             ├─12755 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
             ├─12756 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
             └─12757 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

 1월 16 12:08:30 host1 systemd[1]: Starting A high performance web server and a reverse proxy server...
 1월 16 12:08:30 host1 systemd[1]: Started A high performance web server and a reverse proxy server.

 

2. 컨테이너 생성 및 확인

ubuntu@host1:~$ docker container run -d -p 8001:80 -h=alb-node01 --name=alb-node01 nginx
ccd6c942f9f5e9b3a22cd21f3457e1c06fbf5fa1b3af7f89a778fdca78af5d6b
ubuntu@host1:~$ docker container run -d -p 8002:80 -h=alb-node02 --name=alb-node02 nginx
5616dd359296f2c7d5f5157cd43744da453627b091e03061f4bd17f871d8bdfb
ubuntu@host1:~$ docker container run -d -p 8003:80 -h=alb-node03 --name=alb-node03 nginx
4e49879f5e7f65b84bdd218d9e63847f65bed9c882c05fe2290cd6dfcee5d861

<curl로 확인>

ubuntu@host1:~$ curl localhost:8001
<!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>

 

3. 컨테이너 3개를 구분하기 위해 index.html 수정

 

<컨테이너의 index.html 수정>

 

4. 수정된 파일 컨테이너에 이동

ubuntu@host1:~/Labs/ch05$ docker container cp ./index.8001.html alb-node01:/usr/share/nignx/html/in
dex.html
Successfully copied 2.05kB to alb-node01:/usr/share/nignx/html/index.html
ubuntu@host1:~/Labs/ch05$ docker container cp ./index.8002.html alb-node02:/usr/share/nignx/html/in
dex.html
Successfully copied 2.05kB to alb-node02:/usr/share/nignx/html/index.html
ubuntu@host1:~/Labs/ch05$ docker container cp ./index.8003.html alb-node03:/usr/share/nignx/html/in
dex.html
Successfully copied 2.05kB to alb-node03:/usr/share/nignx/html/index.html

<결과 확인>

 

5. nginx.conf 수정

ubuntu@host1:~/Labs/ch05$ sudo mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.org
[sudo] ubuntu 암호: 
ubuntu@host1:~/Labs/ch05$ ls -l /etc/nginx/
합계 44
drwxr-xr-x 2 root root    6  5월 31  2023 conf.d
-rw-r--r-- 1 root root 1125  5월 31  2023 fastcgi.conf
-rw-r--r-- 1 root root 1055  5월 31  2023 fastcgi_params
-rw-r--r-- 1 root root 2837  5월 31  2023 koi-utf
-rw-r--r-- 1 root root 2223  5월 31  2023 koi-win
-rw-r--r-- 1 root root 3957  5월 31  2023 mime.types
drwxr-xr-x 2 root root    6  5월 31  2023 modules-available
drwxr-xr-x 2 root root 4096  1월 16 12:08 modules-enabled
**-rw-r--r-- 1 root root 1447  5월 31  2023 nginx.conf.org**
-rw-r--r-- 1 root root  180  5월 31  2023 proxy_params
-rw-r--r-- 1 root root  636  5월 31  2023 scgi_params
drwxr-xr-x 2 root root   21  1월 16 12:08 sites-available
drwxr-xr-x 2 root root   21  1월 16 12:08 sites-enabled
drwxr-xr-x 2 root root   51  1월 16 12:08 snippets
-rw-r--r-- 1 root root  664  5월 31  2023 uwsgi_params
-rw-r--r-- 1 root root 3071  5월 31  2023 win-utf
>> 기존 nginx.conf는 다른 이름으로 수정해서 옮기기

<새로 nginx.conf 생성>

ubuntu@host1:~/Labs/ch05$ sudo vi /etc/nginx/nginx.conf

<vi 편집내용>

ubuntu@host1:~/Labs/ch05$ cat /etc/nginx/nginx.conf
events {
        worker_connections 768;
        # multi_accept on;
}

http {
        upstream backend-alb {
                server 192.168.56.101:8001;
                server 192.168.56.101:8002;
                server 192.168.56.101:8003;
        }
        server {
                listen 80 default_server;
                location / {
                        proxy_pass <http://backend-alb>;
                }
        }
}

 

6. 엔진엑스 재가동

ubuntu@host1:~/Labs/ch05$ sudo vi /etc/nginx/nginx.conf
ubuntu@host1:~/Labs/ch05$ sudo systemctl restart nginx.service
ubuntu@host1:~/Labs/ch05$ sudo systemctl status nginx.service
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset>
     Active: active (running) since Tue 2024-01-16 12:38:17 KST; 12s ago
       Docs: man:nginx(8)
    Process: 16874 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_pro>
    Process: 16875 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; >
   Main PID: 16876 (nginx)
      Tasks: 2 (limit: 4597)
     Memory: 1.6M
        CPU: 13ms
     CGroup: /system.slice/nginx.service
             ├─16876 "nginx: master process /usr/sbin/nginx -g daemon on; mast>
             └─16877 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" >

 1월 16 12:38:17 host1 systemd[1]: Starting A high performance web server and >
 1월 16 12:38:17 host1 systemd[1]: Started A high performance web server and a>

 

7. 웹서버 확인