Clean-URL rewrite rule with nested "location" and alias directive

Francis Daly francis at
Sat Nov 23 20:47:33 UTC 2013

On Sat, Nov 23, 2013 at 01:11:46PM -0500, Ben Johnson wrote:
> On 11/23/2013 12:36 PM, Ben Johnson wrote:

Hi there,

> > It's bizarre. At some point while meddling with the configuration,
> > requests for /stage/ began causing the browser to download index.php
> > (and I can open the file and see the PHP code). And no matter what I
> > change in the configuration, this behavior persists.

I tend to test with "curl", because it avoids any chance of a browser
cache being involved.

Any intermediate proxies or caches might still be in the way, of
course. But the nginx access log and/or debug log should show what nginx
thinks is happening.

> > I went so far as to rename the "stage" directory to "staging", and
> > changed all references in nginx's configuration accordingly. Yet, for
> > some reason, requests for /stage/ still download the index.php file! I'm
> > not even sure where this index.php file is coming from, given that nginx
> > should have absolutely no knowledge of this file's new/current location.

The usual reason for this kind of thing is that the nginx.conf that you
are writing and the nginx.conf that nginx is reading are not the same.

> > The weirdest part is that the downloaded file is *not* the file that
> > exists at /index.php (the "main site" index file). I confirmed this by
> > adding some commented PHP code at the bottom of /index.php, and the
> > comments do not appear in the downloaded file.

The other likely reason is that the web server you are talking to and
the web server you think you are talking to are not the same.

(Either a different machine entirely, or else a different server{}
block in the same config file.)

> > Hell, I even tried deleting the entire "staging" directory and this
> > still happens! I've restarted nginx, php5-fpm, etc. and nothing changes.

Possibly you did a "nginx -s reload", but an error in the conf file
meant that nginx didn't stop using the older file, or something like that?

> I removed everything related to this effort (setting-up the staging
> site) from nginx's configuration, and deleted all of the staging site
> files from the filesystem, yet nginx is still serving an "index.php"
> file whenever I request /stage/! How is this possible? This "index.php"
> is indeed "mine"; it contains the code from my application. But, as I
> said, if I add some random, commented PHP code to the bottom of the
> "real index file" at /index.php, it's not present in the downloaded
> file. I just don't see where this file could be coming from at this point.

Add something like

  location = /stage/ { return 200 "Just checking...\n"; }

and restart nginx. If you don't see that response for that request,
you're not using that conf file.

If that much does work, then either read the debug log, or trace through
nginx.conf to see what should happen for this request -- server-level
rewrite module directives, plus the one location that the request
"/stage/" should be handled in, are the most likely sources.

> Thanks for any insight as to what could be happening here...

Make sure nginx is really completely stopped.

Then start it again with a known config file.

And look at the responses to a few requests, and see which are not what
you expect.

I'm afraid I'm reduced to generalities with the information available.

Good luck with it,

Francis Daly        francis at

More information about the nginx mailing list