Adil's blog Blog about web-development.

5Jul/091

lighttpd frontend + apache backend

light_logo_170pxI think you have seen such urls like static.blablabla.com on your favorite web sites. What is it?

Apache HTTPD uses more memory and CPU resources when it has more connections and this make the web site load slower. The light web servers like lighttp, nginx etc. is used to solve this problem.

I will show you how to use lighttpd as proxy to apache and seperate static content from dynamic. Let's begin.

First of all change the apache port.

#Listen 80
Listen 81

I recommend to block the direct access to 81 port using firewall.

Actually Apache will listen to 81th port and lighttpd will listen to 80th port and lighttpd will transfer requests to apache. The connections will be held by lighttpd and apache will held only connections between lighttpd and itself.

Now restart apache:
Linux:

/etc/init.d/apache2 restart

Windows:

net stop apache2
net start apache2

Now time to install lighttpd. Follow the instructions at http://www.lighttpd.net/

Edit the configuration file lighttpd.conf in its directory.
Add the following lines:

#Redirect the hosts which will be used for dynamic content to apache
$HTTP["host"] == "blabla.com" {
  proxy.server = ("" => ( (
        "host" => "127.0.0.1",
        "port" => 81 ) )
  )
}
 
# make virtual host for static content.
$HTTP["host"] == "static.blabla.com" {
  var.server_name = "static.blabla.com"
  server.name = server_name
  server.document-root = "/srv/www/vhosts/static"
}

Make sure the following modules are enabled in lighttpd:

server.modules = (
  "mod_access",
  "mod_alias",
  "mod_redirect",
  "mod_rewrite",
  "mod_proxy",
  "mod_accesslog"
)

Save the file. Restart the lighttpd typing /etc/init.d/lighttpd restart. Enjoy.