• linux 误删nginx.conf文件恢复


    当你不小心误删或者错误操作导致nginx.conf文件丢失,而且nginx处于在住运行的状态,在这种情况下我们就可以在内存中获取配置文件。

    1.获取nginx进程pid

    ps -ef | grep nginx
    
    • 1

    你会得到如下输出,找到master的pid

    root     19812     1  0 7月14 ?       00:00:00 nginx: master process /usr/sbin/nginx
    nginx    19813 19812  0 7月14 ?       00:00:26 nginx: worker process
    nginx    19814 19812  0 7月14 ?       00:00:33 nginx: worker process
    nginx    19815 19812  0 7月14 ?       00:01:15 nginx: worker process
    nginx    19816 19812  0 7月14 ?       00:00:55 nginx: worker process
    nginx    19817 19812  0 7月14 ?       00:00:04 nginx: cache manager process
    root     26171 12446  0 12:56 pts/0    00:00:00 grep --color=auto nginx
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    如上所示,nginx的主要进程pid为19812

    2.查找内存映射

    安装gdb工具

    yum install gdb -y
    
    • 1

    接下来我们需要检查进程正在使用哪些内存映射

    cat /proc/19812/maps | grep heap
    
    • 1
    [root@loghub-server tmp]# cat /proc/19812/maps | grep heap
    55e0e6760000-55e0e69c2000 rw-p 00000000 00:00 0                          [heap]
    
    • 1
    • 2

    可以看到有2处地方,我们只需要关注heap部分。内存位于55e0e6760000-55e0e69c2000之间。

    3.转储堆

    然后需要转储堆

    gdb -p 19812
    
    • 1

    你会得到一个(gdb)提示。现在在这个提示下使用我们之前记下的地址,地址前需要加0x

    (gdb) dump memory /tmp/nginx-memory 0x55e0e6760000 0x55e0e69c2000
    
    • 1

    4.从转储中获取字符串数据

    strings  /tmp/nginx-memory > /tmp/nginx-memory.str
    
    • 1

    5.查找 Nginx 配置

    现在有了内存转储。大多数配置都会有http {一行,现在可以测试下/tmp/nginx-memory.str

    grep -A 20 "http {" /tmp/nginx-memory.str
    
    • 1
    [root@loghub-server tmp]# grep -A 50 "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"';
        access_log  /var/log/nginx/access.log  main;
        sendfile            on;
        tcp_nopush          on;
        tcp_nodelay         on;
        keepalive_timeout   65;
        types_hash_max_size 2048;
        include             /etc/nginx/mime.types;
        default_type        application/octet-stream;
        # Load modular configuration files from the /etc/nginx/conf.d directory.
        # See http://nginx.org/en/docs/ngx_core_module.html#include
        # for more information.
        include /etc/nginx/conf.d/*.conf;
        #proxy_temp_path  /etc/nginx/temp_dir;
        #proxy_cache_path /var/cache/nginx/cache levels=1:2 keys_zone=imgcache:100m inactive=1d max_size=1g;
        #proxy_cache_path /etc/nginx/cache_dir levels=1:2 keys_zone=imgcache:500m max_size=1g inactive=1d use_temp_path=off;
        proxy_cache_path /etc/nginx/conf.d/cache levels=1:2 keys_zone=my_zone:100m inactive=3600s max_size=1g;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    自己复制出来或者修改下格式之类的,就可以了。

  • 相关阅读:
    Java框架 SSM整合
    输出9*9口诀。
    VB6.0 设置窗体的默认焦点位置在 TextBox 中
    SpringCLoud——Eureka注册中心
    磁盘空间占用巨大的meta.db-wal文件缓存(tracker-miner-fs索引服务)彻底清除办法
    【Java】判断语句if.....&选择语句switch......
    HTML之如何下载网页中的音频(二)
    尼莫地平PEG-PLGA纳米粒|葛根素PEG-PLGA纳米粒(Pue-NPs)|负载替米沙坦PLGA纳米粒
    jmeter+nmon+crontab简单的执行接口定时压测
    关于Spring-Boot配置加载顺序解读
  • 原文地址:https://blog.csdn.net/dounine/article/details/125992451