proxying relative urls with nginx
nginx, proxy, reverse-proxy, webserver
Solution
Based on your updated comments; if the upstream backend sends the referer header, you could do something like this:
location ~* ^/(css|js)/.+\.(css|js)$ {
#checking if referer is from app1
if ($http_referer ~ "^.*/app1"){
return 417;
}
#checking if referer is from app2
if ($http_referer ~ "^.*/app2"){
return 418;
}
}
error_page 417 /app1$request_uri;
error_page 418 /app2$request_uri;
location /app1 {
proxy_pass http://app1.com;
}
location /app2 {
proxy_pass http://app2.com;
}
For example, if the backend on app2.com, requests the test.css like this:
curl 'http://example.com/css/test.css' -H 'Referer: http://app2.com/app2/some/api'
The request land here:
/app2/css/test.css
Problem
My question is similar to Nginx Relative URL to Absolute Rewrite Rule? - but with an added twist. I have nginx acting as a proxy server, which proxies for multiple apps, similar to this (simplified) config: ``` server { listen 80; server_name example.com; location /app1 { proxy_pass http://app1.com; } location /app2 { proxy_pass http://app2.com; } } ``` This works fine, but as in the other question, these applications (`app1` and `app2`) use relative urls such as `/css/foo.css`, or `/js/bar.js`. Also it's a big problem to ask all applications to change to something like `/app1/css/foo.css`. Is it possible for nginx to intelligently figure out which application should handle the request? FTR, users would be accessing these applications like this: `http://example.com/app1/fooaction` or `http://example.com/app2/baraction`. If it matters, all applications are Java/Tomcat based apps. TIA!