nginx issue with following all redirects

VSack asked:

I have two redirects in a file at /etc/nginx/redirect.conf:

rewrite /this-link redirect;
rewrite /this-link-too redirect;

Attempting to go to or both go to

Is there a way to force exact matching?

Example config below:

server {
listen 80;


index index.html;

error_page 404;

location / {
  try_files $uri @redirect;

location @redirect {
  include /etc/nginx/redirect.conf;

My answer:

The rewrite directive accepts a regular expression, so /this-link-too matches /this-link, and thus your first rewrite always gets used. Rewrite should be avoided unless you actually need to match a regular expression or do substitutions.

You should consider using exact location matches with return instead.

For example:

location = /this-link {
    return 302;

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.