恢复当前站点可用的文件 [英] Restore Current sites-available File
问题描述
我的团队正在尝试更新 nginx 配置,sites-available/mysite.com
,当他尝试保存时突然无法保存文件,由于磁盘大小问题,现在文件为空(大小为 0).有没有办法根据当前的 nginx conf 创建一个新的配置文件?由于文件被编辑,nginx 服务器还没有重新启动,因此,它仍然可以正常工作,而且我们没有 sites-available/mysite.com
文件的最新备份,因此我更喜欢恢复内存中存在的当前配置.
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.
谢谢.
推荐答案
首先,在尝试此之前,请确保您的服务器上现在有足够的可用空间,并且您还需要具有 sudo 权限.
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.
找到nginx主进程
首先需要找到nginx的主进程
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
如你所见,我的主要 nginx 进程是 3890
pid.
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]
现在会有很多输出,不过不用担心我们主要对[heap]
部分感兴趣.内存位于556990d72000-556990dd5000
.
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
.
转储堆
现在您需要转储堆.确保你已经安装了 gdb
.使用
Now you need to dump the heap. Make sure you have gdb
installed. Connect to the process using
$ sudo gdb -p 3890
你会得到一个 (gdb)
提示.现在在这个提示上使用我们之前记下的地址
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
从转储中获取字符串数据
现在我们的转储在 /tmp/nginx-memory
可用,现在我们需要从中获取字符串数据
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
查找 Nginx 配置
现在你有一个内存转储.大多数配置都会有一个 http {
行,现在您可以测试您的 /tmp/nginx-memory.str
是否相同
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
这篇关于恢复当前站点可用的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!