# Problem with rewrite or internal redirection cycle

**URL:** <https://www.ruby-forum.com/t/problem-with-rewrite-or-internal-redirection-cycle/221531>\
**Category:** NGINX\
**Created:** [July 18, 2012, 5:56pm UTC](https://www.ruby-forum.com/t/problem-with-rewrite-or-internal-redirection-cycle/221531 "2012-07-18T17:56:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [July 18, 2012, 5:56pm UTC](https://www.ruby-forum.com/t/problem-with-rewrite-or-internal-redirection-cycle/221531/1 "2012-07-18T17:56:12Z")

</div>

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:

> **[Problem with rewrite or internal redirection cycle](https://forum.nginx.org/read.php?2,228698,228698#msg-228698)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![drakerc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@drakerc](https://www.ruby-forum.com/u/drakerc)\
**Post date:** [July 19, 2012, 4:28pm UTC](https://www.ruby-forum.com/t/problem-with-rewrite-or-internal-redirection-cycle/221531/2 "2012-07-19T16:28:51Z")

</div>

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.
