Debugging is an essential part of maintaining and optimizing your NGINX server. It provides detailed logs that can help you troubleshoot issues by giving you deeper insights into how requests are being processed.
1. Check if Your NGINX is Compiled with Debug Support
Before you can start using debug logs, you need to make sure that your NGINX installation supports debugging. This support must be enabled during the initial compilation of NGINX.
To check if NGINX was compiled with debugging support, run the following command:
[root@siddhesh ~]# nginx -V
nginx version: nginx/1.20.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-compat --with-debug --with-file-aio --with-google_perftools_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'
[root@siddhesh ~]#
Look for --with-debug in the output. If you see this option, your NGINX is ready to support debug logging. If it’s not there, you’ll need to either recompile NGINX with the --with-debug flag or use a pre-built version that includes it.
2. Setting Up Debug Logging
Once you've confirmed that your NGINX can support debugging, the next step is to set the log level to debug. This is done by modifying the error_log directive in your NGINX configuration file.
Open your NGINX configuration file (usually located at /etc/nginx/nginx.conf or a similar path) and set the error log level to debug like this:
[root@siddhesh ~]# head -n 5 /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log debug;
pid /run/nginx.pid;
[root@siddhesh ~]#
This line tells NGINX to write detailed debugging information to the specified log file.
3. Debugging Logs for Specific Clients
If you only want to enable debugging logs for specific client IP addresses, you can do so using the debug_connection directive. This can be useful when you only need to troubleshoot issues for certain clients.
[root@siddhesh ~]# head -n 13 /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
debug_connection 192.168.1.1;
debug_connection 172.10.1.0/24;
}
[root@siddhesh ~]#
In this example, debug logs will only be generated for requests coming from the specified IP addresses or subnet i.e 192.168.1.1 & 172.10.1.0/24
Conclusion
Enabling debug logging in NGINX is a powerful way to gain insights into your server’s operations and troubleshoot issues more effectively. Following these steps will help you set up and manage debug logging with ease. Just remember to disable or reduce the logging level once you’re done debugging to avoid unnecessary performance overhead.
コメント