back-end를 처음 배우고 싶었던 이유의 답이 nginx에 있었다.
매학기 수강신청을 할때 서버가 터지는 이유를 알고 싶었다.
서버를 구축하는데 있어서 원활하게 돌리기 위해서는 많은 비용이 든다고만 알고 있었지만, 구체적으로 어떤 비용이 어떻게 필요한지 몰랐다.
how to install nginx on mac
- brew install nginx
- nginx를 설치하면 /usr/local/etc/nginx에 설치된다.
https://smali-kazmi.medium.com/setup-nginx-node-js-on-mac-os-x-b31eda9f7d5d
nginx stop - cd [nginx.exe path] / nginx.exe –s stop
nginx로 reverse proxy server를 구현하려면 기본적으로 nodejs로 웹서버를 열고 해당 port로 nginx가 연결이 되어야 한다.
server {
listen 8081;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://localhost:3000;
}
위와 같은 방법이 reverse proxy - port 8081로 접속하면 port 3000으로 접속하게 된다.
upstream back-end {
server localhost:9000 weight=5;
server localhost:9001;
server localhost:9002;
server 192.0.0.1 backup;
}
server {
listen 8081;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://back-end;
}
위와 같은 방법이 load balancing이다. 8081로 접속하면 port 9000는 다른 port보다 5배 많은 트래픽을 받는다. 다른 port가 터지면 backup server를 받는다.
load balancing through reverse proxy server
위 사진을 보면 local 에서 port 9000, 9001 ,9002 를 열어 "http://localhost:900X/"의 화면을 구분하도록 모두 다르게 세팅하였다.
위 사진을 보면 절대적으로 port 9000에 많은 트래픽이 몰리는 것을 알 수 있다.
이렇게 해서 port 8000을 client에게 보이지만 실제로 서버에 접속하는 port는 8000이 아닌 9000, 9001, 9002 로 (reverse proxy + load balancing)
비중 또한 사용자 설정이 가능하다는 것을 알 수 있다.
참고로 트래픽의 분산은 default로 round robin algorithm
방식을 따른다.