Lighttpd
上QQ阅读APP看书,第一时间看更新

Including Variables, Files, and Shell-code

Lighttpd allows us to define and use variables in its configuration files. To make it easier to distinguish between a configuration option and a variable, you have to prefix your variables with var. as in var.docroot. Later on, you can use them by simply putting them in place of whatever value you have given them. For example:

var.docroot = "/var/www"
server.document-root = var.docroot

This can be useful if you have values that appear in a lot of places. Just put them in a variable and if you need to change the value, you only need to change it in one place. We can also set and get variables of the environment. The env namespace is reserved for this:

server.document-root = env.HOME + "/htdocs" # for a user dir
server.document-root = env.LIGHTTPD_BASE + var.htmldir
# to use an environment variable

You can also include files with an include statement:

include "some.conf"

This tells Lighttpd to parse the contents of some.conf as if they were in place of the include statement. You can use includes and variables together to have something like a subroutine in most programming languages. Set variables in the outer config, use them in the included config, and we have a re-usable configuration component!

For example: A virtual host has its document root in /var/www/vhost1, another in /var/www/vhost2. We could set things up for both with a small code snippet in a file we will call vhost.conf file:

$HTTP["host"] == var.vhost + ".ourdomain.net" {
server.name = var.vhost + ".ourdomain.net"
server.document-root = var.docroot + var.vhost
server.follow-symlink = disabled
# we do not trust our vhost
accesslog.filename = var.log + var.vhost
# log each vhost seperately
index-file.names = ("index.html", "index.htm")
# only HTML index files, may be different outside the vhosts
}

Then, our lighttpd.conf could include it like this:

#...
var.docroot = "/var/www/"
var.log = "/var/log/"
var.vhost = "vhost1"
include "vhost.conf"
var.vhost = "vhost2"
include "vhost.conf"
#...

Now, we have set up both virtual hosts and our lighttpd.conf file still looks quite tidy. It is also possible to include the output of a program into the lighttpd.conf file with the include_shell command.

This might seem like a great deal, but remember that usually you are the one starting Lighttpd, so you can also put the output of the include_shell command into a file and include it.

Note

Security Alert (virtual hosting only)

If you are setting up virtual hosting and want to allow your users access to their own configuration, you need to disable or otherwise forbid this feature. Othewise, you give everyone whose configuration you include, a free root shell (given that Lighttpd is started as root, which is required for listening on port 80). You can disable this by downloading configfile.c.patch file from http://www.packtpub.com/files/code/2103_Code.zip.

Another thing variables can be useful for is distinguishing a test and production environment. You might want to run a test Lighttpd without disturbing your production Lighttpd. Variables to the rescue—put the following into lighttpd.conf file:

var.http_port = 80
var.https_port = 443
var.docroot = "/var/www/prod"
include "lighttpd_conf.conf"

And create a test.conf with:

var.http_port = 1080
var.https_port = 1443
var.docroot = "/var/www/test"
include "lighttpd_conf.conf"

Now our lighttpd.conf file can simply use the variables to set the ports (instead of having them plain in our file) and the document root, and then we can start a test Lighttpd that:

  • Listens on different ports and will thus start while our production Lighttpd is still running.
  • Does not mess with our production assets as it uses another document root.