기타
NGINX 프록시 설정 방법
ipxy
2025. 3. 28. 08:26
728x90
✅ NGINX 프록시 설정 방법 정리
📌 목적
외부에서 NGINX 서버로 들어온 요청을 내부의 다른 서버로 프록시(중계) 하여 전달.
🧭 기본 구조
[ 클라이언트 ]
↓
[ NGINX (B망) ]
↓
[ 내부 서버 (C망의 API, DB 등) ]
🛠️ 설정 단계 요약
① NGINX 설치
Ubuntu/Debian 계열:
sudo apt install nginx
RHEL/CentOS/Rocky 계열:
sudo yum install nginx # CentOS/RHEL 7
sudo dnf install nginx # CentOS/RHEL 8+, Rocky/Alma
② 프록시 설정 파일 생성
sudo nano /etc/nginx/conf.d/c-proxy.conf
✨ 예시 설정
server {
listen 10000;
server_name _;
location / {
proxy_pass http://<C망_IP>:<C망_포트>/;
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_pass 끝에 / 꼭 붙여야 하며, 내부망 IP/포트 정확히 기입할 것
③ 기존 default 설정 제거 또는 조정
- /etc/nginx/nginx.conf 또는 /etc/nginx/conf.d/default.conf 에서→ ❌ 충돌 유발, 주석 처리 또는 삭제 권장
- listen 10000 default_server;
④ 설정 반영
sudo nginx -t # 설정 문법 확인
sudo systemctl reload nginx
⑤ 방화벽 포트 열기 (10000번 예시)
firewalld 사용 시:
sudo firewall-cmd --permanent --add-port=10000/tcp
sudo firewall-cmd --reload
🧪 테스트
- NGINX 서버에서 C망 접속 확인:
curl http://<C망_IP>:<포트>/
- 외부에서 프록시 확인:
curl http://<NGINX서버_IP>:10000/
✅ 고급 설정 옵션
기능 | 설정 예시 |
특정 경로만 프록시 | location /api/ { proxy_pass http://...; } |
경로 일부 제거 | location /api/ { proxy_pass http://.../; } + 경로 매핑 유의 |
HTTPS 대상 프록시 | proxy_pass https://...; |
timeout 조정 | proxy_read_timeout, proxy_connect_timeout 등 설정 가능 |
캐시 제어 | proxy_cache, expires 설정 가능 |
인증 헤더 전달 | proxy_set_header Authorization $http_authorization; |
✅ 실무 팁
- proxy_pass 뒤에 슬래시(/) 붙이는 방식에 따라 경로가 달라지므로 주의!
- 로그로 확인:
- tail -f /var/log/nginx/access.log tail -f /var/log/nginx/error.log
🧱 사용 예시 요약
요청 프록시 대상
/ | http://C망_IP:포트/ |
/api/ | http://C망_IP:포트/api/ |
/c-api/** | proxy_pass http://.../ + location /c-api/ 사용 |
728x90