How to choose upstream based on request header

I am trying to send request to different server if a particular header is set
It gives error invalid URL prefix in "http://"
here is my nginx configuration file

    upstream server1 {
        server localhost:5000; 
    }
    
    upstream server2 {
        server 172.31.15.137;
    }
    
    # the nginx server instance
    server {
        listen 80;
        listen [::]:80;
        server_name dev.i2e1.in;
        access_log /var/log/nginx/dev.i2e1.in.log;
    
        # pass the request to the node.js server with the correct headers
        # and much more can be added, see nginx config options
        location / {
        
        proxy_http_version 1.1;
            proxy_set_header   Upgrade $http_upgrade;
            proxy_set_header   Connection keep-alive;
            proxy_set_header   Host $host;
            proxy_cache_bypass $http_upgrade;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Proto $scheme;   
        proxy_pass         http://$http_x_custom_header;
        }
    }

What is missing?


Solution 1:

It looks that there was no X-Custom-Header HTTP header set in the request you made.

Using a header as a proxy_pass destination without any validation is quite insecure.

I would use a map to filter valid endpoints and also enforce a default endpoint:

In http level, add the following map:

map $http_x_custom_header $upstream {
    server1 server1;
    server2 server2;
    default server1;
}

And then use:

proxy_pass http://$upstream;