Problem with rewrite or internal redirection cycle

Hello

I have recently moved my website from Litespeed to nginx. I had to
change some lines in my PHP script to use x-sendfile function and
because of it I also had to change my nginx config. Unfortunately, when
I’m executing my PHP script, I receive 500 Internal Server Error. My
error.log says:

2012/07/18 17:39:52 [error] 23510#0: *20011855 rewrite or internal
redirection cycle while internal redirect to “/index.php”, client:
31.175.62.69, server: modbase.pl, request: "GET […]

I’m not sure what’s causing this problem and how to fix it and I really
need your help.

My nginx website’s config:
server {
listen 80;
server_name modbase.pl;
server_name_in_redirect off;
client_max_body_size 1024m;
access_log /home/log/nginx/localhost.access_log;
error_log /home/log/nginx/localhost.error_log;
send_timeout 180;
root /home/modbase.pl/public_html;
index index.php;
# Support Clean (aka Search Engine Friendly) URLs
location / {
client_max_body_size 1024m;
try_files $uri $uri/ /index.php?q=$uri&$args;
proxy_read_timeout 120;
send_timeout 180;
autoindex on;
}

    index index.php index.html index.htm default.html default.htm;
    # deny running scripts inside writable directories
    location ~*

/(images|cache|media|logs|tmp)/.*.(php|pl|py|jsp|asp|sh|cgi)$ {
return 403;
#error_page 403 /403_error.html;
}

    location ~ .*.php$ {
        include /etc/nginx/fastcgi_params;

fastcgi_connect_timeout 320;

fastcgi_read_timeout 320;
client_max_body_size 1024m;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
send_timeout 180;
fastcgi_param SCRIPT_FILENAME
/home/modbase.pl/public_html$fastcgi_script_name;
}

location /downloads {
internal;
}

    # caching of files
    location ~* \.(ico|pdf|flv)$ {
            expires 1y;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|swf|xml|txt)$ {
            expires 14d;
    }

}

My PHP script snippet that uses x-sendfile:

$filename_directoo = ‘/’.$cat_dir.‘/’.$file_url;
header(“X-Accel-Redirect:”.$filename_directoo);
header(‘Content-Disposition: attachment; filename="’.$filename.‘"’);

Posted at Nginx Forum:

Hello!

On Wed, Jul 18, 2012 at 11:55:49AM -0400, drakerc wrote:

redirection cycle while internal redirect to “/index.php”, client:
31.175.62.69, server: modbase.pl, request: "GET […]

I’m not sure what’s causing this problem and how to fix it and I really
need your help.

Your config with your script produce an infinite loop. Most likely
it’s due to fact that after X-Accel-Redirect request ends up in
location /, and then redirected to /index.php as file isn’t found,
and then again after X-Accel-Redirect request ends up in location
/ and so on.

    location / {
    client_max_body_size 1024m;
    try_files $uri $uri/ /index.php?q=$uri&$args;

[…]

location /downloads {
internal;
}

[…]

My PHP script snippet that uses x-sendfile:

$filename_directoo = ‘/’.$cat_dir.’/’.$file_url;
header(“X-Accel-Redirect:”.$filename_directoo);
header(‘Content-Disposition: attachment; filename="’.$filename.’"’);

It looks like “location /downloads” was added to avoid the above
loop, but you don’t refer it in X-Accel-Redirect.

Maxim D.