Problem: Retrieving Nginx Configuration
Getting the configuration of a running Nginx process can be difficult. You may need to do this when you troubleshoot, audit, or copy server setups. However, it's not clear how to get this information from an active Nginx instance.
Methods to Dump Nginx Configuration
Using the Nginx -T Flag (Nginx 1.9.2 and Later)
Nginx version 1.9.2 and later include a -T flag. This flag dumps the configuration files to standard output. It works like the -t flag, which tests the configuration file, but also shows the content.
To use the -T flag, run this command:
nginx -T
This command will show the full Nginx configuration, including any included files.
If your Nginx uses a custom configuration file, you may need to specify it. For example, if ps aux shows:
nginx: master process /usr/sbin/nginx -c /some/other/config
You would run:
/usr/sbin/nginx -c /some/other/config -T
This command will dump the configuration from the specified file.
Tip: Redirect Output to a File
To save the configuration dump to a file for later analysis, you can redirect the output:
nginx -T > nginx_config_dump.txt
This creates a text file with the full configuration, which you can review or share with others for troubleshooting.
Alternative Method: Using GDB (For Older Nginx Versions)
For Nginx versions older than 1.9.2, you can use the GNU Debugger (GDB) to dump the configuration. This method is more complex and requires care.
To use GDB:
-
Find the Nginx master process ID:
ps aux | grep "nginx: master process"
-
Attach GDB to the process:
sudo gdb -p [MASTER_PROCESS_ID]
-
In GDB, run:
call ngx_http_conf_dump_config()
-
Detach from the process:
detach
-
Quit GDB:
quit
Be careful when using GDB, as it can affect the running process. It's best to use this method in a non-production environment or during maintenance windows.