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

Francis Daly francis at daoine.org
Tue Nov 19 20:39:16 UTC 2013


On Tue, Nov 19, 2013 at 01:45:15PM -0500, Ben Johnson wrote:
> On 11/19/2013 12:38 PM, Francis Daly wrote:
> > On Tue, Nov 19, 2013 at 10:36:53AM -0500, Ben Johnson wrote:

Hi there,

> >> This works as expected, with one major exception: "clean-URL" rewriting
> >> does not work. In other words, the homepage (/stage/) loads correctly,
> >> but all sub-pages return a 404.

Which nginx version do you use, where you get that output with the config
snippet that you provided?

I've tried 1.4.3 and 1.5.7, and I don't get /stage/ to load correctly.

I suspect I'm just doing something silly.

> Certainly: an example URL is /stage/my-account/ . Ultimately, I would
> like for this URL to be rewritten to
> /stage/index.php?q=/stage/my-account/ (the file "index.php" exists on
> the filesystem at /var/www/example.com/private/stage/web/index.php).

I think that the problem may be due to the interaction between "alias"
and "$document_root" being not-always-obvious.

And $document_root is used in try_files, which is probably what you
want for "clean URLs" -- serve the file if present, else hand it to
a controller.

Does the following do what you want for all cases you care about?

===
      location ^~ /stage/ {
        alias /var/www/example.com/private/stage/web/;
        index index.php index.html index.htm;
        try_files $uri $uri/ /stage//stage/index.php?q=$uri;

        location ~ ^/stage/(.+\.php)$ {
                alias /var/www/example.com/private/stage/web/$1;
                try_files "" / /stage/index.php?q=$uri;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_param HTTPS on;
                fastcgi_param SCRIPT_FILENAME $request_filename;
                include /etc/nginx/fastcgi_params;
        }
      }
===

You may want to use something other than $uri in the last argument to
try_files, depending on how you want /stage/my-account/?key=value to
be processed.

And the curious-looking try_files lines are deliberate.

	f
-- 
Francis Daly        francis at daoine.org



More information about the nginx mailing list