Reputation: 235
I have a nginx.conf which basically looks like (unnecessary parts omitted):
upstream app {
server unix:/tmp/unicorn.myapp.sock fail_timeout=0;
}
server {
listen 80;
location @app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app;
}
}
I want to configure nginx so that the value of a specific header is used to rewrite the url being passed to the upstream.
For example, let's assume that I have a request to /test
with the header Accept: application/vnd.demo.v1+json
. I'd like it to be redirected to the upstream URL /v1/test
, i.e. basically the upstream app will receive the request /v1/test
without the header.
Similarly, the request to /test
and the header Accept: application/vnd.demo.v2+json
should be redirected to the upstream URL /v2/test
.
Is this feasible? I've looked into the IfIsEvil nginx module, but the many warnings in there made me hesitant to use it.
Thanks,
r.
edit
In case there's no match, I'd like to return a 412 Precondition Failed
immediately from nginx.
Upvotes: 0
Views: 8232
Reputation: 14372
If Accept
header does not contain required header return error.
map $http_accept $version {
default "";
"~application/vnd\.demo\.v2\+json" "v2";
"~application/vnd\.demo\.v1\+json" "v1";
}
server {
location @app {
if ($version = "") {
return 412;
}
...;
proxy_pass http://app/$version$uri$is_args$args;
}
}
Upvotes: 5