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 want a simple redirect. If the REQUEST_URI is "/", i want redirect to "/blog/". After reading some Pitfall-Docs and BestPractices, i guess try_files is for what i search. But the nginx wiki doesnt clarify to me, how it works exactly. There are two (or one) path(s) and an uri? uri seems to be the REQUEST_URI which is what iam looking for. But i dont understand, how path1 (and path2) interacts with it. What is when evaluated. Can you help me to get the point?

P.S.: nginx version 1.0(.8)

share|improve this question

2 Answers 2

There are two (or one) path(s) and an uri? uri seems to be the REQUEST_URI

No. It is the fallback URI. try_files path1 [path2] uri check if path1, path2 is exist. If it not, Nginx will redirect to the uri.

For example: try_files $uri $uri/ /index.php; is equivalent to:

if (!-e $request_filename) {
   rewrite ^ /index.php last;
}
share|improve this answer
1  
There's one small difference between your rewrite and the try_files. The rewrite will preserve the query string by default, and the try_files fallback will not. You either need to add $is_args$args to the try_files /index.php or add ? to the rewrite /index.php to make them truly equivalent. –  kolbyjack Oct 21 '11 at 17:43
    
So, path1 and path2 are checked against the filesystem? and if these files not found, they assumed the last argument as uri to use?? –  breiti Oct 22 '11 at 9:18

If you want a redirect, then try_files isn't what you want. Try this:

rewrite ^/$ /blog/ redirect;
share|improve this answer
    
Okay - thanks a lot. But can you still clarify try_files? :) –  breiti Oct 21 '11 at 16:33
    
Sure - basically, you give it a list of locations, and it can be as long as you want. It will try to serve the request from each of those locations, in order, until it finds one that works. –  Shane Madden Oct 21 '11 at 16:53
2  
It's better to use location = / { rewrite ^ /blog/ redirect; } or location = / { return 302 $scheme://$host/blog/; } than a server-level rewrite. Also, saying that try_files is a list of locations is misleading, as it will only perform an internal redirect for the last argument, all the arguments before the last are literally checks for files on the filesystem, and if one is found, $uri is set appropriately before calling the location's content handler. –  kolbyjack Oct 21 '11 at 17:46
    
@kolbyjack then i get an too-many-redirect error (i tried the 2nd example)). location / will always get evaluated and initiate the redirect. Regardless if location /blog is before, after or inside location /. :/ what am i doing wrong? –  breiti Oct 22 '11 at 9:17
1  
Did you include the =? location / and location = / mean very different things. –  kolbyjack Oct 22 '11 at 13:13

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.