zoukankan      html  css  js  c++  java
  • [效果不错] nginx 高并发参数配置及linux内核参数优化,完整的内核优化设置。PHP-FPM高负载解决办法。

    背景:对vps小资源的实践中对,https://justwinit.cn/post/7536/ 的再优化,再实践,再优化,特别是Nginx,PHP,内核:

    零)Nginx:

    error_log /data/logs/nginx_error.log notice;

    #Specifies the value for maximum file descriptors that can be opened by this process.

    worker_rlimit_nofile 51200;

    PHP:

    我的1g测试机,开64个是最好的,建议使用压力测试获取最佳值

    rlimit_files = 30000

    [www]

    request_slowlog_timeout = 2

    slowlog = /data/logs/php/slow.log

    pm.max_children = 64

    —————————————————————加上下面的内核优化———————————————————————————

    一)nginx 高并发参数配置及linux内核参数优化 :

    (1) vi /etc/sysctl.conf CentOS5.5中可以将所有内容清空直接替换为如下内容:

    net.ipv4.ip_forward = 0

    net.ipv4.conf.default.rp_filter = 1

    net.ipv4.conf.default.accept_source_route = 0

    kernel.sysrq = 0

    kernel.core_uses_pid = 1

    net.ipv4.tcp_syncookies = 1

    kernel.msgmnb = 65536

    kernel.msgmax = 65536

    kernel.shmmax = 68719476736

    kernel.shmall = 4294967296

    net.ipv4.tcp_max_tw_buckets = 6000

    net.ipv4.tcp_sack = 1

    net.ipv4.tcp_window_scaling = 1

    net.ipv4.tcp_rmem = 4096 87380 4194304

    net.ipv4.tcp_wmem = 4096 16384 4194304

    net.core.wmem_default = 8388608

    net.core.rmem_default = 8388608

    net.core.rmem_max = 16777216

    net.core.wmem_max = 16777216

    net.core.netdev_max_backlog = 262144

    net.core.somaxconn = 262144

    net.ipv4.tcp_max_orphans = 3276800

    net.ipv4.tcp_max_syn_backlog = 262144

    net.ipv4.tcp_timestamps = 0

    net.ipv4.tcp_synack_retries = 1

    net.ipv4.tcp_syn_retries = 1

    net.ipv4.tcp_tw_recycle = 1

    net.ipv4.tcp_tw_reuse = 1

    net.ipv4.tcp_mem = 94500000 915000000 927000000

    net.ipv4.tcp_fin_timeout = 1

    net.ipv4.tcp_keepalive_time = 30

    net.ipv4.ip_local_port_range = 1024 65000

    使配置立即生效可使用如下命令:

    /sbin/sysctl -p

    (2)关于系统连接数的优化:

    linux 默认值 open files 和 max user processes 为1024

    #ulimit -n

    1024

    #ulimit –u

    1024

    问题描述: 说明 server 只允许同时打开 1024 个文件,处理 1024个用户进程

    使用ulimit -a 可以查看当前系统的所有限制值,使用ulimit -n 可以查看当前的最大打开文件数。

    新装的linux 默认只有1024 ,当作负载较大的服务器时,很容易遇到error: too many open files。因此,需要将其改大。

    解决方法:

    使用 ulimit –n 65535 可即时修改,但重启后就无效了。(注ulimit -SHn 65535 等效 ulimit-n 65535 ,-S 指soft ,-H 指hard)

    有如下三种修改方式:

    1. 在/etc/rc.local 中增加一行 ulimit -SHn 65535

    2. 在/etc/profile 中增加一行 ulimit -SHn 65535

    3. 在/etc/security/limits.conf最后增加:

    * soft nofile 65535

    * hard nofile 65535

    * soft nproc 65535

    * hard nproc 65535

    具体使用哪种,在 CentOS 中使用第1 种方式无效果,使用第3 种方式有效果,而在Debian 中使用第2种有效果

    # ulimit -n

    65535

    # ulimit -u

    65535

    备注:ulimit 命令本身就有分软硬设置,加-H 就是硬,加-S 就是软默认显示的是软限制

    soft 限制指的是当前系统生效的设置值。 hard 限制值可以被普通用户降低。但是不能增加。 soft 限制不能设置的比hard 限制更高。 只有 root 用户才能够增加 hard 限制值。

    (3)对nginx作cpu亲和性绑定:

    worker_cpu_affinity 00000001 0000001000000100 00001000 00010000 00100000 01000000 10000000;

    我的:

    worker_processes  12;

    worker_cpu_affinity 0001 0010 0100 1000 0001 0010 0100 1000 0001 0010 0100 1000;

    摘自:http://blog.csdn.net/rachel_luo/article/details/8668137

    二)PHP-FPM高负载解决办法(我的1g测试机,开64个是最好的,建议使用压力测试获取最佳值):

    1.尽量少安装PHP模块,最简单是最好(快)的

    2. Increas PHP FastCGI child number to 100 and even more. Sometime, 200 is OK! ( On 4GB memory server);

    2.把您的PHP FastCGI子进程数调到100或以上,在4G内存的服务器上200就可以

    注:我的1g测试机,开64个是最好的,建议使用压力测试获取最佳值

    3. Using SOCKET PHP FastCGI, and put into /dev/shm on Linux;

    3.使用socket连接FastCGI,linux操作系统可以放在 /dev/shm中

    注:在php-fpm.cnf里设置<value name=”listen_address”>/tmp/nginx.socket</value>就可以通过socket连接FastCGI了,/dev/shm是内存文件系统,放在内存中肯定会快了.记得这时也要在nginx里的配置里进行修改,保持一致.

    location ~ .*.(php|php5)?$

    {

    #将Nginx与FastCGI的通信方式由TCP改为Unix Socket。TCP在高并发访问下比Unix Socket稳定,但Unix Socket速度要比TCP快。

    fastcgi_pass  unix:/tmp/php-cgi.sock;

    #fastcgi_pass  127.0.0.1:9000;

    fastcgi_index index.php;

    include fcgi.conf;

    }

    摘自:http://www.blogjava.net/tbwshc/archive/2012/08/16/385599.html

    三)nginx与php-fpm 打开文件过多(Too many open files)Too many open files

    经过第2)步sysctl -p设置后,一般不会出现上面第三)的问题:

    [root@jackxiang conf]# ulimit -Hn

    65535

    [root@jackxiang conf]# ulimit -Sn

    65535

    PHP-FPM配置文件:

    rlimit_files = 30000

    NGINX配置文件:

    # set open fd limit to 30000

    worker_rlimit_nofile 30000;

    以下是上面的详细解决方案:

    操作系统

    先查看 Linux (Cent Os)的文件打开限制

    ulimit -Hn

    ulimit -Sn

    -H 为 Hard 解释为硬件 -S 为 Soft 为软件。 具体意义不是很明。

    先去修改 /etc/sysctl.conf

    添加或者修改

    fs.file-max = 70000

    修改 /etc/security/limits.conf

    添加或修改

    <user>   soft    nofile   10000

    <user>   hard    nofile   30000

    后面的 10000,30000可以根据需要调整,至于 可以根据需要修改为 对应要扩大文件打开数 的用户,

    这里 因为我要处理 nginx 和 php-fpm 2个服务对应的2个用户,图方便 就使用了 * ,意思就是 所有用户。

    重载 sysctrl配置

    sysctl -p

    PHP-FPM

    修改 /etc/php-fpm.d/www.conf

    添加/修改

    rlimit_files = 30000

    数字随意,当然要低于OS的设置。

    NGINX

    修改 /etc/nginx/nginx.conf

    最外层 我是跟在 worker_processes 之后

    添加或者修改

    # set open fd limit to 30000

    worker_rlimit_nofile 30000;

    调试测试

    重启 nginx, php-fpm

    重新登录OS

    使用 ulimit 查看当前的 限制,看是否改好了

    同时 使用

    lsof | wc -l

    统计当前打开文件的数量,如果低于你的上限,就满足吧,记得高峰时候来查看下,随时调整你的ulimit上限。

  • 相关阅读:
    C++11写算法之顺序查找
    Codeforces Round #632 (Div. 2) 题解
    AtCoder Beginner Contest 161 题解
    Codeforces Round #630 (Div. 2) 部分题解
    2019-2020 ICPC Northwestern European Regional Programming Contest (NWERC 2019) 部分题解
    Codeforces Round #629 (Div. 3) 题解
    Educational Codeforces Round 84 (Rated for Div. 2) 部分题解
    AtCoder Beginner Contest 159 题解
    Codeforces Round #628 (Div. 2) 题解
    Codeforces Round #627 (Div. 3) 题解
  • 原文地址:https://www.cnblogs.com/xiaoleiel/p/8308486.html
Copyright © 2011-2022 走看看