Josh Josh - 2 months ago 18
Apache Configuration Question

Redirecting all API routes using nginx

I'm in the process of switching from Apache to Nginx and have the following code in my Apache VirtualHost:

RedirectMatch 307 ^/api/v1(/.*|$) http://legacy.website.com/api/v1$1


I would like to redirect API calls to our old client for the time being. Would this be an acceptable way of accomplishing this using Nginx?

location /api/v1 {
return 307 http://legacy.website.com/api/v1$1;
}

Answer

Your example does not capture a value for $1. You could change the location block from a prefix type to a regular expression type, but note that its evaluation order will also change. See this document for details.

For example:

location ~ ^/api/v1(/.*|$) {
    return 307 http://legacy.website.com/api/v1$1;
}
Comments