Redirect based on header value

Let assume that server called n1 is reverse proxy (SSL) for http server
s1. Client c1 connects to n1 using given URL https://n1/index.html and
in the end reads web page from /var/www/index.html on s1. What I want to
do is to change URL sent to s1 depending on some header value sent by
client. Namely, c1 connects to n1 with URL https://n1/index.html and
with header “some_header=123” then n1 checks in some configuration file
what to do if some_header has the value of 123 and changes the URL
http://s1/123/index.html (as an example). I did the same thing on
Apache using *.map files and RewriteRule how I can do the same thing in
nginx ?

Posted at Nginx Forum:

Maybe something like this…
(config below assumes your header is “My-Header” and must have an
integer value to be recognized)

server {
# …

set $path_prefix '';
if ($http_my_header ~ (\d+)) {
    set $path_prefix /$1;
}

location / {
    # proxy_set_header calls here
    proxy_pass s1_server$path_prefix;
}

}

I know “if” is evil, but I don’t know that there’s any other way to
accomplish a conditional like this.

Nick

On Thu, Nov 19, 2009 at 04:01:44AM -0500, zestriddle wrote:

Thanks :), but I still need one more thing to make it usable. This comparison $http_my_header ~ (\d+) must work on concrete values from file, namely

In file called header_value.file I would like to have

header_value1
header_value2
header_value3

and comparison $http_my_header ~ (\d+) should check whether any value from header_value.file is in $http_my_header

http {

map $http_my_header  $prefix  {
     default  "";
     123      /123;
     abc      /abc;
     ...
};

server {
    location / {
        proxy_pass  http://s1$prefix$request_uri;
    }

Thanks :), but I still need one more thing to make it usable. This
comparison $http_my_header ~ (\d+) must work on concrete values from
file, namely

In file called header_value.file I would like to have

header_value1
header_value2
header_value3

and comparison $http_my_header ~ (\d+) should check whether any value
from header_value.file is in $http_my_header

Posted at Nginx Forum: