교육/코드스테이츠

[25일차] Nginx를 통한 Reverse Proxy 설정

nooh_ij 2022. 12. 30. 14:03

 

Nginx를 통한 Reverse Proxy 설정

1. 지시어 확인

  • include를 통해 지시어 블록을 원하는 위치에서 불러와 사용할 수 있다.
  • 먼저 cat /etc/nginx/nginx.conf을 통해 include를 확인한다.
  • 나는 sites-enabled 디렉토리를 사용하여 지시어를 설정할 것이다.
  • 먼저 /etc/nginx/sites-available, sites-enalbed를 확인하면 default 파일을 확인하고 수정한다.

2. 지시어 작성

  • 지시어들을 보면, nginx 서버를 구동할 때 10026의 포트를 사용하고 서버이름을 localhost로 지시한다.
  • location에서는 디렉토리 / 에 대해 지시하며, 그 안에는 프록시에 대한 지시어들이 있다
  • proxy_set_header는 프록시 서버에 대한 헤더를 설정한다.
  • proxy_pass는 지정된 서버에 요청 헤더를 전달한다.
    .

프록시 설정 지시어

1. proxy_cache_path (server블럭 위에 위치해야함)

  • 캐시의 경로와 매개변수를 설정한다. 해당 경로에 디렉토리가 없을 경우 생성됨,
  • path 뒤에 있는 내용들로 설정을 진행된다
  • /etc/nginx/cache : 캐시의 디렉토리
  • levels : 설정된 경로 아래에 디렉토리의 계층을 설정
  • key_zone : 캐시 키로 사용될 이름과 크기를 설정
  • max_size : 캐시 크기의 상한선을 설정(선택사항).
  • inactive : 삭제되지 않고 캐시에 남아있을 수 있는 기간을 지정
  • 캐시 경로를 설정하면 서버블록 안에 proxy_cache mycache;를 작성

 

2. proxy_cache_lock

여러 클라이언트가 캐시에 최신이아닌 파일을 요청하는 경우, 첫번째 요청만 Origin 서버를 통해 통과할 수 있고 나머지는 대기 후 캐시에서 파일을 가져옵니다.

 

3. proxy_cache_methods

클라이언트 요청 메서드 목록을 지정해줍니다. “GET” “HEAD” 메서드는 default로 지정되어있지만 명시적으로 지정해 주는 것이 좋습니다.

  • post요청:  GET HEAD POST;

 

4. proxy_cache_valid

valid 뒤에 오는 응답 코드에 대해 캐싱 시간을 설정해 준다. 응답코드 대신 any를 작성할 경우 모든 응답 코드에 대한 캐싱시간을 설정해준다.

 

5. 캐시 설정을 마치고 난 후, add_header를 통해 헤더를 추가하고 확인할 수 있다

  • X-Cache-Status Value
  • MISS : 캐시에서 응답을 찾을 수 없어 Origin 서버에서 가져옴.
  • BYPASSproxy_cache_bypass : 요청이 지시문과 일치하여 Origin 서버에서 응답 가져옴.
  • EXPIRED : 캐시의 항목이 만료됨, 응답은 Origin 서버의 최신 콘텐츠 포함
  • STALE : 원본 서버가 제대로 응답하지 않아 콘텐츠가 오래됨.
  • UPDATING : 이전 요청에 대한 응답으로 업데이트
  • REVALIDATED : proxy_cache_revalidate 지시문이 활성화되었을때 (on)
  • HIT : 응답이 캐시에서 직접 가져온 유효하고 새로운 컨텐츠가 포함되어 있음.

 

 

참고자료

https://nginx.org/en/docs/http/ngx_http_proxy_module.html?&_ga=2.204854544.1036597238.1646892199-1288101505.1645751770#proxy_cache_path 

 

Module ngx_http_proxy_module

Module ngx_http_proxy_module The ngx_http_proxy_module module allows passing requests to another server. Example Configuration location / { proxy_pass http://localhost:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } Directives

nginx.org

https://nginxstore.com/blog/nginx/nginx-%EB%B0%8F-nginx-plus%EB%A5%BC-%EC%82%AC%EC%9A%A9%ED%95%9C-caching/

 

NGINX Cache 시스템 이해하기

NGINX Cache 기능을 사용하여 초보자와 고급 사용자 모두 NGINX 및 NGINX Plus에 포함된 콘텐츠 캐시 기능을 활용하여 더 나은 성능을 볼 수 있도록 도와주는 기술을 다룹니다.

nginxstore.com

https://docs.nginx.com/nginx/admin-guide/content-cache/content-caching/

 

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

https://www.nginx.com/products/nginx/caching/

 

Content caching

Improve performance and reduce load on upstream application servers and databases with scalable content caching in NGINX Plus.

www.nginx.com

https://www.nginx.com/blog/nginx-caching-guide/

 

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