Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have some mod rewrite rules that test for the presence of a file on various NFS mounts and I would like that the tests occur randomly, as a very rudimentary way to distribute load.

For example:

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(.+)$
RewriteCond /mnt/mount1/%1 -f
RewriteRule ^/(.+)$ /mnt/mount1/$1 [L]
RewriteCond %{REQUEST_URI} ^/(.+)$
RewriteCond /mnt/mount2/%1 -f
RewriteRule ^/(.+)$ /mount2/$1 [L]
RewriteCond %{REQUEST_URI} ^/(.+)$
RewriteCond /mnt/mount3/%1 -f
RewriteRule ^/(.+)$ /mnt/mount3/$1 [L]
RewriteCond %{REQUEST_URI} ^/(.+)$
RewriteCond /mnt/mount4/%1 -f
RewriteRule ^/(.+)$ /mnt/mount4/$1 [L]

As far as I understand mod_rewrite Apache will look for the file on /mnt/mount1, then mount2, mount3 and so on.

Can I randomize this on each request? I understand this is an odd request but I need a creative solution to some unforeseen downtime.

On a side note, do I need to redeclare RewriteCond %{REQUEST_URI} ^/(.+)$ each time like I have done?

Thanks

share|improve this question

2 Answers 2

As a suggestion, you can make a cron job that changes the file with the rewrites with other config file with the rewrite conditions in different order. You can run that cron job every minute and have different config files to replace it.

share|improve this answer
    
But I would have to constantly reboot Apache to do that, right? –  Tom Jul 3 '12 at 23:56
    
well, in the same script on the cron you can restart apache service. –  linux with a hammer Jul 5 '12 at 18:54
    
i'm not sure, but i think you can also put the rewrites in a .htaccess file. in that case you don't need to restart apache, just change the htaccess file –  linux with a hammer Jul 5 '12 at 18:55
up vote 0 down vote accepted

I think I found a feature in mod_rewrite that can accomplish this: RewriteMap prg

http://httpd.apache.org/docs/current/rewrite/rewritemap.html

share|improve this answer
    
RewriteMap rnd (randomized plain text) would be even better. –  Andrew Jul 4 '12 at 0:08
    
Are you sure? I need to check the presence of files (the files are not located on all the mounts - only some - and this must be checked. Apache then follows the first valid path). How would I do that with rnd? –  Tom Jul 4 '12 at 1:10
    
It depends how you state the question - the simple case of "random one of these values in a rewrite check" would use rnd, more complex checking could either modify the mapping file (possible race condition between read and change of state) or use an external program. –  Andrew Jul 4 '12 at 1:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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