Skip to content

Instantly share code, notes, and snippets.

@joewest
Created July 20, 2009 02:25
Show Gist options
  • Save joewest/150124 to your computer and use it in GitHub Desktop.
Save joewest/150124 to your computer and use it in GitHub Desktop.
This httpd.conf will set up Apache 2.0.51 or greater to create a basic key-value store in a log file.
# This httpd.conf will set up Apache to create a basic key-value store in a
# log file. Requests should be in the following format:
#
# http://hostname:8080/logr/key/value
#
# The log file will print a log line per request storing the key and value.
# I'd recommend using some log rotation method to parse the results into an
# accessible data store.
#
# An example request:
# shell> curl 127.0.0.1:8080/logr/key/value
#
# Generates this log entry:
# 127.0.0.1 [19/Jul/2009:22:15:59 -0400] key "value"
#
# Requires:
# Apache 2.0.51 or greater
# ok.html file in your root html dir (see below)
#
# Tested on Apache 2.2.11 (via MacPorts 1.710)
# Load the modules we need.
LoadModule dir_module modules/mod_dir.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule mime_module modules/mod_mime.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule setenvif_module modules/mod_setenvif.so
# Basic Apache stuff. Change to suit your install.
User _www
PidFile run/httpd.pid
Listen 8080
DocumentRoot htdocs/
DirectoryIndex index.html
TypesConfig conf/mime.types
# Basic logging.
ErrorLog logs/error_log
LogLevel warn
LogFormat "%h %l %u %t \"%r\" %>s %b" common
CustomLog logs/access_log common
# Sets environment variables based on regex.
SetEnvIf Request_URI "^/logr/([^\/]*)/(.*)$" KEY=$1 VALUE=$2
# Create a file called ok.html in your htdocs. Put anything you want in it.
# I like to include an affirmative message like:
# <html><body><h1>You did it!</h1></body></html>
#
# This will let apache return 200s for key value requests to play nice with
# APIs and other services that may call it.
RewriteEngine on
RewriteRule "^/logr/.*" /ok.html [L]
# Here's where the magic happens. It takes the env variables set earlier
# and writes the requesting IP, timestamp, key and value on a line.
# A comprehensive list of custom log format directives lives here:
#
# http://httpd.apache.org/docs/2.0/mod/mod_log_config.html#formats
LogFormat "%h %t %{KEY}e \"%{VALUE}e\"" kv
CustomLog logs/kv_log kv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment