103

Is it possible to get which conf the nginx is using only from a running nginx process?

To get the conf file path. sometimes ps aux reveal it, sometimes it doesn't. It might be just something like nginx: master process /usr/sbin/nginx (same as /proc/PID/cmdline)

  1. So is nginx -V the only solution?
  2. From this question, is it possible to dump conf data structure from nginx process directly? Or at least dump the conf file path?
Community
  • 1
  • 1
est
  • 11,429
  • 14
  • 70
  • 118
  • What's the scenario here? Why do you want to do this? – willglynn Oct 11 '12 at 05:26
  • 2
    I am getting my hands managing servers left by other admins. I want to automate these things. It's good for collecting data in a batch. – est Oct 11 '12 at 07:32

1 Answers1

129

As of Nginx 1.9.2 you can dump the Nginx config with the -T flag:

-T — same as -t, but additionally dump configuration files to standard output (1.9.2).

Source: http://nginx.org/en/docs/switches.html

This is not the same as dumping for a specific process. If your Nginx is using a different config file, check the output for ps aux and use whatever it gives as the binary, e.g. if it gives something like

nginx: master process /usr/sbin/nginx -c /some/other/config

you need to run

/usr/sbin/nginx -c /some/other/config -T

If you are not on 1.9.2 yet, you can dump the config with gdb:

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
  • 3
    If the config file output is long, you can dump it to a file: `sudo nginx -T > nginx-config.txt` – Daniel Waltrip Feb 09 '18 at 18:56
  • 2
    Awesome! I was able to recover my accidentally-overwritten site config file thanks to this! :D – jdunk Mar 21 '18 at 06:08
  • 63
    `-T` will NOT dump the configuration for the running process, it's shows just the configuration read from all NGINX config files, main file and all includes e.g. site settings. You need to use GDB (from the link above) to get the current loaded config. – goetz Feb 22 '19 at 18:37