Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

--> UPDATE: ANSWERED BY theuni:

The regex goes in the location block definition. one note of caution is that php handler location needs to be ABOVE this location, otherwise you will get a server error because it goes into an infinite redirect loop

location ~ ^/cargo/(.*) {
    try_files /cargo/$1 /cargo/$1/ /cargo/index.php?_REWRITE_COMMAND=$1&args;
}
share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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;
}
share|improve this answer
    
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 6 '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 7 '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 7 '12 at 20:41
add comment

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;
}
share|improve this answer
add comment

Your Answer

 
discard

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.