Nginx location exact match matches beyond arguement
In my nginx config, I would like to catch /foo-bar/
and do a rewrite using location, but for it to ignore /foo-bar/anything-else
.
I've tried:
location = /foo-bar/ {
and
location ~ ^/foo-bar/$ {
and
location ~ /foo-bar/ {
but it also catches and rewrites
/foo-bar/?t=aaa-bbb-ccc
How do I catch only
/foo-bar/
and not
/foo-bar/?t=aaa-bbb-ccc
?
Thanks.
As per comment thread in cnst answer, I used:
if ($request_uri = "/foo-bar/") { rewrite ^ … permanent; }
and I never used
/location/
location = /foo/ {
rewrite ^ … redirect;
}
location ~ /foo/ {
return 404;
}
If this doesn't work, it must be the infamous permanent caching of the 301 Moved Permanently
responses — try your request with curl -v
to make sure it works or doesn't work, and/or clear the whole cache of your Gecko or other browser (in Gecko there is no other way around it (other than clearing the whole cache) — the Shift-refresh trick does not apply to 301
).
Update: if you also need to ensure that no args are accepted (e.g. /foo/?test), then use the following:
location = /foo/ {
if ($args) { return 403; }
rewrite ^ … redirect;
}