KAKAO.GG
백엔드

Nginx를 이용한 포트별 API 경로 분기

GG
2025. 7. 2.
조회 36
#Nginx 경로 라우팅#리버스 프록시 설정#location 별 proxy_pass

두 개의 백엔드 서버(8080, 8090)를 운영하면서, 요청 경로에 따라 적절한 서버로 요청을 분기(route) 시키는 방법

 

예시) 사용 중인 api.test.com 도메인에서

  • /service1 요청은 8090 포트로 전달

  • /service0 요청은 8080 포트로 전달

 

시스템 구성 요약

항목

내용

도메인

https://api.test.com

기존 서버

8080 포트에서 /service0/* 처리

새 서버

8090 포트에서 /service1 처리

목적

경로에 따라 요청을 정확히 분기

server {
    listen 443 ssl;
    server_name api.test.com;
    # 1. /service1 경로는 새 서버로 분기 (8090 포트)
    location /service1 {
        proxy_pass http://localhost:8090;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
    # 2. /service0 경로는 기존 서버로 분기 (8080 포트)
    location /service0 {
        proxy_pass http://localhost:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}