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

I'm using try_files like this:

http {
  server {
    error_log /var/log/nginx debug;
    listen 127.0.0.1:8080;
    location / {
      index off
      default_type  application/octet-stream;
      try_files /files1$uri /files2/$uri /files3$uri;
    }
  }
}

In the error log, it's showing this:

*[error] 15077#0: 45399 rewrite or internal redirection cycle while internally redirecting to "/files1/files2/files3/path/to/my/image.png", client: 127.0.0.1, server: , request: "GET /path/to/my/image.png HTTP/1.1", host: "mydomain.com", referrer: "http://mydomain.com/folder"

Can anyone tell me why nginx is looking for /files1/files2/files3/path/to/my/image.png instead of /files1/path/to/my/image.png, /files2/path/to/my/image.png and /files3/path/to/my/image.png?

Thanks

share|improve this question

3 Answers 3

http://nginx.org/r/try_files

syntax:     try_files file ... uri;
            try_files file ... =code;

The last parameter specifies return code or URI for internal redirect. In your case it's /files3$uri.

Perhaps you actually want this: try_files /files1$uri /files2$uri /files3$uri =404;

share|improve this answer

I suspect that nginx is essentially recursing, because the paths specified in try_files are matched by the location / block, which applies the try_files directive again. Try adding these location blocks to catch the paths which are being searched.

location /files1 {}
location /files2 {}
location /files3 {}
share|improve this answer
    
You're right about a recursion but you're wrong about what caused it. Please, check the docs: nginx.org/r/try_files Checks the existence of files in the specified order, and uses the first found file for request processing; the processing is performed in the current context. –  VBart Jul 8 '12 at 17:12
    
@VBart I'm not following why that means that /files1/files2/files3 is finally prepended? –  mgorven Jul 8 '12 at 17:26
    
0. Assume that $uri is /. 1. Go to location / 2. /files1$uri results not found; 3. /files2/$uri results not found; 4. Redirecting to /files3$uri, because the last parameter in try_files is internal redirect; 5. Go to step 1 with the new $uri. –  VBart Jul 8 '12 at 17:39
    
@VBart Ah, gotcha. –  mgorven Jul 8 '12 at 19:42
up vote -2 down vote accepted

The issue was solved by adding a root directive outside the location block:

http {
  root /;
  location / {
    try_files files1$uri files2$uri files3$uri =404;
  }
}
share|improve this answer
    
No, it was solved because you correctly added the final fallback location. –  Mahmoud Al-Qudsi Aug 16 '13 at 4:30

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.