Settings/Setting

Window nginx loadbalancing test in localhost

Juzdalua 2025. 1. 17. 11:49

1. 다운로드 및 환경변수 추가

https://nginx.org/en/download.html

 

nginx: download

 

nginx.org

 

C 폴더에 압축 해제

 

환경변수 추가


2. 설정파일(nginx.conf) 설정

 

http 블락에 아래 설정 내용을 추가한다.

클라이언트는 5000번 포트로 접근을 시도하고, nginx에서 4001번과 4002번 포트의 서버에 배분한다.

# nginx.conf

# 5000번 포트로 접속된 클라이언트를 4001, 4002 포트 서버로 리버스프록시

...

http {
    upstream local_test_server { # default: 라운드로빈
        server localhost:4001;
        server localhost:4002;
    }

    server {
        listen 5000;
    	server_name localhost;

    	location / {
        	proxy_pass http://local_test_server;
    	}
    }

	...
}

 

# ex) 라이브모드 ip 부여시

http {
    upstream live_test_server { # default: 라운드로빈
        server 192.168.0.101:4001; # 첫 번째 서버 IP와 포트
        server 192.168.0.102:4002; # 두 번째 서버 IP와 포트
    }

    server {
        listen 5000;
        server_name 192.168.0.100; # 이 서버의 IP 주소 (클라이언트가 접근할 IP)

        location / {
            proxy_pass http://live_test_server; # upstream에서 정의한 서버 그룹
        }
    }

    ...
}

 

변경내용 테스트

ok, successful이 뜨면 성공


3. 접속 테스트

# nginx 시작
nginx

# nginx 재시작
nginx -s reload

 

// server.ts

app.get('/', (req: Request, res: Response) => {
  console.log('HI');
  res.end();
});

http://localhost:5000으로 접속하면 알고리즘에 의해 클라이언트를 서버에 분배한다.


4. 서비스 종료

# 실행중인 서비스 확인
tasklist /FI "IMAGENAME eq nginx.exe"

# nginx 종료
nginx -s quit

 

 

아래 에러가 발생한다면, nginx가 설치된 경로로 이동하여 명령어를 실행하자.


번외) nginx 없이 직접 로드밸런서 구성해보기

https://github.com/Juzdalua/test-loadbalancer-nodejs

 

GitHub - Juzdalua/test-loadbalancer-nodejs: Create 4 servers to check server health check

Create 4 servers to check server health check. Contribute to Juzdalua/test-loadbalancer-nodejs development by creating an account on GitHub.

github.com