Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Assuming a location block needs to handle URLs like

  • /test_api/group_1/version (several URLs fall in this group. For example, /test_api/aaa/version, /test_api/bbb/version)
  • /test_api/group_2/revision (several other URLs fall in this group. For example, /test_api/yyy/revision, /test_api/zzz/revision)

How can I selectively rewrite "/test_api/group_1/version" while passing "/test_api/group_2/revision" to a default server within a location block? If that's not possible, are there other ways to achieve the same goal. Below is my current non-discriminating location block.

location /test_api {
    types    { application/json json; }
    root     /usr/local/www/data;
    rewrite  "/test_api/([^/]+)/version(.*)/?$"   /api_response/test_api_$1.json   break;
share|improve this question
Might be better addressed in SuperUser SE – mdpc Dec 20 '12 at 17:10

when you make two locations like

location /test_api/group_1/version {

location /test_api {

the LONGEST one will always match, so yours group_2 will not match 1st rule

share|improve this answer
Thanks for the suggestion. I have many URLs that fall in each group so I can't code it like that. I've updated the question to make it more clear. – ytw Dec 20 '12 at 18:09

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.