10

When you accidentally delete or nginx.conf file is lost, and nginx is in a live and running state , in this case we can get the configuration file in the memory

1. Get the nginx process pid

implement

ps aux | grep nginx

You will get the following output, find the pid of master

[root@VM-8-3-centos /]# ps aux | grep nginx
root      6958  0.0  0.0 112812   968 pts/0    R+   09:10   0:00 grep --color=auto nginx
root     19193  0.0  0.3 109440  6360 ?        Ss   Jun08   0:00 nginx: master process nginx
root     22326  0.0  0.3 112200  7412 ?        S    01:47   0:01 nginx: worker process

As shown above, the main process pid of nginx is 19193

2. Find the memory map

Next we need to check which memory mappings the process is using

sudo cat /proc/19193/maps | grep heap
[root@VM-8-3-centos /]# sudo cat /proc/19193/maps | grep heap
5581bf774000-5581bf8b7000 rw-p 00000000 00:00 0                          [heap]
5581bf8b7000-5581bfa50000 rw-p 00000000 00:00 0                          [heap]

You can see that there are 2 places, we only need to pay attention to the heap part. Memory is located 5581bf774000 - 5581bf8b7000 and 5581bf8b7000 - 5581bfa50000 between.

3. Dump the heap

Then you need to dump the heap. Make sure that gdb is installed. Connect to process using

sudo gdb -p 19193

You will get a (gdb) prompt. Now use the address we wrote down under this prompt, and you need to add 0x

(gdb) dump memory /tmp/nginx-memory 0x5581bf774000 0x5581bf8b7000

4. Get string data from dump

Now our dump is available /tmp/nginx-memory , and now we need to get string data from it

sudo strings  /tmp/nginx-memory > /tmp/nginx-memory.str

5. Find Nginx configuration

Now there is a memory dump. Most configurations will have a http { , now you can test /tmp/nginx-memory.str

grep -A 20 "http {" /tmp/nginx-memory.str
[root@VM-8-3-centos /]# grep -A 20 "http {" /tmp/nginx-memory.str
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    gzip on;
    # 
gzip
    gzip_min_length 1k;
    # gzip 
1-10
    gzip_comp_level 6;
    # 
javascript
    # 
 mime.types 
    gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
    # 
http header
Vary: Accept-Encoding
    gzip_vary on;
    # 

Modify the format yourself and it will be OK

6. /tmp/nginx-memory.str to the local

Put it into the editor to view, you will find the previous configuration, and also pay attention to the format

WX20210922-091703.png


雾岛听风
12.1k 声望8.6k 粉丝

丰富自己,胜过取悦别人。