I have a question regarding lighttpd
best practices for vhosts setups. I wanted to set up a modular config for several vhosts with splitted config files to be able to enable/disable specific vhosts easily. Platform is Debian 8.2 running lighttpd 1.4.35. Requirements are as follows:
- the server should bind to a public NIC and listen on port 80 for the public vhosts
- it also should listen on port 81 for machine2machine communication only
- additionally it should bind to a NIC on an internal network (both ports 80/81)
- vhosts should be defined in separate files under
conf-enabled/vhostN.conf
So I did set up as follows (1.2.3.4 be the IP of the public NIC, 10.0.0.1 the IP inside the private network). In the central lighttpd.conf
I first define a default server binding to both IPs:
server.bind = "1.2.3.4" # public default server
server.port = 80
$SERVER["socket"] == "10.0.0.1:80" {} # private default server
The vhosts are set up in separate files under conf-enabled/
, say vhost1.conf
, vhost2.conf
etc. I tried to split the following directives across the config files:
## This is for machine2machine communication on port 81 over the Internet
$SERVER["socket"] == "1.2.3.4:81" {
...
}
## This is for machine2machine communication on port 81 inside the private net
else $SERVER["socket"] == "10.0.0.1:81" {
...
}
## This is vhost 1, accessible through Internet and private network
else $HTTP["host"] =~ "^vhost1.do.main$" {
...
}
## This is vhost 2
else $HTTP["host"] =~ "^vhost2.do.main$" {
...
}
and so on. Everything works so far if I put it all in one big config file. But when I split the config into separate files, it seems that the else
gives syntax errors since it is not honored across the included cfg files.
While the else
would be redundant between the vhosts sections triggered by the HTTP Host
header, it is not so in relation to the $SERVER["socket"]
section. If I would remove the else
, it would be possible to access all vhosts through port 81, inheriting the setup for the m2m host in addition to the ones for named vhosts.
Furthermore, I could not figure out how to define the section for m2m comm in only one block with two $SERVER["socket"]
directives for two IPs, so I had to duplicate the setup for the same vhost bound to port 81. If there is a way to combine two $SERVER["socket"]
directives for a single section, I could use the default server for port 81 and surround the named vhosts with a $SERVER["socket"]
directive binding to port 80.
So my questions are:
- Is there a trick I didn't see in the docs to accomplish such a vhost setup with separate cfg files resulting in the same effect as using
else
? - Can two
$SERVER["socket"]
directives be combined to avoid having to duplicate the same vhost twice and to encapsulate a bunch of named vhosts, thus avoiding the need for theelse
? I tested with the||
operator, but that didn't work.
Any ideas?