$HTTP["remoteip"] !~ "12\.34\.56\.78|34\.56\.78\.90" { url.access-deny = ( "" ) }
var.allowedIpAddresses = "127\.0\.0\.1|" var.allowedIpAddresses += "12\.34\.56\.78|" # Bob var.allowedIpAddresses += "34\.56\.78\.90|" # Kevin var.allowedIpAddresses += "56\.78\.90\.12" # Stuart $HTTP["remoteip"] !~ allowedIpAddresses { url.access-deny = ( "" ) }
.
) in IP addresses to explicitly match dots rather than any single character|
), otherwise the empty string following it will match any IP address, then whitelisting everybody (i.e. filtering nothing).Lighttpd has no AND / OR logical operators, but it's possible to obtain the same behavior by nesting / chaining conditions.
condition1 {
condition2 {
doSomething
}
}
if
blocks are equivalent to a logical ANDcondition1 { doSomething } condition2 { doSomething }
if
blocks are equivalent to a logical ORif-then-elif-else
construct :condition1 { doSomething1then
block } else condition2 { doSomething2else if
block } else { doSomething3else
block }
The procedure below works better when Lighttpd is directly facing HTTP clients. If there is an extra layer (web cache, load balancing, ...) :
$HTTP["referer"] =~ "BADDOMAIN\.com|IMAGESUCKERDOMAIN\.com" { url.rewrite = ("(?i)(/.*\.(jpe?g|png))$" => "/path/to/hotlink.png" ) }
$HTTP["url"] =~ "(gif|jpg|png|svg)$" { expire.url = ( "" => "access plus 1 weeks" ), setenv.add-response-header = ( "Cache-Control" => "public, max-age=604800" ) }which becomes :
$HTTP["url"] =~ "(gif|jpg|png|svg)$" { $HTTP["referer"] =~ "BADDOMAIN\.com|IMAGESUCKERDOMAIN\.com" { url.rewrite = ("(?i)(/.*\.(gif|jpe?g|png|svg))$" => "pictures/hotlinking.png" ) } expire.url = ( "" => "access plus 1 weeks" ), setenv.add-response-header = ( "Cache-Control" => "public, max-age=604800" ) }
if-then-elif-else
rulesRead : What's the difference between Basic and Digest authentication ?
New password: PaSsWoRd Re-type new password: PaSsWoRd Adding password for user kevin
server.modules = ( "mod_auth", )
$HTTP["host"] =~ "" {
auth.backend = "htpasswd"
auth.backend.htpasswd.userfile = "/path/to/.htpasswd"
auth.require = ( "/url/to/protect" => ( this is what comes after "http://www.example.com/"
"method" => "basic",
"realm" => "prompt", will be displayed on the login/password pop-up window
"require" => "valid-user" valid-user to allow any valid user, or a list of |
-separated user=username
)
)
}
HTTP request sent, awaiting response...
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Basic realm="prompt", charset="UTF-8"
Content-Type: text/html
Username/Password Authentication Failed.
HTTP request sent, awaiting response... HTTP/1.1 401 Unauthorized WWW-Authenticate: Basic realm="prompt", charset="UTF-8" Content-Type: text/html Authentication selected: Basic realm="prompt", charset="UTF-8" Reusing existing connection to www.example.com:80. HTTP request sent, awaiting response... HTTP/1.1 200 OK Content-type: text/html; charset=utf-8
Adding password for kevin in realm prompt. New password: PaSsWoRd Re-type new password: PaSsWoRdSame remark as above regarding the password file.
kevin:prompt:222b05e09cf0635131ab4f0a44bd5d59
server.modules = ( "mod_auth", )
$HTTP["host"] =~ "" {
auth.backend = "htdigest"
auth.backend.htdigest.userfile = "/path/to/.htdigest"
auth.require = ( "/url/to/protect" => ( this is what comes after "http://www.example.com/"
"method" => "digest",
"realm" => "prompt", will be displayed on the login/password pop-up window
"require" => "valid-user" valid-user to allow any valid user, or a list of |
-separated user=username
)
)
}
$HTTP["host"] =~ "" { server.document-root = "/var/www/myVirtualhost" accesslog.filename = "/var/log/myVirtualhost.log" $HTTP["url"] =~ "^/" { match everything auth.backend = "htdigest" auth.backend.htdigest.userfile = "/path/to/.htdigest" auth.require = ( "" => ( empty string, on purpose "method" => "digest", "realm" => "prompt", "require" => "valid-user" ) ) } }
HTTP request sent, awaiting response...
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Digest realm="prompt", charset="UTF-8", nonce="5bf7111e:b193aaf497484f867e4c02793a0ff9fd", qop="auth"
Content-Type: text/html
Username/Password Authentication Failed.
HTTP request sent, awaiting response... HTTP/1.1 401 Unauthorized WWW-Authenticate: Digest realm="prompt", charset="UTF-8", nonce="5bf7120c:89e30ae25bad5914828b14a0d99319f7", qop="auth" Content-Type: text/html Authentication selected: Digest realm="prompt", charset="UTF-8", nonce="5bf7120c:89e30ae25bad5914828b14a0d99319f7", qop="auth" Reusing existing connection to www.example.com:80. HTTP request sent, awaiting response... HTTP/1.1 200 OK Content-type: text/html; charset=utf-8
Flag | Usage |
---|---|
-f configurationFile | Load file configurationFile |
-t | test the configuration file for syntax errors and exit |
-tt | Test the configuration file for syntax errors, load and initialize modules, and exit
this mode may be over-pernickety —especially while the daemon is running— reporting that modules can not be loaded twice. Try with -t
|
url.rewrite-once = ( "^/(.*)" => "/index.html" )
(?!expression)
expression itself can also have some (
)
, be careful .
server.modules = ( , "mod_expire", )
$HTTP["url"] =~ "html$" { expire.url = ( "" => "access plus 15 minutes" ) } $HTTP["url"] =~ "(gif|jpg|png)$" { expire.url = ( "" => "access plus 2 hours" ) }Syntax :
[access|modification] plus n [years|months|days|hours|minutes|seconds]
$HTTP["url"] =~ "^/$" { }
server.modules += ( "mod_setenv" )
setenv.add-response-header += (
"Cache-Control" => "public, max-age=86400"
)
$HTTP["url"] =~ "^/$" { setenv.add-response-header = ( "Cache-Control" => "public, max-age=86400" ) }
server.modules = ( , "mod_redirect", )
$HTTP["host"] =~ "^(my\.site\.tld)" { server.document-root = "/var/www/my.site.tld" url.redirect = ( "^/(.*)\.xml$" => "http://%1/$1.html" ) }
(regExp)
from the current regexp match (i.e. the url.redirect directive). Here $1 matches "document" (since the HTTP request is for /document.xml).(regExp)
from the previous regexp match (i.e. the $HTTP["host"]
directive). Here %1 matches "my.site.tld".url.redirect-code = 302
server.errorlog = "/var/log/lighttpd/error.log"
debug.log-condition-handling = "enable" # nice but VERY verbose
debug.log-response-header = "enable"
debug.log-request-handling = "enable"
server.modules += ( "mod_accesslog" )
server.modules = (
...
"mod_setenv",
"mod_accesslog",
...
)
# TESTING (SSL) $SERVER["socket"] == "192.168.144.114:443" { server.document-root = "/var/www/ssl" ssl.engine = "enable" ssl.pemfile = "/etc/lighttpd/ssl/example.com/example.com.pem" } # /TESTING (SSL)