DaXin Space

DaXin Space


Record the operation of recovering Nginx configuration from memory.


When maintaining their own component pages in the company, in order to conveniently debug on the server, the Nginx.conf was edited. After a lot of effort, Nginx was finally running, and out of habit, some temporary files were to be deleted. However, there was an accidental slip of the hand, and the command "rm -rf nginx*" was executed, deleting all the Nginx configurations on the server. Moreover, there was no backup locally and it was not pushed to GIT either. Fortunately, there were experts online who helped recover everything. Hereby, I record the process.


  1. Install gdb by running the command: yum install gdb. After installing gdb, find the process ID of the Nginx master.
  2. Execute the following commands:
# 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_*
  1. Finally, the command grep server_name mem_* outputs the files that contain the "server_name" keyword.
[root@centos]# grep server_name mem_*
Matched binary file mem_558f03f58000
Matched binary file mem_558f0416f000
  1. After downloading the files, open them with Visual Studio Code (do not use Sublime or similar editors as they will display garbled text due to the files being binary). Perform a global search, and you will be able to see the familiar configuration information.
  2. The approximate starting position of the file can be located using the keyword "http {".
  3. Although it cannot be directly copied and pasted for use, it can still be recovered. It serves as a lesson for being careless. QAQ


Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.