Nginx调优

nginx调优
    nginx安装
    ngx_http_stub_status监控连接信息
    ngxtop监控请求信息
    nginx-rrd图形化监控
    nginx优化

修改yum源
vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

yum install nginx -y

nginx的启停
systemctl status/start/stop nginx    查看启动关闭nginx
nginx -s reload                重启
nginx -V                查看编译参数
/etc/nginx/nginx.conf            默认配置文件
注意:配置反向代理要关闭selinux     setenforce 0

nginx -s stop/start/reload


ngx_http_stub_status配置
主要监控nginx连接信息

使用yum安装时,默认编译了此模块,如编译安装,需手动编译进去 查看编译参数nginx -V
添加配置
location = /nginx_status{
    stub_status on;
    access_log off;
    allow 127.0.0.1;
    deny all;
}


ngxtop安装
主要监控nginx请求信息

yum install epel-release python-pip
pip install ngxtop

ngxtop使用
指定配置文件:    ngxtop -c /etc/nginx/nginx.conf
查询状态是200:    ngxtop -c /etc/nginx/nginx.conf -i 'status==200'
查询访问最多ip:    ngxtop -c /etc/nginx/nginx.conf -g remote_addr



nginx-rrd监控
可以监控nginx的连接信息和请求信息

1.安装nginx 配置ngx_http_stub_status监控
关闭防火墙
iptables -F
systemctl stop firewalld

2.安装php相关依赖
yum install -y php php-gd php-soap php-mbstring php-xmlrpc php-dom php-fpm

3.nginx整合php-fpm
修改/etc/php-fpm.d/www.conf文件中的user和group,与nginx.conf中的user一致.
user = nginx
group = nginx

4.启动php-fpm服务
systemctl start php-fpm

5.修改nginx的配置
location ~ .php${
    root /usr/share/nginx/html;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
    include fastcgi_params;
}

6.重启nginx
nginx -s reload

7.编写index.php
cd /usr/share/nginx/html
cat index.php
<?php phpinfo();?>

8.打开浏览器访问:http://ip/index.php

9.安装rrdtool相关依赖
yum install -y perl rrdtool perl-libwww-perl libwww-perl perl-rrdtool

10.安装nginx-rrd
wget http://soft.vpser.net/status/nginx-rrd/nginx-rrd-0.1.4.tgz
tar -zxvf nginx-rrd-0.1.4.tgz
cd nginx-rrd-0.1.4
cp usr/sbin/* /usr/sbin        #复制主程序文件到/usr/sbin下
cp etc/nginx-rrd.conf /etc    #复制配置文件到/etc下
cp html/index.php /usr/share/nginx/html/

11.修改配置
vim /etc/nginx-rrd.conf
RRD_DIR="/usr/share/nginx/html/nginx-rrd";
WWW_DIR="/usr/share/nginx/html/";

12.新建定时任务        crontab -e
crontab -l
* * * * * /bin/sh /usr/sbin/nginx-collect
*/1 * * * * /bin/sh /usr/sbin/nginx-graph
查看任务
tail -f /var/log/cron        crontab -l

13.ab压测
安装ab压测工具:    yum install -y httpd-tools
ab -n 10000 -c 10 http://127.0.0.1/index.html





nginx优化
    增加工作线程数和并发连接数
    启用长连接                            #开启与后端Server的长连接-反向代理时
    启用缓存/压缩
    操作系统优化
    
配置工作线程和并发数
    worker_processes 4;                    #CPU    也可以改为 auto 让Nginx自动选择
    events{
        worker_connections 10240;        #每一个进程打开的最大连接数,包含了Nginx与客户端和Nginx与upstream之间的连接        该值受限于操作系统
        multi_accept on;                #可以一次建立多个连接
        use epoll;                        #启用epoll网络模型
    }
    
    注:nginx -t 可测试nginx的配置文件是否正确!!!

配置后端Server的长连接
    upstream server_pool{
        server localhost:8080 weight=1 max_fails=2 fail_timeout=30s;
        server localhost:8180 weight=1 max_fails=2 fail_timeout=30s;
        keepalive 300;
    }
    location / {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade":
        proxy_pass http://server_pool/;
    }
    
配置压缩
        gzip on;
        gzip_http_version 1.1;
        gzip_disable "MSIE [1-6].(?!.*SV1)";
        gzip_proxied any;
        gzip_types text/plain text/css application/javascript application/x-javascript application/json application/xml application/vnd.ms-fontobject application/x-font-ttf application/svg+xml application/x-icon;             #html默认开启
        gzip_vary on;                                        #Vary:Accept-Encoding
        gzip_static on;                                        #如果有压缩好的 直接使用  编译nginx需要加入相关选项
        
操作系统优化
    配置文件/etc/sysctl.conf
        sysctl -w net.ipv4.tcp_syncookies = 1        #防止一个套接字有过多的试图连接达到是引起过载
        sysctl -w net.core.somaxconn = 1024            #默认128,连接队列
        sysctl -w net.ipv4.tcp_fin_timeout = 10        #timewait的超时时间
        sysctl -w net.ipv4.tcp_tw_reuse = 1            #OS直接使用timewait的连接
        sysctl -w net.ipv4.tcp_tw_recycle = 0        #回收禁用    如果打开可能会引起tcp协议做不下去,引起系统不稳定

    配置文件/etc/security/limits.conf
        * hard nofile 204800
        * soft nofile 204800
        * soft core unlimited
        * soft stack 204800

其他优化
    sendfile    on;        #减少文件在应用和内核之间的拷贝
    tcp_nopush    on;        #当数据包达到一定大小再发送
    tcp_nodelay    off;    #有数据随时发送
原文地址:https://www.cnblogs.com/jxdong116/p/9635949.html