Nginx proxy_pass based on file extension and query string

luanginxquerystring

I need to proxy_pass static assets (.js .css) based on file extension and query strings.

For example:

domain.com/foo.css – go to upstream1
domain.com/foo.css?V=1234 – go to upstream2

Reason is I have a 3 server setup – a router, an application server and a static server. I would like any request with clean url domain.com/foo.cs to go to upstream1 (where my static server is configured). And any request that has query string url domain.com/foo.css?V=1234 to go to upstream2 (where my application server is configured).

Maybe it can be done using http://wiki.nginx.org/HttpLuaModule?

Thanks!

Best Answer

Use a map.

map $arg_v $node {
    default        upstream1;
    "~^[0-9]+$"    upstream2;
}


server {

    listen 80;
    server_name domain.com;

    location ~ \.(css|js)$ {
        proxy_pass http://$node;
    }

 }
Related Topic