Using Lighttpd to serve Gallery 2 static content with redirect rules

I’ve been testing this out and wanted to share it. It provided with a noticeable increase in certain items loading in specific ways that always bothered me.

  1. This use rewrite rules in the .htaccess file in the folder where your gallery main.php resides.
  2. This does not use mod_proxy on purpose because setting up mod_proxy securely is more work than it is worth for this method.
  3. This requires a second HTTP server, which can be on your own server or elsewhere. I chose Lighttpd because of its reputation and because I already had it running on my server. It is truly light and can serve an incredible amount of requests with low CPU or RAM usage. I mean LOW. I could describe how to set it up if anybody wants, but its incredibly easy.
  4. This could conceivably be expanded to include more files, if you want. I’m sure the rewrite rules could be improved. I’m not a master at them like the G2 team.
  5. This is great if you have a second hard drive on your server for the increased I/O capabilities.

That said, you can have Lighttpd running on your server on a second IP address, and therefore domain name/subdomain name, or on another port such as 81. My gallery is on the domain hardcoredreamer.com so I created a DNS A record for fast.hardcoredreamer.com to point to the IP where my Lighttpd process is on.

lighttpd.conf:
If you are running lighttpd on a different port, you would just need to set the document root to be the folder of your web root.
server.document-root        = "/location/of/my/website/root/"

Running on a subdomain:

$HTTP["host"] == "fast.hardcoredreamer.com" {
server.document-root = "/location/of/my/website/root/"
}

For security reasons:
url.access-deny             = ( "~", ".inc", ".htaccess", ".ini", ".php" )

This will deny somebody access to the config.php with my setup password or .htaccess files. I did not enable Lighttpd to be able to even use PHP, so there is no point in it trying to access them. This means Lighttpd stays “light”.

Inside of my /gallery/.htaccess file I added these rewrite rules [note if your gallery folder is called something else, use that name]:

RewriteCond %{REQUEST_FILENAME} .(js|css|gif|jpg|png)$ [NC]
RewriteCond %{REQUEST_URI} /gallery/themes/$
RewriteRule ^(.*)$ http://fast.hardcoredreamer.com/gallery/themes$1 [R,L]
RewriteCond %{REQUEST_FILENAME} .(js|css)$ [NC]
RewriteRule ^(.*)$ http://fast.hardcoredreamer.com/gallery%1/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} .png [NC]
RewriteCond %{REQUEST_URI} /gallery/modules/icons/iconpacks/$
RewriteRule ^(.*)$ http://fast.hardcoredreamer.com/gallery/modules/icons/iconpacks$1 [R,L]

Explanation: My problem was that my silk icon pack icons loaded incredibly slow. They’re small files and I have a 30Mbit connection from my server to home – I shouldn’t actually see them loading up from top to bottom. This redirects any requests for the .png icons in the icon pack to another server. What this does is redirects the requests to Lighttpd which in turn responds with the icons & themes .png, .css, and .js files in parallel to Apache serving all of the other content such as the main pages and thumbnails.

If you are running Lighttpd or another server on a different port, you would just change the RewriteRule to point to your domain/ip/anything:81 or whichever port you choose.

I’m sure this could be expanded upon somehow. I’m sure this makes little sense to many people. I’ve found performance gains by using mysqld_multi to run simultaneously a separate database instance on a second drive with its own innodb caches as well as another instance for Firestats (since it *severely* degrades performance when its sleeping threads tie up your gallery2/other threads.) If anyone is interested in that, let me know.

If you decide you want to try this out, watch your Lighttpd logs to see if the rules you’ve modified for your own server are indeed serving requests. You can also copy your /gallery folder and put that on another drive and just let Lighttpd have access to that one folder, but i’m also using Lighttpd to serve static files for my wordpress blog and anything else I can think of. Using redirect rules, the end user does not see that such and such file is actually located elsewhere.

I’m willing to say that if you use iconpacks, add in the rule for the .png files in your own .htaccess and see if it works. If your gallery folder is called gallery2 try:

RewriteCond %{REQUEST_FILENAME} .png [NC]
RewriteCond %{REQUEST_URI} /gallery2/modules/icons/iconpacks/$
RewriteRule ^(.*)$ http://fast.hardcoredreamer.com/gallery/modules/icons/iconpacks$1 [R,L]

Caveat: Gallery2 will change your .htaccess file if you tell it to, erasing any additions you make.

If I could just somehow figure out how to get the thumbnails sent through a static server, I’d be quite happy. Compared to Lighttpd, Apache is a lumbering beast filled with features but resource intensive.

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

Post a Comment

Your email is never published nor shared. Required fields are marked *

Improve the web with Nofollow Reciprocity.