Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Apparently, I shouldn't have spent sleepless night trying to debug an application. I wanted to restart my nginx and discovered that its config file is empty. I don't remember truncating it, but fat fingers and reduced attention probably played their part.

I don't have backup of that config file. I know I should have made it.

Good for me, current nginx daemon is still running. Is there a way to dump its configuration to a config file that it'll understand later?

share|improve this question
up vote 31 down vote accepted

You need a gdb installed to dump memory regions of running process.

# Set pid of nginx master process here

# generate gdb commands from the process's memory mappings using awk
cat /proc/$pid/maps | awk '$6 !~ "^/" {split ($1,addrs,"-"); print "dump memory mem_" addrs[1] " 0x" addrs[1] " 0x" addrs[2] ;}END{print "quit"}' > gdb-commands

# use gdb with the -x option to dump these memory regions to mem_* files
gdb -p $pid -x gdb-commands

# look for some (any) nginx.conf text
grep worker_connections mem_*
grep server_name mem_*

You should get something like "Binary file mem_086cb000 matches". Open this file in editor, search for config (e.g. "worker_connections" directive), copy&paste. Profit!

Update: This method isn't entirely reliable. It's based on assumption that nginx process will read configuration and don't overwrite/reuse this memory area later. Master nginx process gives us best chances for that I guess.

share|improve this answer
Thanks, but this is too hardcore for me. I will rewrite the config file from scratch :) – Sergio Tulentsev Feb 18 '12 at 14:03
An mmap'd file implies an ope filehandle. See for an easier way to recover one: – Jeff Ferland Mar 31 '12 at 19:03
@JeffFerland nginx don't keep open fd for configuration file. – kupson Apr 1 '12 at 17:11
Yeah, most applications don't... thought I edited my comment after to include mention. Oops. – Jeff Ferland Apr 1 '12 at 17:47
Awesome! This works for me. Thank you:-) – yegle May 25 '12 at 1:45

The ngx_conf_t is a type of a structure used for configuration parsing. It only exists during configuration parsing, and obviously you can't access it after configuration parsing is complete.

share|improve this answer
It's 'obviously' inaccessible just because apparently there's no such facility implemented in nginx; other programs have such facilities, such as postconf -n for Postfix or exim -bP for Exim or (the badly named) testparm -v for Samba, etc. – Josip Rodin Nov 26 '15 at 10:54

the ideal way is to look for the ngx_conf_t struct from nginx process image.

It is defined here

My C & gdb sucks so home someone else could come up with a solution.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.