개발

[웹개발] (1) Nginx로 Load Balancer 적용하기

2023. 8. 6. 15:20
목차
  1. 1. Web Server
  2. 2. Nginx
  3. (1) Nginx 설치
  4. (2) Ningx에 Load Balancer 세팅하기

간단한 웹 데모를 개발하면서 겪었던 과정을 정리하고자 한다. 전체적인 구조는 다음과 같으며 Load Balancer(A)와 Web Server(B)는 별도의 서버로 구성하였다.

 

환경정보는 다음과 같다.

  • Ubuntu 20.04
  • Python 3.8
  • FastAPI 0.100.1

 

1. Web Server

FastAPI로 Frontend, Middleware, Backend를 구현하였고 이 레포를 기반으로 편하게 만들었다.

 

2. Nginx

Nginx는 2004년에 개발된 비동기 이벤트 기반의 경량화된 웹 서버 프로그램이다. 20년된 프로그램이지만 가벼워서 아직도 많이 쓰인다.

 

(1) Nginx 설치

먼저 A서버에 Nginx를 설치하자.

sudo apt update
sudo apt install nginx

 

다음으로 Nginx를 사용하기 위해서 방화벽을 설정하자.

sudo ufw app list

>>>> 
Available applications:
  Nginx Full
  Nginx HTTP
  Nginx HTTPS
  OpenSSH

 

추후에 SSL 인증까지 적용할 것이므로 HTTP, HTTPS를 모두 활성화시켜주자. 그리고 OpenSSH도 활성화 시켜주자.

sudo ufw allow 'Nginx HTTP'
sudo ufw allow 'Nginx HTTPS'
sudo ufw allow OpenSSH

sudo ufw status
>>>>
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
Nginx HTTP                  ALLOW       Anywhere
Nginx HTTPS                 ALLOW       Anywhere
OpenSSH (v6)                ALLOW       Anywhere (v6)
Nginx HTTP (v6)             ALLOW       Anywhere (v6)
Nginx HTTPS (v6)            ALLOW       Anywhere (v6)

이때 Status: inactive라는 메세지가 뜨면 sudo ufw enable으로 방화벽을 활성화시켜준다.

 

 그리고 Nginx를 활성화시켜주자.

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 Thu 2023-08-03 01:21:48 UTC; 23h ago
       Docs: man:nginx(8)
    Process: 37107 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 37108 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 37109 (nginx)
      Tasks: 3 (limit: 4402)
     Memory: 4.5M
     CGroup: /system.slice/nginx.service
             ├─37109 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ├─37110 nginx: worker process
             └─37111 nginx: worker process

 

(2) Ningx에 Load Balancer 세팅하기

Nginx를 설치하면 다음과 같이 3가지 폴더가 생긴다.

etc/nginx/sites-available
etc/nginx/sites-enabled
etc/nginx/conf.d

sites-available에는 각 도메인 별 설정 파일을 저장해둘 수 있고, 실제 서비스는 sites-enabled에 symbolic link를 연결해서 띄운다. 하지만 이런 방식으로 서비스를 띄우는 것은 권장되지 않는다고 한다(참고).

 

이제 A(Load Balancer)에 접속하면 B에 연결되도록 A서버에서 /etc/nginx/sites-enabled/default 파일을 다음과 같이 작성하자.

upstream (B주소) {
	server (B주소):9050;
	server (B주소):9051;
}

server {
	listen 80 ssl;
	listen [::]:80 ssl;

	root /var/www/html;
	index index.html index.htm index.nginx-debian.html;

	server_name _;

	location / {
		proxy_pass http://(B주소);
		proxy_set_header Host $host;
	    proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header X-Forwarded-Proto $scheme;
	 }
}

 

이후 sudo systemctl restart nginx로 재시작을 해주면 Load Balancer가 잘 적용되는 것을 확인할 수 있다.

 

 

 

[참고]

  • https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-20-04
  • https://sy34.net/nginxyi-gibonjeogin-seoljeongdeuleul-alaboja/
  • https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-as-a-reverse-proxy-on-ubuntu-22-04
  • https://kscory.com/dev/nginx/loadbalancer

 

 

 

 

728x90
저작자표시 비영리 변경금지 (새창열림)

'개발' 카테고리의 다른 글

[Docker] Docker 용량 정리  (0) 2023.08.30
[웹개발] (2) Nginx로 Load Balancer + SSL 적용하기  (0) 2023.08.06
[Miniconda] Ubuntu에 Miniconda 설치하기  (0) 2023.08.01
Gunicorn & Uvicorn  (0) 2023.07.09
[Nginx] 맥os에 Nginx 설치하기  (0) 2023.07.08
  1. 1. Web Server
  2. 2. Nginx
  3. (1) Nginx 설치
  4. (2) Ningx에 Load Balancer 세팅하기
'개발' 카테고리의 다른 글
  • [Docker] Docker 용량 정리
  • [웹개발] (2) Nginx로 Load Balancer + SSL 적용하기
  • [Miniconda] Ubuntu에 Miniconda 설치하기
  • Gunicorn & Uvicorn
Fine애플
Fine애플
이것저것
Fine애플
끄적끄적
Fine애플
전체
오늘
어제
  • 분류 전체보기 (167)
    • 논문 및 개념 정리 (27)
    • Pattern Recognition (8)
    • 개발 (57)
    • python 메모 (45)
    • pytorch, tensorflow (5)
    • 알고리즘 (9)
    • Toy Projects (4)
    • 통계이론 (2)
    • Reinforcement Learning (10)

블로그 메뉴

  • 홈

공지사항

인기 글

태그

  • tensorflow
  • container
  • ubuntu
  • reinforcement learning
  • python
  • PyTorch
  • GPU
  • transformer
  • nlp
  • 자연어
  • Bert
  • 언어모델
  • Docker
  • Probability
  • pandas
  • 딥러닝
  • BigBird
  • 개발환경
  • 알고리즘
  • miniconda

최근 댓글

최근 글

hELLO · Designed By 정상우.
Fine애플
[웹개발] (1) Nginx로 Load Balancer 적용하기
상단으로

티스토리툴바

개인정보

  • 티스토리 홈
  • 포럼
  • 로그인

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.