Rootop 服务器运维与web架构

2019-05-06
发表者 Venus
关于php-fpm配置最大并发已关闭评论

关于php-fpm配置最大并发

# 子进程管理方式。 static 静态,dynamic 动态调整
pm = static

# 最大子进程
pm.max_children = 50
pm.start_servers = 5 # 只针对pm = dynamic时有效
pm.min_spare_servers = 5 # 只针对pm = dynamic时有效
pm.max_spare_servers = 35 # 只针对pm = dynamic时有效

如,当 pm = static ,pm.max_children = 50时,启动php-fpm。

[root@localhost php-fpm.d]# ps aux | grep php-fpm | grep -vE "master|grep" | wc -l
50

通过ps可以看到有50个php的子进程,也就意味着php一启动就创建50个进程(固定值)。

当 pm = dynamic 时,其它值不变。启动php

[root@localhost php-fpm.d]# ps aux | grep php-fpm | grep -vE "master|grep" | wc -l
5

通过ps可以看到有5个php的子进程,也就意味着php一启动就创建5个进程(pm.start_servers)。
随着请求访问而变化,但最高不超过pm.max_children的值。

2019-05-05
发表者 Venus
配置linux尽可能使用物理内存已关闭评论

配置linux尽可能使用物理内存

有台测试机zabbx最近一直报交换分区不足,查看内存cache用了近20G,想配置系统尽量使用物理内存。通过配置系统参数swappiness可以实现。

redhat性能调整参考:https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/performance_tuning_guide/sect-red_hat_enterprise_linux-performance_tuning_guide-configuration_tools-configuring_system_memory_capacity

swappiness
The swappiness value, ranging from 0 to 100, controls the degree to which the system favors anonymous memory or the page cache. A high value improves file-system performance while aggressively swapping less active processes out of RAM. A low value avoids swapping processes out of memory, which usually decreases latency at the cost of I/O performance. The default value is 60.
简单意思就是swappiness值越小,则尽可能利用物理内存,值越大,则用交换分区。默认值60。
官方文档也没看到说默认值60是指已用还是剩余60%的内存。

官方有个警告提醒:
Setting swappiness==0 will very aggressively avoids swapping out, which increase the risk of OOM killing under strong memory and I/O pressure.
设置为0的话,则非常积极的使用物理内存,但容易出现OOM的风险。

个人理解设置为0,”关闭” 使用交换分区,设置为100,积极使用交换分区。

# 2种方法查看当前值

[root@hub ~]# cat /proc/sys/vm/swappiness
30

[root@hub ~]# sysctl -q vm.swappiness
vm.swappiness = 30

# 修改默认值:

[root@localhost ~]# vi /etc/sysctl.conf 追加一行
vm.swappiness = 10
保存退出,执行。
[root@localhost ~]# sysctl -p
[root@localhost ~]# sysctl -q vm.swappiness
vm.swappiness = 10

执行完交换分区使用率不会立即释放,可能会由系统自动调整。
建议重启下机器,或者在业务上线前进行配置。

2019-04-30
发表者 Venus
在jenkins shell脚本中获取提交者信息已关闭评论

在jenkins shell脚本中获取提交者信息

jenkins中可以传递给shell脚本的变量:http://YOUR_JENKINS_ADDRESS/env-vars.html/ 从这个地址可以看到。
本来想取GIT_COMMITTER_NAME、GIT_AUTHOR_NAME就是取不到,咱也不敢说为什么,也不敢问。

变通路子,从拉下来的git仓库目录里执行git命令查。
https://git-scm.com/docs/git-show 这里可以看到git show支持的参数。

#/bin/bash
username=`git show -s --pretty=%an`
pushtime=`git show -s --pretty=%aD`
echo "分支:" ${GIT_BRANCH} # 这个直接从jenkins变量中取。
echo "推送者:" $username
echo "时间:" $pushtime

这样才算取到提交者信息。

2019-04-30
发表者 Venus
nginx中关于error_page配置参数介绍已关闭评论

nginx中关于error_page配置参数介绍

# 当访问文件不存在返回404状态码,由nginx内部重定向返回/404.html的内容。

server {    
.

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html; # 支持多个状态码重定向到同一个地址。
.
}

# 如果写一个外部地址,则会发生302跳转。

server {    
.

    error_page 404 https://www.rootop.org;
    
    #error_page 404 = @fallback; # 这里也可以写内部重定向(location @name 命名位置)
    #location @fallback {
    #    proxy_pass http://backend;
    #}
.
}

# 关于location @name 资料看官方:http://nginx.org/en/docs/http/ngx_http_core_module.html#location
The “@” prefix defines a named location.
Such a location is not used for a regular request processing, but instead used for request redirection.
They cannot be nested, and cannot contain nested locations.

#当nginx配置为FastCGI(如lnmp)发生500错误时进行跳转的时候添加如下:

error_page 500 https://www.rootop.org/500.jpg;

会发现浏览器还是直接显示了500错误,并没有跳转到500.jpg(302)。

这就需要在fastcgi部分添加一行配置 fastcgi_intercept_errors on;

location ~ .*\.(php|php5)?$
{
    try_files $uri =404;
    fastcgi_pass  unix:/tmp/php-cgi.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
    fastcgi_intercept_errors on;
}

参数作用参考:http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_intercept_errors
意思就是fastcgi服务器发生大于等于300的错误码时,交由nginx的error_page指令处理后返给客户端。
所以在lnmp环境中,如果配置了error_page指令没有效果,就去查有没有这个参数。

2019-04-29
发表者 Venus
nginx中try_files作用及location @作用已关闭评论

nginx中try_files作用及location @作用

# nginx 配置文件:

server {

    listen 80;
    server_name  api.xxx.com;

    root /mnt/try;

    location / {
        add_header  Content-Type 'text/html; charset=utf-8';
        #echo $uri;
        try_files $uri @default;
    }

    location @default {
        root /mnt/default;
    }
}

location @xxx解释:定义一个location段,不能被外部请求所访问,只能用于nginx内部配置指令使用,比如 try_files、error_page。
$uri解释:URI代表资源的名称

浏览器访问 http://api.xxx.com/abc/index.html 时,当前的$uri值为/abc/index.html

可以通过编译nginx参数--add-module添加第三方echo模块打印$uri

echo模块地址:https://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gz

# try_files作用:
try_files会先尝试去/mnt/try目录下找abc目录下的index.html,如果有,直接返回,没有的话则跳转到@default部分(内部重定向)。
在default部分会去/mnt/default目录下找abc目录下的index.html,有,直接返回,没有就返回404错误。

try_files可以理解为实现rewrite的作用。