Skip to content

Instantly share code, notes, and snippets.

@carlcortright
Created December 10, 2024 22:54
Show Gist options
  • Save carlcortright/716b96f401b85207182c2433f8b9337f to your computer and use it in GitHub Desktop.
Save carlcortright/716b96f401b85207182c2433f8b9337f to your computer and use it in GitHub Desktop.
worker_processes auto;
events {
worker_connections 2048;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Add error logging
error_log /dev/stderr debug;
access_log /dev/stdout;
# Cache settings
proxy_cache_path /tmp/nginx_cache levels=1:2 keys_zone=rpc_cache:10m max_size=10g inactive=60m use_temp_path=off;
# Define allowed origins
map $http_origin $is_allowed_origin {
default 0;
"http://localhost:3000" 1;
"https://alpha.higherrrrrrr.fun" 1;
}
server {
listen 8080;
location / {
# Check if origin is allowed
if ($is_allowed_origin = 0) {
return 403;
}
# Remove CORS headers from upstream
proxy_hide_header 'Access-Control-Allow-Origin';
proxy_hide_header 'Access-Control-Allow-Methods';
proxy_hide_header 'Access-Control-Allow-Headers';
proxy_hide_header 'Access-Control-Allow-Credentials';
proxy_hide_header 'Access-Control-Max-Age';
# Add our CORS headers
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'Content-Type,Authorization' always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Max-Age' '2592000' always;
add_header 'X-Cache-Status' $upstream_cache_status always;
# Handle preflight
if ($request_method = 'OPTIONS') {
return 204;
}
# Proxy settings
proxy_pass <your RPC URL>;
proxy_http_version 1.1;
proxy_set_header Host base-mainnet.g.alchemy.com;
proxy_set_header Content-Type 'application/json';
proxy_set_header Origin '';
proxy_set_header Referer '';
proxy_set_header User-Agent '';
# Cache settings
proxy_cache rpc_cache;
proxy_cache_methods POST;
proxy_cache_key "$request_body";
proxy_cache_valid 200 22s;
# Cache control
set $no_eth_call 1;
if ($request_body ~ "eth_call") {
set $no_eth_call 0;
}
proxy_cache_bypass $http_cache_bypass;
proxy_no_cache $no_eth_call;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment