Skip to content

Instantly share code, notes, and snippets.

@jtpaasch
Last active February 10, 2016 20:48
Show Gist options
  • Save jtpaasch/b13dd937c392e5c9e113 to your computer and use it in GitHub Desktop.
Save jtpaasch/b13dd937c392e5c9e113 to your computer and use it in GitHub Desktop.
Logging with nginx
# An events block is required for Nginx to run, even if it's empty.
events {}
# HTTP processing.
http {
# Note: log formats only apply to access_logs, not error_logs.
# Error logs always start with time stamp and then the level,
# e.g., [emerg] or [alert] or whatever.
log_format web_app
'{'
'"logname": "access-log-for-web-app", '
'"remote_addr": "$remote_addr", '
'"remote_user": "$remote_user", '
'"time_local": "$time_local", '
'"request": "$request", '
'"status": "$status", '
'"body_bytes_sent": "$body_bytes_sent", '
'"http_referer": "$http_referer", '
'"http_user_agent": "$http_user_agent"'
'}';
log_format usenet_app
'{'
'"logname": "access-log-for-usenet-app", '
'"remote_addr": "$remote_addr", '
'"remote_user": "$remote_user", '
'"time_local": "$time_local", '
'"request": "$request", '
'"status": "$status", '
'"body_bytes_sent": "$body_bytes_sent", '
'"http_referer": "$http_referer", '
'"http_user_agent": "$http_user_agent"'
'}';
log_format api_app
'{'
'"logname": "access-log-for-api-app", '
'"remote_addr": "$remote_addr", '
'"remote_user": "$remote_user", '
'"time_local": "$time_local", '
'"request": "$request", '
'"status": "$status", '
'"body_bytes_sent": "$body_bytes_sent", '
'"http_referer": "$http_referer", '
'"http_user_agent": "$http_user_agent"'
'}';
# Listen on any interface on port 9050.
server {
listen 0.0.0.0:9050;
server_name "";
# Any requests starting with / go here.
location / {
root /srv;
# Nginx will log to STDOUT if specify no access_log.
# If you want to specify a log format, use /dev/stdout.
# This one uses the `web_app` format.
access_log /dev/stdout web_app;
# Nginx will log to STDERR if you specify no error_log.
# But you can also do this:
error_log stderr;
}
# Any requests starting with /usenet go here.
location /usenet {
root /srv-alternate;
# Nginx will log to STDERR if you specify no error_log.
# But you can also do this:
error_log stderr;
# Nginx will log to STDOUT if specify no access_log.
# If you want to specify a log format, use /dev/stdout.
# This one uses the `usenet_app` format.
access_log /dev/stdout usenet_app;
}
# Any requests starting with /api go here.
location /api {
# Set up logging.
error_log stderr;
access_log /dev/stdout api_app;
# The FastCGI protocol cannot receive HTTP headers.
# So, it needs that data sent to it as fastcgi_params.
# This will include a set of industry-standard params:
include fastcgi_params;
# Where is the web root on the FastCGI server?
# Note: this path need not exist on this server.
# It specifies the path on the FastCGI server.
root /srv;
# FastCGI needs to know which file it should open.
# This sets the SCRIPT_FILENAME parameter, which FastCGI will
# read to figure out what file it should open.
# Note the variables:
# - $document_root: the same as the `root` directive.
# - $fastcgi_script_name: the requested URI, e.g., /my-page.php
# So, a request to /my-page would become /srv/my-page.php.
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# Or, if you want to always send the request to index.php, point
# directly to index.php in the doc root, like this:
fastcgi_param SCRIPT_FILENAME $document_root/logging.php;
# If the request URI ends in a slash, the fastcgi_index will be
# appended to it in $fastcgi_script_name. So, a request to /my-dir/
# would become /srv/my-dir/index.php.
# fastcgi_index index.php;
# Finally, where do we send the request? Note the `hhvm:9000`.
# That host is set by a docker link. You can't prepend "http://" to it.
fastcgi_pass hhvm:9000;
# Or, if you used a Unix socket:
# fastcgi_pass unix:/var/run/hhvm/sock;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment