1

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!

2

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;
    }

 }
4
  • Hi Xavier, This does seem to sort of work. However, it just sends requests to upstream1. When I try to request domain.com/test.js?V=asdf or domain.com/test.js?V=1234 it always proxies to upstream1. I guess I should update the regex ? – Radoslav Stefanov Oct 22 '14 at 11:12
  • @RadoslavStefanov There's no reason that test.js?V=1234 would be proxied to upstream1. How do you test it, and what's your full config ? If you have not only digits then yes you will need to update the regex. – Xavier Lucas Oct 22 '14 at 11:22
  • I just applied this config for testing and requested domain.com/bla.js?V=1234 map $arg_v $node { default 192.168.10.1; "~^[0-9]+$" 192.168.10.2; } server { listen 192.168.10.80:80; server_name domain.com; location ~ \.(css|js)$ { proxy_pass node; } } This actually gives me an error: nginx: [emerg] host not found in upstream "node" in /etc/nginx/sites-enabled/test:13 – Radoslav Stefanov Oct 22 '14 at 11:32
  • 1
    @RadoslavStefanov Oh yeah, typo in my post. Use $node not node. – Xavier Lucas Oct 22 '14 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.