We often recommend to set lighttpd in front of apache to handle http requests (more about https://www.percona.com/blog/2006/05/21/speedup-your-lamp-stack-with-lighttpd/ ) , redirect dynamic requests to apache and handle static files by itself. I just gathered step-by-step instruction how to do that in 10 minutes, as it may be not so obvious.
- Of course you need lighttpd by itself, it’s available on http://www.lighttpd.net/download
- You may want pcre-devel packet, which allows to use regular expressions in lighttpd.conf. For CentOS boxes we just run
1yum install pcre-devel
- Take sample config file
1lighttpd-1.4.XX/doc/lighttpd.conf1/etc/lighttpd/lighttpd.conf
- Create directory to store log files, e.g.
1/var/log/lighttpd
- Set
1server.document-root = "/www/html/htdocs"
- Uncomment next lines in lighttpd.conf
1234567server.modules = (..."mod_auth","mod_status","mod_proxy",...)mod_status is useful to show additional information from lighttpd
mod_auth to protect mod_status from unauthorized access 🙂
and mod_proxy is proxy by itself - to enable status uncomment or add
1status.status-url = "/server-status"
- to protect access to /server-status put next lines to lighttpd.conf
12345678910auth.backend = "htpasswd"auth.backend.htpasswd.userfile = "/var/www/.htpasswd"auth.require = ( "/server-status" =>("method" => "basic","realm" => "status","require" => "valid-user"))It enables htpasswd file based authentication, you can just manage this file with htpasswd utility from apache.
- and finally make changes related to proxy
1. Move apache from 80 port to another, e.g. 8080 or pick your own.
You need to change1Listen1VirtualHost
and meantime set1KeepAlive Off
2. put next lines to lighttpd.conf:
123456$HTTP["url"] !~ "\.(js|css|gif|jpg|png|ico|txt|swf|html|htm)$" {proxy.server = ( "" => (( "host" => "127.0.0.1", "port" => 8080 )))}
This actually directs lighttpd to redirect all requests, besides one ending1js|css|gif|jpg|png|ico|txt|swf|html|htm - Final step to add lighttpd to list of services (actual only for RedHat / CentOS)
1cp <sourcedirt>/doc/rc.lighttpd.redhat /etc/init.d/lighttpdfix path
1lighttpd="/usr/local/lighttpd/sbin/lighttpd"and add script to autostart
12chkconfig --add lighttpdchkconfig --level 2345 lighttpd on
That’s all. For complex apache setups and VirtualHosts it may be trickier, but work just fine for simple cases.
Comments (28)
This is interesting – we actually do the opposite 🙂 – apache as reverse proxy + mod_security + static file handling in front of the lighttpd/fcgi.
ew. you should look at nginx. ncache (based on nginx) if you need a proxy that -stores- a cache of the content.
nginx is the most efficient proxy out there, hands down. once you try it you -will not- go back.
Here, we use “HAproxy” in front for reverse proxy and load balancing.
It’s VERY fast to parse HTTP requests and it handle failover.
It’s possible to do the same thing with it : send all static content requests to lighttpd and all dynamic content to apache.
If you have a lot of traffic, you should look at this 🙂
http://haproxy.1wt.eu/
we do not handle static files at all 😉 we completely rely on amazon S3 for that and lighttpd just handle the dynamic pages
to solve the ETAG, expiration of new static files, we use either a revision number in the URLs or an SVN tag
we also decide to compress all the files we put on amazon S3 as gzip mode in mainly supported by all browsers
What do you mean with compress all the files? They’re just compressed with gzip and then decompressed on the fly by the web browser???
For example, having file.txt, do you mean compressing it to file.txt.gz and be able to access http://server.com/file.txt ? I’d love to know how to do it!
Or is just enabling the on the fly server compression like mod_deflate/mod_gzip, wich is forbidden for it’s cpu usage by most hostings?
Isn’t it better to use a cache/accelerator dedicated product like squid as a front-end instead of lighttpd?
use nginx for this.
it is better then lighttpd
now 2 million hosts use this server (month ago — 1 million…)
http://nginx.net/ enjoy
( http://wiki.codemongers.com/Main )
Isn’t Lighttpd better suited as a replacement for Apache rather than a front end to it ? To serve static content I recommend using thttpd, it is very fast, very stable and very light on resources.
or tux 🙂
http://www.redhat.com/docs/manuals/tux/TUX-2.2-Manual/
If you wish to give the impression of a single domain surely using Squid in reverse proxy mode is the simplest? Get one httpd to serve both static and dynamic, but get squid to cache the static?
At fav.or.it we have a dedicated domain for static content, which is served by lighttpd. Apache then hosts the dynamic.
sent from: fav.or.it [FID179790]