Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NGINX API Versioning Techniques

I am looking to use NGINX to handle API versioning. I thought it would be as simple as this to handle sending the traffic to different URLS:

    location = /1.0/* {
        root = /var/www/html/version_1.0/public;
    }
    location = /1.1/* {
        root = /var/www/html/version_1.1/public;
    }

I would then write some form of rewrite to strip out the 1.0/ or 1.1/ from the URL. Is that correct? Anyways, the location method doesn't work. Is my syntax off?

Thanks!

like image 963
Du3 Avatar asked Jun 17 '26 14:06

Du3


1 Answers

Make sure this is not about matching order:

nginx first searches for the most specific location given by literal strings regardless of the listed order. In the configuration above the only literal location is “/” and since it matches any request it will be used as a last resort.
Then nginx checks locations given by regular expression in the order listed in the configuration file. The first matching expression stops the search and nginx will use this location.
If no regular expression matches a request, then nginx uses the most specific literal location found earlier.

You can try a location directive, testing for the literal you want, and preventing any regex to be checked:

location ^~ /1.0/ {
  # matches any query beginning with /1.0/ and halts searching,
  # so regular expressions will not be checked.
  [ configuration C ] 
}

Then you can check rewrite procedures.

like image 78
VonC Avatar answered Jun 20 '26 05:06

VonC