Rootop 服务器运维与web架构

2019-10-31
发表者 Venus
解决inode使用100%已关闭评论

解决inode使用100%

# 从根目录开始确定哪个目录文件多

for i in /*; do echo $i; find $i | wc -l; done

# 根据最多的目录,进入,再定位子目录,确定哪个目录占的多

for i in ./*; do echo $i; find $i | wc -l; done

# 根据时间来删除(7天前的),防止出现 Argument list too long 问题

find .  -ctime +7 -name "*" | xargs -i rm -rf {}

如果提示Argument list too long ,就把7加大。
或者用rsync同步并删除实现
参考:https://www.rootop.org/pages/4316.html

2019-10-28
发表者 Venus
nginx中if和location优先级问题已关闭评论

nginx中if和location优先级问题

问题:nginx要实现代理wss访问(websocket secure),但是配置以后测试提示404,配置如下:
# nginx配置

server
{
	#略过部分配置

    location /wss {
		proxy_pass http://127.0.0.1:8202;
		proxy_http_version 1.1;
		proxy_set_header Upgrade $http_upgrade;
		proxy_set_header Connection "Upgrade";
		proxy_set_header X-Real-IP $remote_addr;
		proxy_read_timeout  84000;
	}
	
	if (!-e $request_filename) {
		rewrite ^/(.*)$ /index.php/$1 last;
		break;
	}
}

提示404也就是未匹配到location,发现是下面if段的问题。
把配置改为如下:

server
{
	#略过部分配置
	
    location / {
      if (!-e $request_filename) {
          rewrite ^/(.*)$ /index.php/$1 last;
          break;
      }
    }
	
    location /wss {
		proxy_pass http://127.0.0.1:8202;
		proxy_http_version 1.1;
		proxy_set_header Upgrade $http_upgrade;
		proxy_set_header Connection "Upgrade";
		proxy_set_header X-Real-IP $remote_addr;
		proxy_read_timeout  84000;
	}
	
}

这样才实现访问到location /wss {} 段。
查资料说if相当于在内部实现一个location,如果匹配了,只会执行这个内部的动作。
换句话说,if和location同级的时候,if优先级高于location。
所以把if(){}放到location / {}中后问题解决。

2019-10-27
发表者 Venus
man命令查看系统调用方法已关闭评论

man命令查看系统调用方法

# 根据资料输入命令可以查看系统调用的方法描述,但是提示没有帮助文档。

[root@VM_0_8_centos ~]# man 2 read
No manual entry for read in section 2

# 原因:默认只能看第一区段,其他区段需要再安装。

# 所有的区段:
区段1:用户指令
区段2:系统调用
区段3:程序库调用
区段4:设备
区段5:文件格式
区段6:游戏
区段7:杂项
区段8:系统指令
区段9:内核内部指令
区段n:Tcl或Tk指令

# 安装所有区段 (英文版)

yum install man-pages -y

# 中文手册,相当于安装汉化包,需要系统支持中文

yum install man-pages-zh-CN -y

# 系统中文支持安装(centos7)

yum install kde-l10n-Chinese

# 临时设置系统中文显示

export LANG=zh_CN.UTF-8

2019-10-10
发表者 Venus
The ‘INFORMATION_SCHEMA.SESSION_VARIABLES’ feature is disabled; see the documentation for ‘show_compatibility_56’已关闭评论

The ‘INFORMATION_SCHEMA.SESSION_VARIABLES’ feature is disabled; see the documentation for ‘show_compatibility_56’

在用MySQL-Front时报错。


The ‘INFORMATION_SCHEMA.SESSION_VARIABLES’ feature is disabled; see the documentation for ‘show_compatibility_56’

原因:
从mysql5.7.6开始 information_schema.global_status 已经开始被舍弃(表不存在),为了兼容性,此时需要打开show_compatibility_56

临时打开:
进入mysql命令行执行:

MySQL> set global show_compatibility_56=on;
MySQL> show variables like '%show_compatibility_56%';

永久打开:
修改mysql配置文件my.ini/my.cnf
在 [mysqld]段添加一行

show_compatibility_56 = 1

重启MySQL生效。

2019-08-30
发表者 Venus
nginx日志转为json格式已关闭评论

nginx日志转为json格式

# nginx配置文件

# 手动拼成json格式
log_format log_json
'{'
	'"remote_addr":"$remote_addr",'
	'"x-forwarded-for":"$http_x_forwarded_for",'
	'"remote_user":"$remote_user",'
	'"time_local":"$time_local",'
	'"request":"$request",'
	'"status":"$status",'
	'"body_bytes_sent":"$body_bytes_sent",'
	'"http_referer":"$http_referer",'
	'"user_agent":"$http_user_agent"'
'}';

# 通过内置参数实现,需要nginx版本 >= 1.11.8
# 官方文档:http://nginx.org/en/docs/http/ngx_http_log_module.html#log_format
log_format log_json2 escape=json
'{'
	'"remote_addr":"$remote_addr",'
	'"x-forwarded-for":"$http_x_forwarded_for",'
	'"remote_user":"$remote_user",'
	'"time_local":"$time_local",'
	'"request":"$request",'
	'"status":"$status",'
	'"body_bytes_sent":"$body_bytes_sent",'
	'"http_referer":"$http_referer",'
	'"user_agent":"$http_user_agent"'
'}';

server
{
	listen 80;
	root html;
	access_log /tmp/access.log log_json2;
}

# 浏览器请求地址
http://192.168.10.17/index.php?method=\add&id=1

# 不加 escape=json 参数日志打印

{"remote_addr":"192.168.1.139","x-forwarded-for":"-","remote_user":"-","time_local":"30/Aug/2019:14:28:46 +0800","request":"GET /index.php?method=\x5Cadd&id=1 HTTP/1.1","status":"404","body_bytes_sent":"555","http_referer":"-","user_agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3314.0 Safari/537.36 SE 2.X MetaSr 1.0"}

# 添加 escape=json 参数日志打印

{"remote_addr":"192.168.1.139","x-forwarded-for":"","remote_user":"","time_local":"30/Aug/2019:14:28:24 +0800","request":"GET /index.php?method=\\add&id=1 HTTP/1.1","status":"404","body_bytes_sent":"555","http_referer":"","user_agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3314.0 Safari/537.36 SE 2.X MetaSr 1.0"}

可以看到x-forwarded-for和request两个字段是不同的。
日志如果获取不到调用变量的值,则用 横杠 “-” 做为值,添加了escape=json参数则变为空字符串。
反斜杠 “\” 变为 “\x5C”,添加了escape=json参数则自动转义,前面又添加了个反斜杠 “\\”

之前手动拼出来的json格式,在python里解析时候就报错,知道是转义问题,但是不知道怎么解决,今天发现支持内部转义。
这样json格式便于日志分析处理。