Q:

How could I redirect nginx request based on the URL

I have 3 app servers which are handled by nginx server.
upstream he {
    server h1.abc.example.com;
    server h2.abc.example.com;
}

How could I redirect requests request based on the some header value.Eg

abc-h1.abc.example.com should go to server h1.abc.example.com
def-h1.abc.example.com should go to server h2.abc.example.com

where -h1.abc.example.com is going be same for all requests

A:

The most efficient way to do this would be with the server_name directive in multiple server blocks.  Example:
server {
    server_name abc-h1.example.com;
    proxy_pass http://h1.example.com$request_uri;
}

server {
    server_name def-h1.example.com;
    proxy_pass http://h2.example.com$request_uri;
}

Obviously if you're not using NGINX as an HTTP proxy then replace proxy_pass with whatever directive is appropriate: fastcgi_pass, uwsgi_pass etc.

