Skip to content

Instantly share code, notes, and snippets.

@karlseguin
Created June 10, 2012 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karlseguin/2906701 to your computer and use it in GitHub Desktop.
Save karlseguin/2906701 to your computer and use it in GitHub Desktop.
nginx unsafe patch for filtering access_log logging by http status
Index: src/http/modules/ngx_http_log_module.c
===================================================================
--- src/http/modules/ngx_http_log_module.c (revision 4682)
+++ src/http/modules/ngx_http_log_module.c (working copy)
@@ -52,6 +52,7 @@
time_t disk_full_time;
time_t error_log_time;
ngx_http_log_fmt_t *format;
+ ngx_uint_t status;
} ngx_http_log_t;
@@ -94,6 +95,7 @@
ngx_http_log_op_t *op);
static u_char *ngx_http_log_status(ngx_http_request_t *r, u_char *buf,
ngx_http_log_op_t *op);
+static ngx_uint_t ngx_http_log_get_status(ngx_http_request_t *r);
static u_char *ngx_http_log_bytes_sent(ngx_http_request_t *r, u_char *buf,
ngx_http_log_op_t *op);
static u_char *ngx_http_log_body_bytes_sent(ngx_http_request_t *r,
@@ -136,7 +138,7 @@
{ ngx_string("access_log"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
- |NGX_HTTP_LMT_CONF|NGX_CONF_TAKE123,
+ |NGX_HTTP_LMT_CONF|NGX_CONF_TAKE1234,
ngx_http_log_set_log,
NGX_HTTP_LOC_CONF_OFFSET,
0,
@@ -239,6 +241,11 @@
}
log = lcf->logs->elts;
+
+ if (log->status > 0 && log->status != ngx_http_log_get_status(r)) {
+ return NGX_OK;
+ }
+
for (l = 0; l < lcf->logs->nelts; l++) {
if (ngx_time() == log[l].disk_full_time) {
@@ -577,26 +584,33 @@
{
ngx_uint_t status;
- if (r->err_status) {
- status = r->err_status;
-
- } else if (r->headers_out.status) {
- status = r->headers_out.status;
-
- } else if (r->http_version == NGX_HTTP_VERSION_9) {
+ if (r->http_version == NGX_HTTP_VERSION_9) {
*buf++ = '0';
*buf++ = '0';
*buf++ = '9';
return buf;
-
- } else {
- status = 0;
}
+ status = ngx_http_log_get_status(r);
+
return ngx_sprintf(buf, "%03ui", status);
}
+static ngx_uint_t
+ngx_http_log_get_status(ngx_http_request_t *r)
+{
+ if (r->err_status) {
+ return r->err_status;
+ }
+ if (r->headers_out.status) {
+ return r->headers_out.status;
+ }
+
+ return 0;
+}
+
+
static u_char *
ngx_http_log_bytes_sent(ngx_http_request_t *r, u_char *buf,
ngx_http_log_op_t *op)
@@ -877,13 +891,14 @@
ngx_http_log_loc_conf_t *llcf = conf;
ssize_t buf;
- ngx_uint_t i, n;
+ ngx_uint_t i, n, status;
ngx_str_t *value, name;
ngx_http_log_t *log;
ngx_http_log_fmt_t *fmt;
ngx_http_log_main_conf_t *lmcf;
ngx_http_script_compile_t sc;
+
value = cf->args->elts;
if (ngx_strcmp(value[1].data, "off") == 0) {
@@ -974,7 +989,7 @@
buffer:
- if (cf->args->nelts == 4) {
+ if (cf->args->nelts >= 4) {
if (ngx_strncmp(value[3].data, "buffer=", 7) != 0) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid parameter \"%V\"", &value[3]);
@@ -1014,6 +1029,16 @@
log->file->last = log->file->buffer + buf;
}
+ if (cf->args->nelts == 5) {
+ status = ngx_atoi(value[4].data, value[4].len);
+ if (status <= 0) {
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+ "invalid log status \"%V\"", &value[4]);
+ return NGX_CONF_ERROR;
+ }
+ log->status = status;
+ }
+
return NGX_CONF_OK;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment