mod_rewrite Recipes
Dynamic virtual hosts
Allow each user on your system automatically have username.domain.com mapped to their home directory.
RewriteEngine On
# Skip www.domain.com
RewriteCond %{HTTP_HOST} !^(www.)
RewriteCond %{HTTP_HOST} ^([^.]+).domain.com
RewriteRule ^(.*)$ /home/%1/www$1
Serve content through Coral CDN for requests generated from myspace.com
RewriteEngine On
RewriteCond %{HTTP_REFERER} myspace.com
RewriteCond %{HTTP_USER_AGENT} !^CoralWebPrx # Prevent Coral Cache client from entering a loop
RewriteCond %{QUERY_STRING} !coral-no-serve$
RewriteRule ^/(.*) http://skreuzer.f2o.org.nyud.net:8090/$1 [R,Last]
Check for Code Red IIS/Windows Hacking non-sense
RewriteCond %{REQUEST_FILENAME} /winnt/ [NC,OR]
RewriteCond %{REQUEST_FILENAME} /system32/ [NC,OR]
RewriteCond %{REQUEST_FILENAME} .ida.*$ [NC,OR]
RewriteCond %{REQUEST_FILENAME} .exe.*$ [NC,OR]
RewriteCond %{REQUEST_FILENAME} .com.*$ [NC,OR]
RewriteCond %{REQUEST_FILENAME} .dll.*$ [NC]
RewriteRule ^.*$ http://skreuzer.f2o.org/403.shtml [L]
Performace Tuning
MaxSpareServers and MinSpareServers determine how many child processes to keep while waiting for requests. If the MinSpareServers is too low and a bunch of requests come in, then Apache will have to spawn additional child processes to serve the requests. Creating child processes is relatively expensive. If the server is busy creating child processes, it won't be able to serve the client requests immediately. MaxSpareServers shouldn't be set too high, it can cause resource problems since the child processes consume resources.
Tune MinSpareServers and MaxSpareServers such that Apache need not frequently spwan more than 4 child processes per second (Apache can spwan a maximum of 32 child processes per second). When more than 4 children are spawned per second, a message will be logged in the ErrorLog.
The StartServers directive sets the number of child server processes created on startup. Apache will continue creating child process until the MinSpareServers setting is reached. Doesn't have much effect on performance if the server isn't restarted frequently. If there are lot of requests and Apache is restarted frequently, set this to a relatively high value.
The MaxRequestsPerChild directive sets the limit on the number of requests that an individual child server process will handle. After MaxRequestsPerChild requests, the child process will die. It's set to 0 by default, that means the child process will never expire. It is appropriate to set this to a value of few thousands. This can help prevent memory leakage since the process dies after serving a certain number of requests. Do not set this too low, since creating new processes does have overhead.
The KeepAlive directive allows multiple requests to be sent over the same TCP connection. This is particularly useful while serving HTML pages with lot of images. If KeepAlive is set to Off, then for each images, a separate TCP connection has to be made. Overhead due to establishing TCP connection can be eliminated by turning On KeepAlive.
KeepAliveTimeout determines how long to wait for the next request. Set this to a low value, perhaps between two to five seconds. If it is set too high, child processed are tied up waiting for the client when they could be used for serving new clients.
HTTP Compression & Caching
HTTP compression is completely specified in HTTP/1.1. The server uses gzip or deflate encoding method to the response payload before it is sent to the client. Client then decompresses the payload. There is no need to install any additional software at the client side since all major browsers support this. HTTP Compression can be enabled in Apache using mod_deflate module. Payload is compressed only if the browser requests compression, otherwise uncompressed content is served. A compression aware browser informs the server that it prefers compressed content through the HTTP request header - "Accept-Encoding: gzip,deflate". Then the server responds with compressed payload and the response header set to "Content-Encoding: gzip"
Comments (0)
You don't have permission to comment on this page.