IT STUDY LOG
Sprint - Proxy Server 본문
이번 스프린트에서는 nginx를 통해 아래의 요구사항을 반영한 리버스 프록시(Reverse Proxy) 서버를 작성합니다. nginx는 정적 웹페이지를 호스팅하기 위해서 사용될 뿐만 아니라, 원(origin) 서버의 앞 단에 위치하여, 로드밸런서, 캐싱, 보안 등을 위한 프록시 서버로 사용됩니다.
# 학습 목표
- 리버스 프록시(Reverse Proxy)와 포워드 프록시(Forward Proxy)에 대해서 이해하고, 특징을 학습합니다.
- 프록시 서버에 사용되는 지시어(directives)를 학습하고, 이를 활용합니다.
- 프록시 서버에서 원 서버로 전달되는 요청 헤더를 설정하는 방법을 학습합니다.
- 프록시 서버의 cache-control 방법과 캐시 관련 지시어 사용법을 확인합니다.
# 해결 과제
- 내 컴퓨터를 원(origin) 서버의 리버스 프록시 서버로 만들어야 합니다.
- 캐싱 기능을 포함한 프록시 서버를 작성해야 합니다.
- sprint-proxy-server 레포지토리에 Pull Request를 통해, nginx.conf 설정 내용을 제출해야 합니다.
# 실습 자료
sprint-proxy-server
# 과제 항목별 진행 상황
1. 내 컴퓨터를 원(origin) 서버의 리버스 프록시 서버로 만들기
- nginx proxy 서버 공식문서를 참고하여, 프록시 서버 만들기
# nginx.conf 파일
# ... 상략 ... #
http {
# ... 중략 .... #
server {
# 프락시 서버 주소, 포트 설정
listen 10026;
server_name localhost;
location / {
# 프락시 서버 관련 설정
# 헤더에 호스트명 지정
proxy_set_header Host $host;
# 헤더에 IP 지정
proxy_set_header X-Real-IP $remote_addr;
# custom 헤더
proxy_set_header myname devops04-roheerumi;
# origin-server 설정
proxy_pass http://{originserver};
# ...하략 ... #
}
}
}

2. 프록시 서버에 캐싱 기능 포함하기
- nginx caching 공식문서 와 nginx caching guide 를 참고해 캐싱 기능 구현
# nginx.conf 파일
# ... 상략 ... #
http {
# ... 중략 ... #
# 캐시 저장할 경로, 메모리상 캐시 이름, 캐시 할당 크기 지정
proxy_cache_path /{캐시 저장 디렉토리}/cache keys_zone=mycache:10m;
server {
listen 10026;
server_name localhost;
location / {
# ... 중략 ... #
# 프락시 캐시 정책 설정
# 사용할 캐시명
proxy_cache mycache;
# 캐시 유효 기간 설정, 상태코드 (any, 200, 404 등)와 시간 (s,m,...)
proxy_cache_valid any 5s;
# X-Cache-Status 헤더 추가
add_header X-Cache-Status $upstream_cache_status;
# Cache-Control 헤더 추가
add_header Cache-Control "public";
}
}
}

# TROUBLE SHOOTING LOG
[문제 1] 별도로 생성한 .conf 파일에서 설정할 때 오류 발생
오류 내용
[emerg] "http" directive is not allowed here in /etc/nginx/sites-enabled/proxy_study:3
파일 내용
# /etc/nginx/sites-available/proxy_sutdy.conf
http {
proxy_cache_path /{캐시 저장 디렉토리}/cache keys_zone=mycache:10m;
server {
listen 10026;
server_name localhost;
location / {
proxy_cache_valid any 5s;
add_header X-Cache-Status $upstream_cache_status;
add_header Cache-Control "public";
proxy_cache mycache;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header myname devops04-roheerumi;
proxy_pass http://3.39.193.136:8080;
}
}
}
원인
- nginx.conf 파일에서 http 지시자는 한 번만 사용할 수 있으므로 다른 설정 파일에 http가 들어가면 중첩되어 오류
해결 방안
- http 지시자를 제거해서 설정, 이 경우 nginx.conf 파일에 별도로 proxy_cache_path를 지정하지 않아도 정상 동작
# /etc/nginx/sites-available/proxy_sutdy.conf
proxy_cache_path /{캐시 저장 디렉토리}/cache keys_zone=mycache:10m;
server {
listen 10026;
server_name localhost;
location / {
proxy_cache_valid any 5s;
add_header X-Cache-Status $upstream_cache_status;
add_header Cache-Control "public";
proxy_cache mycache;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header myname devops04-roheerumi;
proxy_pass http://3.39.193.136:8080;
}
}
#References
NGINX Reverse Proxy | NGINX Plus
NGINX Reverse Proxy Configure NGINX as a reverse proxy for HTTP and other protocols, with support for modifying request headers and fine-tuned buffering of responses. This article describes the basic configuration of a proxy server. You will learn how to p
docs.nginx.com
NGINX Content Caching | NGINX Plus
NGINX Content Caching Cache both static and dynamic content from your proxied web and application servers, to speed delivery to clients and reduce the load on the servers. Overview When caching is enabled, NGINX Plus saves responses in a disk cache and use
docs.nginx.com
A Guide to Caching with NGINX and NGINX Plus - NGINX
Performance is critical to success, and caching is one basic tool for improving it. Learn all about caching with NGINX and NGINX Plus.
www.nginx.com
nginx: [emerg] "http" directive is not allowed here in /etc/nginx/sites-enabled/default:1
I'm new to NGINX and I'm trying to setup minimal working thing. So I trying to run aiohttp mini-app with nginx and supervisor (by this example). But I can't configure Nginx right and getting the
stackoverflow.com
'devops bootcamp 4 > pair/team log' 카테고리의 다른 글
Sprint - 애플리케이션 컨테이너화 (0) | 2023.04.13 |
---|---|
Sprint - YAML 작성 (0) | 2023.04.11 |
Sprint - 로그 파이프라인 (0) | 2023.03.30 |
Sprint - Cozstory WAS 개발 (0) | 2023.03.28 |
Mini WAS 개발 hands-on (0) | 2023.03.28 |