Skip to content

Instantly share code, notes, and snippets.

@undying
Created June 11, 2025 15:57
Show Gist options
  • Save undying/b96e814d557d7f787048abfe8566549b to your computer and use it in GitHub Desktop.
Save undying/b96e814d557d7f787048abfe8566549b to your computer and use it in GitHub Desktop.
Script to count nginx errors by their types
#!/usr/bin/awk -f
BEGIN {
ERROR_USERID_SHORT = 0
ERROR_LIMIT_REQ = 1
ERROR_RECV = 2
ERROR_UPSTREAM_CLOSED = 3
ERROR_OPEN_FILE = 4
ERROR_SSL_HANDSHAKE = 5
ERROR_FILE_NOT_FOUND = 6
ERROR_BROKEN_HEADER = 7
ERROR_USERID_INVALID = 8
ERROR_RESOLVE = 9
ERROR_DIR_INDEX = 10
ERROR_UPSTREAM_READ_TIMEOUT = 11
ERROR_UPSTREAM_TIMEOUT = 12
ERROR_UPSTREAM_INVALID_HEADER = 13
ERROR_OPEN = 14
error_names[ERROR_USERID_SHORT] = "client sent too short userid cookie"
error_names[ERROR_LIMIT_REQ] = "limiting requests"
error_names[ERROR_RECV] = "recv() failed"
error_names[ERROR_UPSTREAM_CLOSED] = "upstream prematurely closed connection"
error_names[ERROR_OPEN_FILE] = "open() file failed"
error_names[ERROR_SSL_HANDSHAKE] = "peer closed connection in SSL handshake"
error_names[ERROR_FILE_NOT_FOUND] = "file not found"
error_names[ERROR_BROKEN_HEADER] = "broken header"
error_names[ERROR_USERID_INVALID] = "client sent invalid userid cookie"
error_names[ERROR_RESOLVE] = "could not be resolved"
error_names[ERROR_DIR_INDEX] = "directory index of"
error_names[ERROR_UPSTREAM_READ_TIMEOUT] = "upstream timed out while reading"
error_names[ERROR_UPSTREAM_TIMEOUT] = "upstream timed out while connecting"
error_names[ERROR_UPSTREAM_INVALID_HEADER] = "upstream sent no valid HTTP header"
error_names[ERROR_OPEN] = "open() failed"
error_patterns[ERROR_USERID_SHORT] = "client sent too short userid cookie"
error_patterns[ERROR_LIMIT_REQ] = "limiting requests"
error_patterns[ERROR_RECV] = "recv\\(\\) failed"
error_patterns[ERROR_UPSTREAM_CLOSED] = "upstream prematurely closed connection"
error_patterns[ERROR_OPEN_FILE] = "open\\(\\) \"[^\"]+\" failed \\([0-9]+: [^)]+\\)"
error_patterns[ERROR_SSL_HANDSHAKE] = "peer closed connection in SSL handshake"
error_patterns[ERROR_FILE_NOT_FOUND] = "\"[^\"]+\" is not found \\([0-9]+: [^)]+\\)"
error_patterns[ERROR_BROKEN_HEADER] = "broken header"
error_patterns[ERROR_USERID_INVALID] = "client sent invalid userid cookie"
error_patterns[ERROR_RESOLVE] = "could not be resolved"
error_patterns[ERROR_DIR_INDEX] = "directory index of"
error_patterns[ERROR_UPSTREAM_READ_TIMEOUT] = "upstream timed out \\([0-9]+: Connection timed out\\) while reading upstream"
error_patterns[ERROR_UPSTREAM_TIMEOUT] = "upstream timed out \\([0-9]+: Connection timed out\\) while connecting to upstream"
error_patterns[ERROR_UPSTREAM_INVALID_HEADER] = "upstream sent no valid HTTP/[0-9.]+ header while reading response header from upstream"
error_patterns[ERROR_OPEN] = "open\\(\\) failed"
for (i = 0; i < 15; i++) {
counts[error_names[i]] = 0;
}
counts["unknown"] = 0;
}
{
matched = 0;
for (i = 0; i < 15; i++) {
if ($0 ~ error_patterns[i]) {
counts[error_names[i]]++;
matched = 1;
break;
}
}
if (!matched) {
counts["unknown"]++;
printf("%s\n", $0);
}
}
END {
print "=== Nginx Error Counts ===";
PROCINFO["sorted_in"] = "@val_num_desc";
for (pattern in counts) {
printf("%s: %d\n", pattern, counts[pattern]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment