Created
August 9, 2024 08:44
-
-
Save seanstrom/2fe1f195d0918822985b5f551bf8718f to your computer and use it in GitHub Desktop.
My Nginx Conf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
worker_processes 1; | |
events { | |
worker_connections 1024; | |
} | |
http { | |
# Lua package path and C modules directory | |
lua_package_path "/usr/local/share/lua/5.1/?.lua;;"; | |
lua_package_cpath "/usr/local/lib/lua/5.1/?.so;;"; | |
proxy_cache_path /tmp/nginx-cache-infura levels=1:2 keys_zone=cache-infura:10m inactive=24h max_size=1g; | |
server { | |
listen 8080; | |
location /mock-api { | |
rewrite_by_lua_block { | |
ngx.req.read_body() | |
local json = require "cjson.safe" | |
-- Extract request body data | |
local body = ngx.req.get_body_data() | |
if not body then | |
ngx.exit(ngx.HTTP_BAD_REQUEST) | |
end | |
-- Decode request body json | |
local data = json.decode(body) | |
if not data then | |
ngx.exit(ngx.HTTP_BAD_REQUEST) | |
end | |
-- Register methods that we want to cache | |
local special_methods = { | |
["eth_getBlockByNumber"] = true, | |
} | |
-- Register params that we want to cache differently for a method | |
local special_params = { | |
["eth_getBlockByNumber"] = { | |
["safe"] = true, | |
["latest"] = true, | |
["pending"] = true, | |
["earliest"] = true, | |
["finalized"] = true, | |
} | |
} | |
-- Check if the RPC method is meant to be cached | |
if data and data.method and data.params and special_methods[data.method] then | |
local method = data.method | |
local block_param = data.params[1] | |
-- Determine if we want to cache differently for methods with special parameters | |
-- Reference: https://docs.infura.io/api/networks/ethereum/json-rpc-methods/eth_getblockbynumber | |
if block_param and special_params[method] and special_params[method][block_param] then | |
ngx.exec("@cached-10s") | |
else | |
ngx.exec("@cached-1h") | |
end | |
else | |
ngx.exec("@cached-default") | |
end | |
} | |
} | |
location @cached-1h { | |
set $path "http://node_service:5000/mock-api/token"; | |
proxy_cache cache-infura; | |
proxy_pass $path; | |
proxy_cache_methods POST; | |
proxy_cache_key "$scheme$proxy_host$request_uri|$request_body"; | |
proxy_cache_valid 200 1h; | |
add_header X-Cache-Status $upstream_cache_status always; | |
} | |
location @cached-10s { | |
set $path "http://node_service:5000/mock-api/token"; | |
proxy_cache cache-infura; | |
proxy_pass $path; | |
proxy_cache_methods POST; | |
proxy_cache_key "$scheme$proxy_host$request_uri|$request_body"; | |
proxy_cache_valid 200 10s; | |
add_header X-Cache-Status $upstream_cache_status always; | |
} | |
location @cached-default { | |
proxy_pass http://node_service:5000; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment