Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restore Current sites-available File

Tags:

nginx

My team was trying to update nginx config, sites-available/mysite.com, when he was trying to save it suddenly the file can not be saved, due to the disk size problem and now the file is empty (size is 0). Is there any way to create a new config file based on current nginx conf? The nginx server haven't restarted yet since the file is edited, hence, it still works fine and we don't have the latest backup of sites-available/mysite.com file, thus I prefer to restore current config which exist in the memory.

Thank you.

like image 648
antonifs Avatar asked Aug 24 '17 02:08

antonifs


1 Answers

First of all before attempting this make sure now you have enough space free on your server and you will need to have sudo rights as well.

Finding the main nginx process

The first thing you need is to find the main process of nginx

$ ps aux | grep nginx
root      3890  0.0  0.1 124972  1424 ?        Ss   06:06   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data  3891  0.0  0.3 125332  3128 ?        S    06:06   0:00 nginx: worker process

As you can see my main nginx process is 3890 pid.

Finding the memory map Next we need to check what memory maps the process is using

$ sudo cat /proc/3890/maps
55698ed20000-55698ee2e000 r-xp 00000000 fc:00 414469                     /usr/sbin/nginx
55698f02e000-55698f030000 r--p 0010e000 fc:00 414469                     /usr/sbin/nginx
55698f030000-55698f04c000 rw-p 00110000 fc:00 414469                     /usr/sbin/nginx
55698f04c000-55698f06c000 rw-p 00000000 00:00 0
556990d72000-556990dd5000 rw-p 00000000 00:00 0                          [heap]
7f4cfa551000-7f4cfa55c000 r-xp 00000000 fc:00 1314482                    ....
7f4d01f70000-7f4d01f71000 rw-p 00000000 00:00 0
7ffde6f5e000-7ffde6f7f000 rw-p 00000000 00:00 0                          [stack]
7ffde6f9a000-7ffde6f9c000 r--p 00000000 00:00 0                          [vvar]
7ffde6f9c000-7ffde6f9e000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]

Now there will be a lot of output, but don't worry we mainly interested in the [heap] section. The memory is located at 556990d72000-556990dd5000.

Dumping the heap

Now you need to dump the heap. Make sure you have gdb installed. Connect to the process using

$ sudo gdb -p 3890

You will get a (gdb) prompt. Now on this prompt use the addresses that we noted earlier

(gdb) dump memory /tmp/nginx-memory 0x556990d72000 0x556990dd5000

Getting string data out of dump

Now our dump is available at /tmp/nginx-memory, now we need to get string data out of it

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

Finding the Nginx Config

Now you have a memory dump. Most configs will have a http { line and now you can test your /tmp/nginx-memory.str for the same

$ grep -A 20 "http {" /tmp/nginx-memory.str
http {
    # Basic Settings
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;
    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    # SSL Settings
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;
    # Logging Settings
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    # Gzip Settings
    gzip on;
    gzip_disable "msie6";
--
http {
    # Basic Settings
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;
    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    # SSL Settings
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;
    # Logging Settings
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    # Gzip Settings
    gzip on;
    gzip_disable "msie6";

So look into this file carefully and recover you config and create a new one

like image 73
Tarun Lalwani Avatar answered Oct 15 '22 18:10

Tarun Lalwani