Location directive in nginx for root only, nested in /

I have a proxy_pass working on / and want certain config only apply to root.

I've tried

location / {
   proxy_pass http://foo.com/
}

location = / {
   subs_filter 'foo' 'bar';
}

But i'm getting a 404 from foo.com - how would i make the subs_filter only apply to the root?


Solution 1:

To nginx, only "the" one matching location block is used; in the question this means that for the url / there is no proxy_pass - it'll treat the request as a request for a file in the (default if not configured) root.

There is an example in the documentation very similar to the question:

location /user/ {
    proxy_pass http://user.example.com;
}

location = /user {
    proxy_pass http://login.example.com;
}

Applying that to the question the only thing missing is to add proxy_pass to the = / location block, i.e.

location / {
   proxy_pass http://foo.com/
}

location = / {
   proxy_pass http://foo.com/
   subs_filter 'foo' 'bar';
}