While a large proportion of the world's webservers are currently using Apache, a competitor has been steadily gaining popularity on many high-usage sites.
Lighttpd, pronounced "lighty", is a small-footprint, high-speed webserver, and is notably used by sites such as
Sourceforge,
YouTube and
MiniNova. Netcraft state that Lighttpd is currently being used on
1.38 million sites, and is steadily gaining on Sun's share of the market. This introductory article provides a guide to getting Lighttpd installed and configured.
Conditionals
Most of the options available in lighttpd can be configured conditionally; that is, the options may have different values under different conditions. For example, we may have a webserver set up where the default index page is index.html under all directories. Some time later, we decide to import a large directory of content from an outside source where the default index page was home.html. The conditional variable $HTTP["url"] can be used to handle this, in conjunction with a regular expression:
index-file.names = ( "index.html" )
$HTTP["url"] =~ "^/newcontent/" {
index-file.names = ( "home.html" )
}
This will limit the second index-files.names statement to only those files under the /newcontent directory.
The available conditional variables are:
- $HTTP["cookie"] - matches on cookie
- $HTTP["host"] - matches on the hostname in the url
- $HTTP["useragent"] - matches on useragent
- $HTTP["referer"] - matches on referer url
- $HTTP["url"] - matches on the document filename part of the url
- $HTTP["remoteip"] - matches on the client's IP address
- $SERVER["socket"] - matches on the socket, format ipaddr:port
There are four operators available:
- == - matches if strings are equal
- != - matches if strings are not equal
- =~ - true if there is a regexp match
- =! - true if there is not a regexp match
$SERVER["socket"] only supports the equality operator. It can be used to create IP based virtual hosts.
$HTTP["remoteip"] can be used to create areas within the document tree that are available only to certain IP ranges:
$HTTP["remoteip"] == "192.168.3.0/24" {
status.status-url = "/server-status"
status.config-url = "/server-config"
status.statistics-url = "/server-statistics"
}
Virtual Hosting
Now that we're familiar with conditionals, we can easily set up name-based virtual hosting. The following lines create two name-based virtual hosts, domain1.biz and domain2.info:
$HTTP["host"] =~ "(^|\.)domain1.biz$" (
server.document-root = "/usr/local/lighttpd/virtual/domain1.biz/"
)
$HTTP["host"] =~ "(^|\.)domain2.info$" (
server.document-root = "/usr/local/lighttpd/virtual/domain2.info/"
)
IP or port based virtual hosting can be done just as easily, using
$SERVER["socket"]. The following configuration will create two separate virtual hosts, one listening on 10.1.8.1 port 80, and the other listening on 10.1.8.5, port 80:
$SERVER["socket"] == "10.1.8.1:80" (
server.document-root = "/usr/local/lighttpd/virtual/server1/"
)
$SERVER["socket"] == "10.1.8.5:80" (
server.document-root = "/usr/local/lighttpd/virtual/server2/"
)
If you found this article helpful, consider making a donation to offset the costs of running this server, to one of these addresses: