Skip to content

Instantly share code, notes, and snippets.

@nikola43
Last active April 26, 2025 13:23
Show Gist options
  • Save nikola43/c65abd7b8030b1dabde05d12b27ee4c9 to your computer and use it in GitHub Desktop.
Save nikola43/c65abd7b8030b1dabde05d12b27ee4c9 to your computer and use it in GitHub Desktop.
Subgraph load balancer nginx
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 10s;
# Manual random load balance
map $request_id $backend_host {
default DOMAIN_1;
~*[13579a-f]$ DOMAIN_2; # if last char odd → subgraph2
}
server {
listen 127.0.0.1:80;
server_name loadbalancer.domain.com;
access_log /var/log/nginx/loadbalancer.access.log;
error_log /var/log/nginx/loadbalancer.error.log;
location /health {
return 200 "Load balancer operational\n";
}
location / {
proxy_pass https://$backend_host;
proxy_http_version 1.1;
proxy_ssl_server_name on;
proxy_set_header Host $backend_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /subgraphs/ {
proxy_pass https://$backend_host;
proxy_http_version 1.1;
proxy_ssl_server_name on;
proxy_set_header Host $backend_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /node {
proxy_pass https://$backend_host/node;
proxy_http_version 1.1;
proxy_ssl_server_name on;
proxy_set_header Host $backend_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /index-node/graphql {
proxy_pass https://$backend_host/index-node/graphql;
proxy_http_version 1.1;
proxy_ssl_server_name on;
proxy_set_header Host $backend_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /subscriptions {
proxy_pass https://$backend_host/subscriptions;
proxy_http_version 1.1;
proxy_ssl_server_name on;
proxy_set_header Host $backend_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /ipfs/ {
proxy_pass https://$backend_host/ipfs/;
proxy_http_version 1.1;
proxy_ssl_server_name on;
proxy_set_header Host $backend_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_request_buffering off;
proxy_buffering off;
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
send_timeout 600;
client_max_body_size 500M;
}
}
#!/bin/bash
subgraph1=0
subgraph2=0
for i in {1..10}; do
echo "Request $i:"
# Save full headers to a file for inspection
curl -sLI "https://loadbalancer.domain.com/subgraphs/name/exchange" > headers.txt
# Print all headers for debugging
echo "Full headers for request $i:"
cat headers.txt
echo "------------------------------"
# Try multiple header names
for header in "X-Backend-Server" "X-Used-Backend" "X-Proxy-Host"; do
backend=$(grep -i "$header" headers.txt | awk -F': ' '{print $2}' | tr -d '\r')
if [ -n "$backend" ]; then
echo "Found $header: $backend"
if [[ "$backend" == *"subgraph1"* ]]; then
((subgraph1++))
backend_found=1
break
elif [[ "$backend" == *"subgraph2"* ]]; then
((subgraph2++))
backend_found=1
break
fi
fi
done
if [ -z "$backend_found" ]; then
echo "No backend information found in request $i"
fi
echo ""
sleep 1 # Small delay between requests
done
echo "=== Distribution Results ==="
echo "subgraph1: $subgraph1 requests"
echo "subgraph2: $subgraph2 requests"
server {
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name DOMAIN_1;
location /node {
proxy_pass http://127.0.0.1:8020;
}
location /subgraphs {
proxy_pass http://127.0.0.1:8000/subgraphs;
}
location /index-node/graphql {
proxy_pass http://127.0.0.1:8030/graphql;
}
location /subscriptions {
proxy_pass http://127.0.0.1:8001/subgraphs;
}
location /ipfs/ {
proxy_pass http://127.0.0.1:5001/;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# Disable buffering for large file uploads
proxy_request_buffering off;
proxy_buffering off;
# Increase timeouts (in seconds)
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
send_timeout 600;
# Allow very large file uploads (500MB)
client_max_body_size 500M;
}
add_header X-Backend-Server "subgraph1" always; # Add this line
listen 127.0.0.1:80;
}
server {
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name DOMAIN_2;
location /node {
proxy_pass http://127.0.0.1:8020;
}
location /subgraphs {
proxy_pass http://127.0.0.1:8000/subgraphs;
}
location /index-node/graphql {
proxy_pass http://127.0.0.1:8030/graphql;
}
location /subscriptions {
proxy_pass http://127.0.0.1:8001/subgraphs;
}
location /ipfs/ {
proxy_pass http://127.0.0.1:5001/;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# Disable buffering for large file uploads
proxy_request_buffering off;
proxy_buffering off;
# Increase timeouts (in seconds)
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
send_timeout 600;
# Allow very large file uploads (500MB)
client_max_body_size 500M;
}
add_header X-Backend-Server "subgraph2" always; # Add this line
listen 127.0.0.1:80;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment