Recipe 3.12 Logging Proxy Requests

Problem

You want to log requests that go through your proxy to a different file than the requests coming directly to your server.

Solution

Use the SetEnv directive to earmark those requests that came through the proxy server, in order to trigger conditional logging:

<Directory proxy:*>     SetEnv is_proxied 1 </Directory> CustomLog logs/proxy_log combined env=is_proxied

Discussion

Apache 1.3 has a special syntax for the <Directory> directive, which applies specifically to requests passing through the proxy module. Although the * makes it appear that wildcards can be used to match documents, it's misleading; it isn't really a wildcard. You may either match explicit paths, such as proxy:http://example.com/foo.html, or use * to match everything. You can not do something like proxy:http://example.com/*.html.

If you want to apply different directives to different proxied paths, you need to take advantage of another module. Because you're dealing with requests that are passing through your server rather than being handled by it directly (i.e., your server is a proxy rather than an origin server), you can't use <Files> or <FilesMatch> containers to apply directives to particular proxied documents. Nor can you use <Location> or <LocationMatch> stanzas, because they can't appear inside a <Directory> container. You can, however, use mod_rewrite's capabilities to make decisions based on the path of the requested document. For instance, you can log proxied requests for images in a separate file with something like this:

<Directory proxy:*>     RewriteEngine On     RewriteRule "\.(gif|png|jpg)$" "-" [ENV=proxied_image:1]     RewriteCond "%{ENV:proxied_image}" "!1"     RewriteRule "^" "-" [ENV=proxied_other:1] </Directory> CustomLog logs/proxy_image_log combined env=proxied_image CustomLog logs/proxy_other_log combined env=proxied_other

Directives in the <Directory proxy:*> container will only apply to requests going through your server. The first RewriteRule directive sets an environment variable if the requested document ends in .gif, .png, or .jpg. The RewriteCond directive tests to see if that envariable isn't set, and the following RewriteRule will set a different envariable if so. The two CustomLog directives send the different types of requests to different logfiles according to the environment variables.

See Also

  • The mod_rewrite and mod_log_config documentation



Apache Cookbook
Apache Cookbook: Solutions and Examples for Apache Administrators
ISBN: 0596529945
EAN: 2147483647
Year: 2006
Pages: 215

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net