Skip to content

Instantly share code, notes, and snippets.

@icy
Last active September 20, 2016 02:32
Show Gist options
  • Save icy/703eb4e362088b53f1e8 to your computer and use it in GitHub Desktop.
Save icy/703eb4e362088b53f1e8 to your computer and use it in GitHub Desktop.
See how nginx includes its files
#!/bin/bash
# Purpose:
# See how nginx loads/includes its configuration files
# Very useful to detect quickly where there errors come from.
# Author : Anh K. Huynh
# Date : 2015 Aug 14th
# License: MIT
# Notes :
# Some system has a different level. Here it's 3.
# If you see the "negative argument" error, use a lower number, e.g., (level-2).
# If you see some extra spaces before the first line, increase it, e.g, (level-4).
# This magic number may vary ;) I don't want to fix that
# because it simply makes the script complex.
# Ruby version
````
$ sudo strace -s 64 /path/to/your/nginx -t 2>&1 \
| grep '^open(' \
| grep /etc/nginx/ \
| ruby -n -e '
if gs = $_.match(/^open\("(.+)".*= ([0-9]+)$/) then
file, level = gs[1], gs[2].to_i;
puts "#{" " * 2 * (level - 3)}#{file}";
end
'
````
# Gawk version
````
$ sudo strace -s 64 nginx -t 2>&1 \
| grep '^open(' \
| grep /etc/nginx/ \
| gawk -vMAGIC=3 '{
n = match($0, /^open\("(.+)".*= ([0-9]+)$/,gs);
if (n) {
prefix = "";
for (i=0; i < gs[2] - MAGIC; i++) {
prefix = prefix " ";
};
printf("%s%s\n", prefix, gs[1]);
}
}'
````
# Example output
````
/etc/nginx/nginx.conf
/etc/nginx/mime.types
/etc/nginx/sites/00_default.txt
/etc/nginx/sites/00_openssl.txt
/etc/nginx/sites/00_cache_paths.txt
/etc/nginx/sites
/etc/nginx/sites/00_acharset.conf
/etc/nginx/sites/00_blacklist.conf
/etc/nginx/sites/blacklist
/etc/nginx/sites/blacklist/baidu.list
/etc/nginx/sites/00_headers.conf
/etc/nginx/sites/00_limit_reg.conf
/etc/nginx/sites/2005.viettug.org.conf
/etc/nginx/sites/00_exit.txt
/etc/nginx/sites/00_errors.txt
/etc/nginx/sites/jsmath.txt
/etc/nginx/sites/2008.viettug.org.conf
/etc/nginx/sites/00_exit.txt
/etc/nginx/sites/00_errors.txt
/etc/nginx/sites/jsmath.txt
/etc/nginx/sites/archlinuxvn.org.conf
/etc/nginx/sites/00_exit.txt
/etc/nginx/sites/00_errors.txt
/etc/nginx/sites/00_freezone.txt
/etc/nginx/sites/cachnhiet.net.conf
/etc/nginx/sites/00_exit.txt
````
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment