anonymous-one
anonymous-one

Reputation: 15122

rewrite based accesslog using apache + mod_rewrite - possible?

we have a bunch of mod_rewrite rules.

these are simple URI + GeoIP Continent = R=302,L rules.

they work great. we have tested using boxes on various continents and everything works as expected.

we would like to add a final rule that simply enables an access log for requests that have not matched any of the 'above' rules, but allows the request to be fulfilled as requested. just so we can get a more clear insight as to whats making it thru all the rules un-redirected.

possible using apache2 + mod_rewrite?


how we accomplished this, as the answer by regilero states below (credit is all his):

1)

at the end of the rewrite rules, we added a rule that was sort of a catch all for the specific file we wanted to log. we used the E=envname:value as regilero stated.

RewriteRule ^filename.ext$ filename.ext [L,E=logme:true]

2)

we then added the following customlog entry

CustomLog /var/log/httpd/your_custom_log.log "%v:%p | %r | %{User-agent}i" env=logme

as you can see, env=logme. basically meaning if variable logme is set, customlog that shizzle.

the above customlog is fairly minimal, we just want to know the server:port | requested url | useragent. you can change it as you see fit using the "Custom Log Formats" found in http://httpd.apache.org/docs/2.0/mod/mod_log_config.html.

3)

using the above we are able to find out what (if anything) is making it thru our slightly complex (useragent + geoip continent) rewrite rules and act on this information accordingly.

a final note: although we did not do any real empirical testing, im sure these 'catch all' rewrite rules are fairly heavy. so ideally, you will only want to do this as a decision making tool and turning them off once you decide the correct course of action. although im sure that all depends on how busy your web property is ;)


this is VERY useful. using this method you can log all sorts of activity. once again, a heart felt thank you to regilero. we will be using this all over.

Upvotes: 2

Views: 659

Answers (1)

regilero
regilero

Reputation: 30546

You can use mod_rewrite to set an environment variable on the last catch-all rule (for the ones which did'nt match any geoIp rules) with the tag [E=myspecialenvname:1], you could unset this variable in other matching case in case of specific multi-loop process with [E=!myspecialenvname] on the other rules.

Then you'll have to log based on that env thing. If you check customLog directive you'll see the syntax is:

CustomLog file|pipe format|nickname [env=[!]environment-variable]

The third argument is optional and controls whether or not to log a particular request based on the presence or absence of a particular variable in the server environment. If the specified environment variable is set for the request (or is not set, in the case of a 'env=!name' clause), then the request will be logged.

So you'll have to write something like:

CustomLog path/to/unmatched-geoip.log "%h %l %a %A %t \"%r\" %>s %b" env=myspecialenvname

Upvotes: 3

Related Questions