Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

proxying 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 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!

like image 330
ragebiswas Avatar asked Dec 02 '14 05:12

ragebiswas


People also ask

Can I use Nginx as a forward proxy?

By using the nginx forward proxy we can masking the location and IP for gaining access to services. Nginx forward proxy will continuing the request on behalf of the client. At the time when the host server will accept the request then only we can see the IP of the nginx proxy server.

Why use Nginx reverse proxy?

The benefits of using Nginx as a reverse proxy include: Clients access all backend resources through a single web address. The reverse proxy can serve static content, which reduces the load on application servers such as Express, Tomcat or WebSphere.

What is proxy pass in Nginx?

proxy_pass – This directive specifies the proxied server's address. The configuration below tells NGINX to pass every request to the proxied application on http://127.0.0.1:8000.

What is location in Nginx?

The location directive within NGINX server block allows to route request to correct location within the file system. The directive is used to tell NGINX where to look for a resource by including files and folders while matching a location block against an URL.


1 Answers

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 
like image 192
danielgpm Avatar answered Sep 18 '22 18:09

danielgpm