Proxy relative URLs with nginx

My question is similar to Nginx Relative URL to Absolute Rewrite Rule? - but with an added twist.

I have nginx acting as a proxy, which is a proxy for several applications, similar to this (simplified) configuration:

server { listen 80; server_name example.com; location /app1 { proxy_pass http://app1.com; } location /app2 { proxy_pass http://app2.com; } } 

This works great, but as in the other question, these applications ( app1 and app2 ) use relative URLs like /css/foo.css or /js/bar.js . There is also a big problem to ask all applications to change something like /app1/css/foo.css .

Is it possible for nginx to reasonably determine which application should process the request? FTR, users will access these applications as follows:

http://example.com/app1/fooaction or http://example.com/app2/baraction .

If that matters, all applications are Java / Tomcat-based applications.

TIA!

+5
source share
1 answer

Based on your updated comments; if the backstream server sends a referent header , you can 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 test.css as follows:

 curl 'http://example.com/css/test.css' -H 'Referer: http://app2.com/app2/some/api' 

Request here:

 /app2/css/test.css 
+7
source

Source: https://habr.com/ru/post/1208101/


All Articles