# Nginx rewrite to PHP

**URL:** <https://www.ruby-forum.com/t/nginx-rewrite-to-php/219151>\
**Category:** NGINX\
**Created:** [April 22, 2012, 4:13am UTC](https://www.ruby-forum.com/t/nginx-rewrite-to-php/219151 "2012-04-22T04:13:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [April 22, 2012, 4:13am UTC](https://www.ruby-forum.com/t/nginx-rewrite-to-php/219151/1 "2012-04-22T04:13:02Z")

</div>

Hi,

I’m using Nginx linked to PHP using fastcgi proxy (to the FPM module of  
PHP  
core btw).

I am trying to use some rewrite rules, which apparently work since I get  
redirected (HTTP answer 200), but the content is not passed through the  
PHP  
handler…  
I am returned the file for downloading which content is the raw PHP  
code!

I am using a configuration like the following:

location / {  
rewrite ^/dunno/([^.^/]+)$ /index.php?q=$1 break;  
return 404;  
}

location ~ .php$ {  
// PHP handling there  
}

## What am I doing wrong?

_B. R._

---

<div class="post-metadata">

**Author:** ![B.R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3be4f8/32.png) [@B.R](https://www.ruby-forum.com/u/B.R)\
**Post date:** [April 22, 2012, 4:18am UTC](https://www.ruby-forum.com/t/nginx-rewrite-to-php/219151/2 "2012-04-22T04:18:38Z")

</div>

2012/4/22 B.R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I am using a configuration like the following:
> 
> location / {  
> rewrite ^/dunno/([^.^/]+)$ /index.php?q=$1 break;  
> return 404;  
> }

break means the file is handled by current location block (location /  
{ }). The one  
you want is last.

reference: [Module ngx\_http\_rewrite\_module](http://nginx.org/r/rewrite)

---

<div class="post-metadata">

**Author:** ![B.R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3be4f8/32.png) [@B.R](https://www.ruby-forum.com/u/B.R)\
**Post date:** [April 22, 2012, 4:24am UTC](https://www.ruby-forum.com/t/nginx-rewrite-to-php/219151/3 "2012-04-22T04:24:52Z")

</div>

Thanks Edho,

I also use the official reference first-hand ;o)  
From the example given on the Wiki page of the Rewrite module you  
quoted[http://wiki.nginx.org/HttpRewriteModule](http://wiki.nginx.org/HttpRewriteModule),  
they replace ‘last’ by ‘break’ when rewrite rules put inside a location  
block…

## Isn’t it a problem to put ‘last’ flags inside the location block? What is the 10-cycles-then-500-error referring to?

_B. R._

---

<div class="post-metadata">

**Author:** ![B.R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3be4f8/32.png) [@B.R](https://www.ruby-forum.com/u/B.R)\
**Post date:** [April 22, 2012, 4:33am UTC](https://www.ruby-forum.com/t/nginx-rewrite-to-php/219151/4 "2012-04-22T04:33:32Z")

</div>

2012/4/22 B.R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Thanks Edho,
> 
> I also use the official reference first-hand ;o)  
> From the example given on the Wiki page of the Rewrite module you quoted,  
> they replace ‘last’ by ‘break’ when rewrite rules put inside a location  
> block…
> 
> Isn’t it a problem to put ‘last’ flags inside the location block? What is  
> the 10-cycles-then-500-error referring to?

The 500 error is cause by the rewrite: it rewrites /download/ (clean  
url) to /download/ (actual file). If it use last flag, the /download/  
location block will be searched again (and in fact it may return 403  
instead of 500 if the rewritten url doesn’t match the rewrite rule  
anymore - it’ll return 500 only on certain cases).

The one you want is for it to be handled by different location block  
(.php$) - hence last flag.

---

<div class="post-metadata">

**Author:** ![B.R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3be4f8/32.png) [@B.R](https://www.ruby-forum.com/u/B.R)\
**Post date:** [April 22, 2012, 4:39am UTC](https://www.ruby-forum.com/t/nginx-rewrite-to-php/219151/5 "2012-04-22T04:39:46Z")

</div>

Well, I didn’t take the required time to understand these examples…  
Those are definitely hopefully not to be seen in real config files! ;o)

Thank you for having taken time to explain all that to me.  
I just made the change and my requests end up at the right places.

## Thanks again!

_B. R._
