Skip to content

Commit

Permalink
CON-303 Cache CN 200-status /health_check responses for 1sec in nginx (
Browse files Browse the repository at this point in the history
  • Loading branch information
SidSethi authored Aug 3, 2022
1 parent b33a56c commit 798b7fa
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions creator-node/nginx_conf/nginx.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# nginx Configuration file
#
# Notes:
# - any value below prefixed with $arg indicates a request query param, e.g. $arg_bypasscache will match query param `bypasscache=<value>`

worker_processes 1;

events {
Expand All @@ -19,7 +24,7 @@ http {
# If inactive period passes, content WILL BE DELETED from the cache by the cache
# manager, regardless whether or not it has expired.
# https://www.nginx.com/blog/nginx-caching-guide#How-to-Set-Up-and-Configure-Basic-Caching
proxy_cache_path /usr/local/openresty/cache levels=1:2 keys_zone=cidcache:1000m
proxy_cache_path /usr/local/openresty/cache levels=1:2 keys_zone=cache:1000m
max_size=4g inactive=12h use_temp_path=off;
proxy_read_timeout 3600; # 1 hour in seconds

Expand All @@ -30,10 +35,10 @@ http {
# If present in cache, serve. Else, hit upstream server + update cache + serve.
# http://nginx.org/en/docs/http/ngx_http_core_module.html#location
location ~ (/ipfs/|/content/) {
proxy_cache cidcache;
proxy_cache cache;
proxy_pass http://127.0.0.1:3000;

# Set client IP
# Set client IP as `X-Forwarded-For` response header
proxy_set_header X-Forwarded-For $remote_addr;

# proxy_cache_use_stale + proxy_cache_background_update -> deliver stale content when client requests
Expand Down Expand Up @@ -63,10 +68,30 @@ http {
add_header X-Cache-Status $upstream_cache_status always;
}

location = /health_check {
proxy_cache cache;

proxy_pass http://127.0.0.1:3000;

# Set client IP as `X-Forwarded-For` response header
proxy_set_header X-Forwarded-For $remote_addr;

# Never serve a stale response

# Cache responses with 200 status, for 1 second
proxy_cache_valid 200 1s;

# Bypass cache with bypasscache=true query string and save new response to proxy cache
proxy_cache_bypass $arg_bypasscache;

# Add response header to indicate the status of the cache with this request
add_header X-Cache-Status $upstream_cache_status always;
}

# Pass all other requests to upstream server
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
}

0 comments on commit 798b7fa

Please sign in to comment.