0

I would like to use a rewrite rule that is executed when a symlink exists but is broken.

So the scenario's would be:

  1. Symlink does not exist: normal 404/403 error.
  2. Symlink exists but is broken: generate-cache.php is called.
  3. Symlink exists and is working: target file is loaded normally.

For example:

## Symlink does not exist.
GET /links/cache/secret.jpg
404 Not Found

## Symlink is broken.
GET /links/cache/secret.jpg
  Links to /images/cache/secret.jpg
  Because it's broken, rewrites to: generate-cache.php?path=cache/secret.jpg
200 OK

## Symlink works.
GET /links/cache/secret.jpg
  Links to /images/cache/secret.jpg
200 OK

Update: I want to avoid using PHP to do these checks, because it causes a performance bottleneck. Outputting the file through PHP if it exists causes PHP to lock. Also I have no option to use multiple PHP threads or install additional apache modules.

1
  • A web server with only one thread is a) very unusual and b) unlikely to be very useful. Do you mean that it causes too many threads, or are you encountering some specific locking issue?
    – IMSoP
    May 15, 2013 at 17:41

1 Answer 1

2

I don't know of a way of testing for a broken symlink in mod_rewrite (-l checks for the existence of a symlink, but doesn't attempt to follow it), which may mean you'd need to write some kind of callback in PHP (or some other language).

An alternative approach would be to rewrite all requests, and build this logic in PHP:

  1. if the file exists in the cache directory, set appropriate headers and use readfile() to output the data
  2. if the symlink exists (or just an empty file with the right name in a "control" directory; I presume you have some other process creating the symlinks, so this could be amended to touch files instead), do appropriate generation
  3. if the symlink/control file doesn't exist, send a 404 header and immediately exit

Another variation, slightly more efficient, would be to let Apache serve the cached image directly if it exists, and rewrite to PHP for steps 2 and 3. Something like this:

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteRule /links/cache/(.*) generate-cache.php?path=$1

And in PHP

if ( ! file_exists('cache_control/' . $_GET['path'] )
{
     header('HTTP/1.1 404 Not Found');
     exit;
}
else
{
     // Control file exists, so this is an allowable file; carry on...
     generate_file_by_whatever_magic_you_have( 'links/cache/' . $_GET['path'] );
     header('Content-Type: image/jpeg'); // May need to support different types
     readfile( 'links/cache/' . $_GET['path'] );
     exit;
}

Assuming you can replace the symlinks with control files, and the names match up directly (i.e. the target of your symlink can be "guessed" from its name), you could move the control file check into mod_rewrite as well:

# If the requested file doesn't exist (if it does, let Apache serve it)
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
# Match the basic path format and capture image name into %1
RewriteCond %{REQUEST_FILENAME} /links/cache/(.*)
# Check if a cache control file exists with that image name
RewriteCond %{DOCUMENT_ROOT}/cache_control/%1 -f
# If so, serve via PHP; if not, no rewrite will happen, so Apache will return a 404
RewriteRule /links/cache/(.*) generate-cache.php?path=$1
8
  • Using PHP is exactly what I try to avoid in this scenario, for performance issues. Outputting files using PHP causes PHP to lock for the duration of the output. In larger galleries this is too slow. The symlink is generated by PHP ahead of time to mark you have permissions to generate the target file if it does not exist.
    – Beanow
    May 15, 2013 at 17:34
  • See the extra solution I've just added which puts more of the effort into Apache, but using empty control files rather than broken symlinks. Also, when you say "causes PHP to lock", what do you mean? Your server should be capable of serving plenty of concurrent PHP processes. Perhaps you are opening an (unneeded) PHP session for each image served, and this is causing things to lock?
    – IMSoP
    May 15, 2013 at 17:39
  • @Beanow Also, note that the solution I gave the PHP code for only creates a PHP process if the image doesn't exist on disk. The only difference from your idea (and my all-rewrite suggestion) is that it has to start PHP even for files which are "not allowed", but in that case the PHP script processes 3 lines of code then exits, so is unlikely to cause a significant performance issue.
    – IMSoP
    May 15, 2013 at 17:47
  • If with sessions you mean session_start(), yes that was being used by a framework that does the generating of the cache file. The performance issue is that images files are loaded sequentially when PHP outputs them even if no image manipulation is performed or any files overlap.
    – Beanow
    May 15, 2013 at 17:53
  • 1
    @Beanow The reason sessions cause locking is that the whole session is saved into a single file. If two processes could "open" the same session, they could set the same session variable to conflicting values. If you need to read something from the session but not write to it, you can call session_write_close() immediately after session_start(); it will still have to wait for a lock, but it will release it as soon as it gets it.
    – IMSoP
    May 15, 2013 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.