22

I have a rewrite in my ngix conf file that works properly except it seems to include the location block as part of the $uri variable. I only want the path after the location block. My current config code is:

location /cargo {
    try_files $uri $uri/ /cargo/index.php?_REWRITE_COMMAND=$uri&args;
}

Using an example url of http://localhost/cargo/testpage the redirect works, however the value of the "_REWRITE_COMMAND" parameter received by my php file is "/cargo/testpage". I need to strip off the location block and just have "testpage" as the $uri

I am pretty sure there is a regex syntax to split the $uri and assign it to a new variable using $1 $2 etc, but I can't find any example to do just a variable assignment using a regex that is not part of a rewrite statement. I've been looking and trying for hours and I just can't seem to get past this last step.

I also know I could just strip this out on the application code, but the reason I want to try to fix it in the nginx conf is for compatibility reasons as it also runs on Apache. I also should say that I have figured out a really hacky way to do it, but it involves an "if" statement to check for file existance and the documentation specifically says not to do it that way.

Cimbali
  • 183
  • 8
Jason
  • 323
  • 1
  • 2
  • 7

3 Answers3

24

Looking around I would guess that using a regexp location with captures is the easiest. Adapting your example I end up with:

location ~ ^/cargo(.*) {
    try_files $1 $1/ /cargo/index.php?_REWRITE_COMMAND=$1&args;
}
Theuni
  • 958
  • 5
  • 15
  • aha, so simple once somebody points it out to you! I had to make some minor revisions to handle the root url rewriting to the correct location. I put the answer up in the question since I can't do any formatting in this comment. The only caveat left is that the php block now has to be above this location block or else an infinite redirect loop is created (due to /cargo being a regex and the redirect url also contains /cargo). not sure if there's another way to prevent that from happening? – Jason Dec 06 '12 at 22:49
  • I recommend reading up on the ordering of regexp and non-regexp locations. I usually turn to making all locations regexp at some point because then the ordering is determined by the order in the config file instead of the specificity of the match. – Theuni Dec 07 '12 at 13:07
  • cool thanks, i was imagining what would make the regex above better would be if it matches /cargo/(anything except index.php) then that would prevent server error due to infinite rewrite loop. – Jason Dec 07 '12 at 20:41
7

I found another thing that worked for me (as I'm using gunicorn, I can't choose what to pass)

You should be able to get away with

location /cargo {
    rewrite ^/cargo(.*)$ $1 break;
    try_files $uri $uri/ /cargo/index.php?_REWRITE_COMMAND=$uri&args;
}
g3rv4
  • 171
  • 1
  • 3
4

For those who might be struggling to add it for micro service or API with Node JS I used the following to remove api from the url on my server:

location ^~ /api {
        rewrite ^/api(/.*)$ $1 break;
        proxy_pass    http://127.0.0.1:3001/;
    }
George Mylonas
  • 201
  • 1
  • 2
  • 5