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

Putting it All Together

As we have seen on the last pages, Lighttpd has a lot to offer for setting up a download or streaming server. But we might be puzzled about how they relate to each other, and how we can use them together for optimal benefit. Therefore, here is a huge example configuration that has it all:

# use all modules we need
server.modules = (...,
"mod_rewrite", "mod_trigger_b4_dl", "mod_flv_streaming",
"mod_evasive", "mod_extforward", "mod_secdownload", "mod_alias",
"mod_accesslog", ...)
server.document-root = "/web/public"
# use the rewrite trick
$HTTP["cookie"] =~ "download/([0-9a-f]){32}/([0-9a-f]){8}" {
url.rewrite = ("/download/(.*)$" => "/download/%1/%2/$1")
} else {
# rewrite to avert mod_secdownload
url.rewrite = ("^/download/" => "/freeload/")
}
# configure mod_secdownload
secdownload.secret = "foo bar qux!!!1one!eleven"
secdownload.document-root = server.document-root
secdownload.uri-prefix = "/download/"
secdownload.timeout = 120
# configure mod_dirlisting
dir-listing.exclude = ("~$", "\.old$")
dir-listing.show-header = "enable"
dir-listing.hide-header-file = "enable"
dir-listing.external-css = "/css/dir.css"
# or use a dir listing generator
# (see chapter 3 for PHP configuration)
# server.index_files = ("index.html", "/dirlisting.php")
$HTTP["url"] =~ "^/download/([0-9a-f]){32}/([0-9a-f]){8}/" {
# paying customers, full speed
server.backend = "posix-aio"
server.kbytes-per-second = 0
connection.kbytes-per-second = 0
# make it easy on the paying customer
server.max-write-idle = 720
# they pay, we trust
extforward.forwarder = ("all" => "trust")
dir-listing.activate = "enable"
dir-listing.set-footer = "Thanks for trusting <a\
href=\"http://ourdomain.com\">ourdomain.com</a>!"
# finally, allow streaming
flv-streaming.extensions = ( ".flv" )
} else $HTTP["url"] =~ "^/freeload" {
# also downloads, so use the best backend
server.backend = "posix-aio"
# re-redirect internally to circumvent mod_secdownload. We could
# also use a different directory with symbolic links
alias.url = ("/freeload" => "/download")
# anonymous freeloaders, support by ads
trigger-before-download.gdbm-filename = "/web/internal/ad_trigger.db"
trigger-before-download.trigger-url = "^/ads/"
trigger-before-download.download-url = "^/download/"
trigger-before-download.deny-url = "/no-ads-no-downloads.html"
trigger-before-download.trigger-timeout = 5
dir-listing.activate = "enable"
dir-listing.set-footer = "Why not <a\
href=\"http://ourdomain.com/sign.html\">sign up</a>?"
# also traffic-shape, make it sloooow
server.kbytes-per-second = 1024
connection.kbytes-per-second = 32
evasive.max-conns-per-ip = 2
# and throttle keepalives to close sessions earlier
server.max-keepalive-requests = 8
server.max-keepalive-idle = 5
# allow streaming anyway
flv-streaming.extensions = ( ".flv" )
}

With this configuration, our Lighttpd is a true download server. Note that most of this configuration has little or no side effects. So we can also use our Lighttpd for other purposes in parallel. By the way, if you want to use the above configuration, download the downloadserver.conf file from http://www.packtpub.com/files/code/2103_Code.zip.