# Proper location and rewrite for finding target at any depth

**URL:** <https://www.ruby-forum.com/t/proper-location-and-rewrite-for-finding-target-at-any-depth/189079>\
**Category:** NGINX\
**Created:** [May 24, 2010, 5:09am UTC](https://www.ruby-forum.com/t/proper-location-and-rewrite-for-finding-target-at-any-depth/189079 "2010-05-24T05:09:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [May 24, 2010, 5:09am UTC](https://www.ruby-forum.com/t/proper-location-and-rewrite-for-finding-target-at-any-depth/189079/1 "2010-05-24T05:09:08Z")

</div>

I thought I’d branch this away from the “nginx 0day exploit for nginx +  
fastcgi PHP” so as not to fog up that issue more. As you may know from  
my contributions to that thread (starting at [Re: nginx 0day exploit for nginx + fastcgi PHP](http://bit.ly/a00UU0)), I  
had problems with cgi.fix-pathinfo=0 breaking extensionless php files  
that Igor had created locations for back in '08.

Since my regular .php files seem to work fine regardless of the  
settings, I thought I’d bite the bullet and create .php versions of my  
extensionless scripts and have nginx rewrite the clean urls to the new  
.php versions, passing the path info on as a get variable. I’d then just  
have my php scripts explode that variable instead of the  
$\_SERVER[PATH\_INFO] one. I’m guessing it’ll make my life (and Igor’s!)  
easier.

I created a phpinfo() test case and put a gazoo.php in the root  
directory:

location ^~ /gazoo {  
rewrite ^/gazoo(/.\*$|$) /gazoo.php?mypath=$1 last;  
root /usr/local/apache/htdocs;  
fastcgi\_pass 127.0.0.1:10004;  
}

I restarted nginx and went to  
[http://www.example.com/gazoo/123/457?page=12](http://www.example.com/gazoo/123/457?page=12) for a full on test.

That seemed to work just fine:  
\_REQUEST[“mypath”] /123/457  
\_REQUEST[“page”] 12  
\_GET[“mypath”] /123/457  
\_GET[“page”] 12  
\_SERVER[“QUERY\_STRING”] mypath=/123/457&page=12

A couple of quick questions:

1. Are there any issues I should be aware of with my location or regex?  
I usually preface my comments with “I suck at regex” so I’m proud I made  
it htis far. Just want to make sure there are no problems.

2. Most of my extensionless files exist in the root but a couple (e.g.  
photos) can be found at any depth. Since I’ll have photos.php dotted  
over a few different subdirs, what do I need to change to my location  
and regex to make sure that:

[example.com/nginx-luncheon/2010/photos/12](http://example.com/nginx-luncheon/2010/photos/12) runs at  
[example.com/nginx-luncheon/2010/photos.php](http://example.com/nginx-luncheon/2010/photos.php) and  
[example.com/regexawards/photos/15](http://example.com/regexawards/photos/15) runs at  
[example.com/regexawards/photos.php](http://example.com/regexawards/photos.php)?

Thanks all!
