Serving static versions of dynamic pages (non-RoR)

Jean-Philippe skateinmars at
Thu Aug 7 15:32:18 MSD 2008

Ian M. Evans a écrit :
> As you might remember from the "fastcgi php migration" thread back in 
> March, the list helped solve a problem migrating our apache setup where 
> php ran through files with extensions (.php, .shtml) and extensionless 
> files (e.g. /galleries/123/72 where galleries is a php script NOT a 
> directory and /123/72 were the arguments or /some-event/day1/photos/12 
> where photos was a php script that could be located in any depth of 
> subdirs)
> The solutions suggested by Igor and the list have worked perfectly. 
> Thanks again!
> I've been looking at various .conf's and have seen how they handle Rails 
> static caching and I'm wondering how to apply that in my non-Rails setup.
> Sometimes after a big event we can get hammered (once even getting 
> listed on a Yahoo! news page) and I've thought about creating static 
> versions of my script pages using php and curl or something.
> I'd have two situations. One where an extensioned but argument-less php 
> script had a cached .html version and second where an extensionless 
> script and its pathinfo had a cached version
> 1) In the first case, say I had a database hungry file at:
> /tribeca-fest/winners.shtml and I created a cached version as 
> /tribeca-fest/winners.shtml.html am I correct in assuming that:
> if (-f $request_filename.html) {
> break;
> }
> would serve it properly?

You have to rewrite your request for nginx to understand that it should serve 
the cached version. Common rails recipes use :

     if (-f $request_filename/index.html)
       rewrite ^(.*)$ $1/index.html break;

     if (-f $request_filename.html)
       rewrite ^(.*)$ $1.html break;

(So it can be used for directories and files, you may want to skip the first part.)

> 2) The 2nd case would be a little more crazy and I'm not sure if it's 
> doable.
> Let's say I have the extensionless file "galleries" in the root and it's 
> pathinfo can be:
> /galleries/123/     <- an overview listing
> /galleries/123/5    <- a more detailed listing
> /galleries/123/5/72 <- an individual photo
> I could create three cache files:
> /galleries_123.html
> /galleries_123_5.html
> /galleries_123_5_72.html
> Is there anyway for nginx to translate a request for /galleries/123/5/72 
> or the other two examples into their underscore seperated .html equivalent?

Seems tricky if you want to test the cache existence and not simply rewrite to 
the underscored version (which could be done with a simple rewrite).

Maybe an if block like this :

if ($request_filename ~ (foo)/(bar)/(baz)) {
set $foo $1;
set $bar $2;
set $baz $3;

Could be used before another if block to check the local file.

> I'm thinking that handling #1 is dead easy but the problem in #2 might 
> be a little nuts? :-)
> Thanks for any advice.

More information about the nginx mailing list