nginx 20

nginx proxy hierarchy

nginx proxy를 이용해 많은 contents를 cache할려면, 아래와 같은 hierarchy구조를 고려하여야 한다. my (edge cache server)의 disk 의 용량이 한계가 있기 때문에 아래와 같이 relay 계층을 추가하고, consistance hash구조로 만들면 relay-my에는 서로다른 contents를 수용할 수 있어 분산처리 및 많은 contents를 보유한 서비스에 대한 cache 효율을 높일 수 있다. 아래와 같이 edge서버에는 my.conf 에 proxy_pass http://relay-my를 추가하고, upstream블락에 consistant hash를 구성하면 3개의 서버로 트래픽 분산 및 서버추가 삭제가 용의하다. 이때 relay서버의 hash를 $host..

nginx 2023.06.19

nginx upstream module - part 3 (dns lookup)

https://kubby72.tistory.com/entry/nginx-upstream-module 에서 upstram 에 대한 load-balacing방법에 대해 알아 보있다. 이중upstream에 아래와 같이 dns (org)을 입력하고 upstream org { hash $host$uri consistent; server org; } /etc/hosts를 아래와 같이 수정해 dnsmasq로 dns 를 통해 전파하게 되면 https://kubby72.tistory.com/entry/simple-DNS-server-dnsmasq 192.168.1.100 org 192.168.1.101 org 192.168.1.102 org nginx는 아래와 같이 upstream 블락에 server ip_addr를 3..

nginx 2023.06.19

nginx upstream module - part2

server address [parameters]; server directive는 서버의 address및 기타 parameter를 정의합니다 . address는 port를 포함한 도메인 이름 또는 IP 주소로 지정하거나, “unix:” prefix 뒤에 UNIX-domain socket path 를 지정할 수 있습니다. port 를 지정하지 않으면 포트 80이 사용됩니다. domain name lookup 시 다수의 서버가 lookup될 경우, 도메인 이름으로 지정하면 한 번에 여러 서버를 지정할 수 있습니다. 사용할 수 있는 parameter는 다음과 같습니다. ■ weight= upstream backend { server backend1.example.com weight=5; server back..

nginx 2023.06.19

Consistant hash

URL을 md5로 hash한  $hsh 변수에 저장된 16진수 문자열을 10진수 정수값으로 변환한 후, 총 슬롯 수 $total_slots로 나누어 node_index를 구하는 함수를 bash로 작성하면 아래와 같다. hash_fn() { key=$1 total_servers=3 # converting data into bytes and passing it to hash function hsh=$(echo -n $key | md5sum | awk '{print $1}') # converting the HEX digest into equivalent integer value and converting negative values to positive echo $(( (16#..

nginx 2023.06.16

nginx upstream module - part1 (load balancing)

nginx에는 proxy_pass지시자로 원본서버(upstream서버)로 traffic을 보낼 수 있는데 upstream 모듈을 사용하면 다양한 형태로 upstream을 구성할 수 있다. server { listen 80; server_name my; location / { proxy_cache my_zone; proxy_cache_key $host$uri$is_args$args; proxy_pass http://org; add_header X-CACHE-STATUS $upstream_cache_status; } } 위 구성은 proxy_pass로 http://org을 지정하고 있고, 이는 nginx 가 loading될 때 dns lookup을 통해 획득한 IP로 트래픽을 보내게 된다. 현재 서버구성은 ..

nginx 2023.06.15

nginx cache status 확인

nginx 설정에서 아래와 같이 X-CACHE-STATUS해더를 추가해 주면 브라우저에서 cache status 확인이 가능하다. server { listen 80; server_name my; location / { proxy_cache my_zone; proxy_cache_key $host$uri$is_args$args; proxy_pass http://org; add_header X-CACHE-STATUS $upstream_cache_status; } } 위와 같이 설정 후 curl로 확인하면 아래와 같이 X-CACHE-STATUS: MISS 해더가 추가된 것을 확인할 수 있다. $ curl my/1.json -I HTTP/1.1 200 OK Server: nginx/1.24.0 Date: Thu,..

nginx 2023.06.15

nginx cache-control 및 priority

캐싱은 'client' 와 'cache proxy' 모두에서 발생할 수 있습니다. 원본서버(Origin)에서는 HTTP 응답 헤더를 사용하여 'client' 와 'cache proxy' 모두에 대한 캐싱을 제어할 수 있습니다. 원본의 Cache-Control 원본 서버는 Cache-Control응답 헤더를 통해 콘텐츠 캐싱 여부 및 TTL등을 제어할 수 있습니다.. public: client 및 proxy 모두에 의해 캐시될 수 있음 private: client 에 의해 캐시될 수 있지만 proxy 는 캐시될 수 없음 max-age: cache TTL을 결정하고 그 후에 요청은 Origin에 요청 s-maxage: proxy에만 사용되는 TTL must-revalidate: max-age TTL이 만료되..

nginx 2023.06.14

cli로 nginx cache file 찾기, 삭제 및 TTL변경

https://kubby72.tistory.com/entry/nginx-cache-key설정 에서 nginx에서 cache file을 저장하는 logic을 설명하였다. 이번에 간단한 script를 통해 해당 파일을 찾고 이를 삭제, 변경하는 방법에 대해 설명하고자 합니다. get-cache-md5 스크립트 작성해 해당 파일을 쉽게 찾을 수 있는 shell script 생성 #!/bin/bash cache_key=$1 md5=$(echo -n "$cache_key" | md5sum | cut -f1 -d' ') p1=$(echo "${md5:0-1:1}") p2=$(echo "${md5:0-3:2}") file_path="/var/cache/nginx/$p1/$p2/$md5" # echo "$file_pa..

nginx 2023.06.14

nginx cache key설정

nginx 를 proxy로 설정할 때 아래는 nginx에서 캐시를 설정하는 예제입니다. http { proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_zone:10m inactive=60m; server { listen 80; server_name my; location / { proxy_cache my_zone; # 위에서 지정한 keys_zone 이름을 지정합니다. proxy_cache_key $host$uri$is_args$args; # cache key설정 proxy_pass http://org; # 요청을 전달할 백엔드 서버의 주소를 지정합니다. } } } proxy_cache_path는 캐시 파일이 저장될 디렉토리 경로를 지정합니다. 이 설..

nginx 2023.06.11

nginx (web server, proxy, api gateway...)

Nginx란? Nginx는 웹 서버 소프트웨어로, Apache 웹 서버와 함께 가장 많이 사용되는 웹 서버 중 하나입니다. Nginx는 높은 성능과 안정성, 확장성 등의 장점으로 인해 많은 사용자들에게 인기가 있습니다. Nginx 설치하기 Nginx를 설치하기 위해서는 우선 해당 운영체제의 패키지 매니저를 이용하거나, 공식 홈페이지에서 소스 코드를 다운로드하여 컴파일하는 방법이 있습니다. 이번에는 Ubuntu를 기준으로 패키지 매니저를 이용한 설치 방법을 소개해드리겠습니다. 1. Nginx 패키지를 설치합니다. sudo apt-get update​ sudo apt-get install nginx 2. 설치가 완료되면 Nginx 서비스를 시작하고, 정상 동작하는지 확인합니다. sudo systemctl s..

nginx 2023.06.11
반응형