Rootop 服务器运维与web架构

2019-05-13
发表者 Venus
nginx利用ngx_http_limit_conn_module模块限制连接数已关闭评论

nginx利用ngx_http_limit_conn_module模块限制连接数

以前介绍过这个模块,再拿出来研究下:https://www.rootop.org/pages/3355.html

ngx_http_limit_conn_module模块默认编译,不需要单独参数。
官方文档:http://nginx.org/en/docs/http/ngx_http_limit_conn_module.html

http {
    # 以客户端ip做为标识,在内存中设置一块区域名为addr,内存大小为10M的空间
    limit_conn_zone $binary_remote_addr zone=addr:10m;

    # 以server_name做为标识,设置大小为10M名字为perserver的空间
    limit_conn_zone $server_name zone=perserver:10m;

    server {

        # limit_conn 参数的有效配置段 http, server, location
        location / {
            limit_conn addr 1; # 设置同一个ip同一时间只允许建立1个连接。
            limit_conn perserver 100; # 设置当前虚拟主机最大连接数(注意不是最大ip连接数)
            limit_rate 100k; # 限速,特别是下载资源时对每一个连接限速到100kb。
            proxy_pass http://127.0.0.1:8080;
        }
    }
}

 

注意:

limit_rate指令属于ngx_http_core_module模块,参考:http://nginx.org/en/docs/http/ngx_http_core_module.html#limit_rate
有个静态页,比如index.html中引用了3张图片

<html>
<body>
    <img src="./img/1.jpg">
    <img src="./img/2.jpg">
    <img src="./img/3.jpg">
</body>
</html>

那么在浏览器访问的时候,有一定的几率某张图片加载不出来,通过chrome 调试可以看到这个资源出现503错误,
这个错误就是limit_conn addr 1;限制连接数生效了,1张图、一个js、css都是一个连接,所以此配置的值需要根据页面有多少个资源来算出。
之所以说有一定几率出现,而不是百分百,个人推测受下面因素影响。
1、客户端到服务器的网络情况
2、服务器对请求资源的处理速度
3、nginx处理请求头的速度

注意官方文档中的一句话:
Not all connections are counted.
A connection is counted only if it has a request being processed by the server and the whole request header has already been read.
并非所有连接都被计算在内,只有当服务器正在处理请求并且下一个请求已经读取了整个请求头时,才会计算连接。

In HTTP/2 and SPDY, each concurrent request is considered a separate connection.
(这句没做测试,没了解 SPDY,http/2资料说一个页面中的资源都由同一个连接发出请求)

另外如果要实现限制某个资源目录download下,比如 location /download { },可以在此段里设置 limit_conn addr 1;和limit_rate 100k;参数,这样就可以针对某个目录设置连接数及速率,否则大部分浏览器或者下载工具为了提高下载速度,会发起多个并发下载,导致下载速度没有限制住。可以用chrome浏览器测试,或者IE。或者限制单IP限制为1个并发(limit_conn addr 1;)连接就可以测出来效果。

2019-05-10
发表者 Venus
linux下文件夹及文件的隐藏属性已关闭评论

linux下文件夹及文件的隐藏属性

# 案例

[root@localhost ~]# mkdir test
[root@localhost ~]# chattr +i test # 禁止修改、重命名、删除属性
[root@localhost ~]# lsattr -d test # -d 列出目录的隐藏属性
----i----------- test

# chattr参数:

-R 递归 包括文件夹及文件 
-V Be verbose with chattr's output and print the program version.
-f 抑制大部分错误信息

# chattr属性操作符,一般常用+和-:

'+' 添加文件或文件夹属性 
'-' 删除属性 

更多帮助可参考 man chattr

常用属性基本是”a”、”i”

a # 只允许对文件进行 echo xxx >> fileName 这种方式追加内容,不允许其它方式写、删除、修改。
i # 什么都不允许,只能看

# 查看文件,或文件夹的隐藏属性
查看文件夹

[root@localhost ~]# lsattr -d test/
-----a---------- test/

查看文件

[root@localhost test]# lsattr a
-----a---------- a

当操作一个带有a或i隐藏属性的文件或文件夹是会提示 操作被拒绝

[root@localhost test]# rm -f a
rm: cannot remove ‘a’: Operation not permitted

只有root用户才能设置这个权限,设置权限以后包括root都不能删除文件。只能去掉权限后才操作。

2019-05-10
发表者 Venus
shell读取yaml配置文件的值已关闭评论

shell读取yaml配置文件的值

目的:在代码提交自动打包时,先检测application.yml中spring.profiles.active的值是否为test(加载测试环境配置文件),如果不是则取消打包。

PS:
当然也可以在jar包启动时通过命令指定测试环境配置文件

--spring.profiles.active=test

会覆盖代码中的值,命令行优先级高于代码中配置。

# 首先pip安装py中的一个包
$ pip install shyaml

windows安装后是个exe程序
linux安装后是python脚本

# a.yaml 示例

fx:
  url: http://192.168.10.16:8080/

# 获取url值:

$ cat a.yml | shyaml.exe get-value fx.url
http://192.168.10.16:8080/

这样配合其他脚本可以实现某些操作。

2019-05-09
发表者 Venus
nginx通过geoip2模块实现判断用户来源国家跳转中英站已关闭评论

nginx通过geoip2模块实现判断用户来源国家跳转中英站

ip数据库文件下载地址:https://dev.maxmind.com/geoip/geoip2/geolite2/

编译nginx添加第二代geoip2模块,第一代自带 的–with-http_geoip_module 生命周期算是尾声了。ip地址库都下载不到了。

# 下载独立的geoip2模块,官方nginx还未加入

git clone https://github.com/leev/ngx_http_geoip2_module

# 编译nginx加上geoip2

--add-module=../ngx_http_geoip2_module

报错:
./configure: error: the geoip2 module requires the maxminddb library.
解决:需要安装 libmaxminddb 库,这个是用来读取ip数据文件的。

下载 https://github.com/maxmind/libmaxminddb/releases/download/1.3.2/libmaxminddb-1.3.2.tar.gz
解压依次执行:

./configure
make
make install
echo /usr/local/lib  >> /etc/ld.so.conf.d/local.conf
ldconfig

即可。

重新编译nginx通过。

# 编辑nginx配置文件

http {
    # 只提取关键配置参数
    
    geoip2 /usr/local/GeoLite2-Country_20190507/GeoLite2-Country.mmdb {
        $geoip2_data_country_code default=CN country iso_code;
    }

    server {
        add_header "country" $geoip2_data_country_code; #添加个响应头,方便查。
        
        location / {
            if ($geoip2_data_country_code = CN) {
                root /mnt/cn;
            }
            
            if ($geoip2_data_country_code != CN) {
                root /mnt/other;
            }
        }
    }
}

访问测试,查看响应头中的country字段。

$geoip2_data_country_code 变量是自定义名称的变量,其值是在geoip2解析ip后生成。
default=CN country iso_code 这些参数实际是下面命令返回json的key,default是为了查不到相关key的值时,设置一个默认值。

[root@localhost]# mmdblookup --file GeoLite2-City.mmdb --ip x.x.x.x

2019-05-08
发表者 Venus
apache上传大文件超时问题已关闭评论

apache上传大文件超时问题

apache服务器上传大文件超过21-22秒就提示503错误了。
开始怀疑是apache反向代理php-fpm的问题,找了几个参数也没解决。后来开启apache的debug模式才解决。

# 修改 httpd.conf 开启debug

LogLevel debug

重启apache。

找到error.log
# 看到这么一条日志

[Tue May 07 17:13:02.195708 2019] [reqtimeout:info] [pid 24671:tid 140592553060096] [client 192.168.1.147:11002] AH01382: Request body read timeout

判断是接收数据超时了。

官方文档:http://httpd.apache.org/docs/2.4/mod/mod_reqtimeout.html

# 修改http.conf 设置接收正文超时200秒

<IfModule reqtimeout_module>
    RequestReadTimeout body=200
</IfModule>

解决