Nginx性能优化(上)

简介: Nginx性能优化

一、性能优化概述


(1)性能优化之前要考虑的事情


  • 当前系统结构的瓶颈


使用ab压力测试观察指标


  • 了解业务模式


接口业务类型,系统层次化结构


  • 性能与安全


性能好安全弱,安全好性能低


(2)压力测试工具

******(1)做基础配置(略)
******(2)使用源代码包安装nginx(略)
******(3)使用yum安装ab
[root@rzy ~]# yum -y install httpd-tools  #ab是apache自带的,但是没有安装apache所以需要使用yum安装
。。。。。。
完毕!
******(4)使用ab压力测试工具测试nginx  #一般都是整个服务器架构搭建好之后在进行ab测试
[root@rzy ~]# ab -n 1000 -c 20 http://127.0.0.1/
#总共1000个请求,20个并发
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
Server Software:        nginx/1.18.0
Server Hostname:        127.0.0.1
Server Port:            80
Document Path:          /
Document Length:        612 bytes
Concurrency Level:      20
Time taken for tests:   0.079 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      845000 bytes
HTML transferred:       612000 bytes
Requests per second:    12624.35 [#/sec] (mean)
Time per request:       1.584 [ms] (mean)
Time per request:       0.079 [ms] (mean, across all concurrent requests)
Transfer rate:          10417.55 [Kbytes/sec] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       0
Processing:     0    1   5.9      1      43
Waiting:        0    1   5.8      0      43
Total:          1    2   5.9      1      43
Percentage of the requests served within a certain time (ms)
  50%      1
  66%      1
  75%      1
  80%      1
  90%      1
  95%      1
  98%     43
  99%     43
 100%     43 (longest request)

(3)影响nginx服务器性能的指标


网络: 网络的流量,网络是否丢包,这些会影响http的请求与调用

系统: 硬件有没有磁盘损坏,磁盘速率

服务: 连接优化,请求优化

程序: 接口性能,处理速度,程序执行效率

数据库: 每个架构服务与服务之间或多或少有一些关联,我们需要将整个架构进行分层,找到对应系统或服务的短板,然后进行优化


例如:Nginx交给Tomcat,Tomcat在交给mysql数据


二、Nginx性能优化


(1)Nginx基础配置


******(1)使用源码包方式安装nginx(略)
******(2)查看Nginx基础配置文件
[root@rzy ~]# ps aux | grep nginx  #查看nginx的进程
root       1044  0.0  0.0  20520   620 ?        Ss   17:12   0:00 nginx: master process   #/usr/local/nginx/sbin/nginx   #Nginx的主进程
nginx      1045  0.0  0.1  20956  1076 ?        S    17:12   0:00 nginx: worker process  #Nginx的工作进程
root       1047  0.0  0.0 112676   984 pts/0    R+   17:12   0:00 grep --color=auto nginx
——————————————————————————————————————————————————————————————————————
#nginx的主配置文件,一个区块对应一个{},不在{}里的表示是全局配置 
#main位于nginx。conf的最高层
#main层下面可以有Event和HTTP层
#HTTP层下面可以有多个Server层。用于对不同网站做不同的配置
#Server层也允许有多个Location
——————————————————————————————————————————————————————————————————————
[root@rzy ~]# vim /usr/local/nginx/conf/nginx.conf 
#user  nobody;            #设置nginx服务的系统使用用户
worker_processes  1;      #工作进程,配置要和CPU个数保持一致
#error_log  logs/error.log;        #错误日志,后面的是路径
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;        #Nginx服务启动时的pid
events {    #事件模块
    use epoll;                       #use指定Nginx的内核模型,一般是epoll
    worker_connections  1024;        #每个worker进程支持的最大连接数
}
http {              #非虚拟主机的配置或者公共配置要配置到httpd{}中,Server{}外
    include       mime.types;
    default_type  application/octet-stream;
    charset utf-8      #统一使用utf-8字符集
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    #access_log  logs/access.log  main;
    sendfile        on;  
    #tcp_nopush     on;   #静态资源服务器建议打开
    #keepalive_timeout  0;   
    keepalive_timeout  65;  #动态资源服务器需要打开keepalived
    #gzip  on;
    server {        #必须使用虚拟主机配置站点,每个虚拟主机使用一个Server{}
        listen       80;           #监听端口,默认80
        server_name  localhost;    #提供服务的域名或者主机名
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   html;                     #存放网站路径,是相对路径,即/usr/local/nginx
            index  index.html index.htm;     #默认可以解析的页面,配合php可以解析.php的动态页面
        }                                      #可以写多个server配置,配置多个虚拟主机
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {  
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}

(2)系统性能优化


文件句柄:Linux系统中一切皆文件,文件句柄可以理解为是一个索引,文件句柄会随着我们频繁调用进程的而增加


系统默认对文件句柄有着限制,不会让一个进程无限的调用,需要限制每个进程和每个服务可以调用的进程的上限,故而可以限制文件句柄的上限(默认限制为1024个)


文件句柄是必须要调整的优化参数,不修改的话当进程达到限制数量就会报错:Error:too many open files

文件句柄的限制方式有两种设置方式,分别是系统全局性修改(直接对系统生效),用户局部性修改(只对某个用户生效)
[root@rzy ~]# vim /etc/security/limits.conf  #设置的主配置文件
。。。。。。#最后一行添加
#用户局部性修改
root soft nofile 65535   #软限制,最大65535,想要所有用户都进行限制,只需要把root改成*即可,最大限制一般为65535
root hard nofile 65535   #硬限制
#对Nginx进程进行限制
worker_rlimit_nofile 45535;   #对Nginx进程限制最大45535

使用xshell再开一个终端,使用ulimit -n 进行查询,没改之前是默认的1024

20210511091900278.png

修改文档之后,再次重新开一个终端,发现已经成功修改


20210511091923619.png


(3)Nginx性能优化之CPU亲和


CPU亲和,也叫CPU绑定,减少进程之间不断频繁迁移,减少性能损耗

#查看当前CPU物理状态
[root@rzy ~]# lscpu | grep "CPU(s)"  #通过grep查看筛选
CPU(s):                1
On-line CPU(s) list:   0
#配置主配置文件进行绑定
[root@rzy ~]# vim /usr/local/nginx/conf/nginx.conf
  1
  2 #user  nobody;
  3 worker_processes  1;  #这里是1核,所以这里绑定一个核心
  4                       #这里可以写worker_cpu_affinity 两核写01 10 四核写0001 0010 0100 1000依次类推
。。。。。。
也可以写成自动:
worker_processes auto
worker_cpu_affinity auto

(4)Nginx常见问题


1、Nginx多个相同的Server_name优先级


  • 实验环境:


系统 ip 主机名 ngixn版本
Centos7.4 192.168.100.202 rzy nginx-1.18.0


  • 实验步骤:
******(1)使用源码包方式安装nginx(略)
******(2)创建三个目录
[root@rzy ~]# mkdir -p /aaa/bbb{1..3}  #创建aaa目录下以bbb开头后面分别是1到3的目录
[root@rzy ~]# for i in {1..3};do echo "abc $i" > /aaa/bbb"$i"/index.html;done #使用for循环,给三个目录下写网页
******(3)修改主配置文件
[root@rzy ~]# vim /usr/local/nginx/conf/nginx.conf #以#开头的注释行可以删除
。。。。。。  #在http{}里修改,创建三个虚拟主机,分别对应刚才创建的三个不同的目录
 34     server {
 35         listen 80;
 36         server_name 192.168.100.202;
 37         location / {
 38              root /aaa/bbb2;
 39              index index.html;
 40            }
 41        }
 42     server {
 43         listen 80;
 44         server_name 192.168.100.202;
 45         location / {
 46              root /aaa/bbb1;
 47              index index.html;
 48            }
 49        }
 50     server {
 51         listen 80;
 52         server_name 192.168.100.202;
 53         location / {
 54              root /aaa/bbb3;
 55              index index.html;
 56            }
 57        }
 58 
。。。。。。
#保存退出
[root@rzy ~]# nginx -t  #检查nginx语句是否正确
nginx: [warn] conflicting server name "192.168.100.202" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "192.168.100.202" on 0.0.0.0:80, ignored
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@rzy ~]# systemctl restart nginx  #重启服务
[root@rzy ~]# curl 127.0.0.1  #访问本地 
abc 2      
#发现访问的是bbb2目录的,但是主配置文件里的server虚拟主机不是按1、2、3的这种顺序写的,而bbb2在第一个,说明当servername相同时,访问的优先级是从上到下的(域名都包含192.168.100.202)

2、location优先级


  • 实验环境:


系统 ip 主机名 ngixn版本
Centos7.4 192.168.100.202 rzy nginx-1.18.0


  • location匹配优先级


location指定资源,即用户访问这个资源的时候location中的配置才会生效,location / 默认匹配则全部生效


完整匹配 优先级
= 进行普通字符的精确匹配,即完全匹配
^~ 表示普通字符匹配,使用前缀匹配
正则匹配 匹配后会继续查找更加精确的location
~ 区分大小写匹配
~* 不区分大小写匹配
/ 默认匹配


  • 实验步骤:
******继续上面的操作步骤
******(1)修改主配置文件
[root@rzy ~]# vim /usr/local/nginx/conf/nginx.conf
。。。。。。#还是修改server虚拟主机
 34     server {
 35         listen 80;
 36         server_name  192.168.100.202;
 37         root /aaa;
 38         index index.html;
 39         location = /bbb1 {
 40         rewrite ^(.*)$ /bbb1/index.html break;
 41            }
 42         location ~ /bbb* {
 43         rewrite ^(.*)$ /bbb2/index.html break;
 44            }
 45         location ^~ /bbb {
 46         rewrite ^(.*)$ /bbb3/index.html break;
 47            }
 48        }
。。。。。。
#保存退出
[root@rzy aaa]# nginx -t  #检查nginx语句是否配置正确
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@rzy aaa]# systemctl restart nginx  #重启服务
[root@rzy aaa]# curl http://192.168.100.202/bbb1  #访问本地,指定访问目录
abc 1
******(2)再次修改配置文件,注释掉精确匹配那一行
[root@rzy aaa]# vim /usr/local/nginx/conf/nginx.conf
。。。。。。
 34     server {
 35         listen 80;
 36         server_name  192.168.100.202;
 37         root /aaa;
 38         index index.html;
 39        # location = /bbb1 {
 40         #rewrite ^(.*)$ /bbb1/index.html break;
 41         #   }
 42         location ~ /bbb* {
 43         rewrite ^(.*)$ /bbb2/index.html break;
 44            }
 45         location ^~ /bbb {
 46         rewrite ^(.*)$ /bbb3/index.html break;
 47            }
 48        }
。。。。。。
#保存退出
[root@rzy aaa]# nginx -t  
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@rzy aaa]# systemctl restart nginx
[root@rzy aaa]# curl http://192.168.100.202/bbb1  #再次访问本地,发现访问的变成了bbb3目录
abc 3
******(3)再次注释掉前缀匹配那一行
[root@rzy aaa]# vim /usr/local/nginx/conf/nginx.conf
。。。。。。
 34     server {
 35         listen 80;
 36         server_name  192.168.100.202;
 37         root /aaa;
 38         index index.html;
 39        # location = /bbb1 {
 40         #rewrite ^(.*)$ /bbb1/index.html break;
 41         #   }
 42         location ~ /bbb* {
 43         rewrite ^(.*)$ /bbb2/index.html break;
 44            }
 45         #location ^~ /bbb {
 46         #rewrite ^(.*)$ /bbb3/index.html break;
 47         #   }
 48        }
。。。。。。
#保存退出
[root@rzy aaa]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@rzy aaa]# systemctl restart nginx
[root@rzy aaa]# curl http://192.168.100.202/bbb1  #再次访问本地,发现访问的是bbb2目录
abc 2
#由此得知,location的优先级是精确匹配大于前缀匹配大于区分大小写匹配大于默认匹配的(nginx默认是默认匹配)

= > 正则匹配 > ^~ > */ ** > 默认匹配

目录
相关文章
|
6月前
|
应用服务中间件 Linux PHP
|
网络协议 Unix 应用服务中间件
Nginx极简实战—Nginx服务器高性能优化配置,轻松实现10万并发访问量
如何使Nginx轻松实现10万并发访问量。通常来说,一个正常的 Nginx Linux 服务器可以达到 500,000 – 600,000 次/秒 的请求处理性能,如果Nginx服务器经过优化的话,则可以稳定地达到 904,000 次/秒 的处理性能,大大提高Nginx的并发访问量。
Nginx极简实战—Nginx服务器高性能优化配置,轻松实现10万并发访问量
|
14天前
|
安全 应用服务中间件 Linux
高并发下Nginx优化(一)
【4月更文挑战第15天】高并发下Nginx优化
16 0
|
6月前
|
缓存 应用服务中间件 Linux
深入理解Nginx工作原理及优化技巧(下)
深入理解Nginx工作原理及优化技巧
|
9月前
|
应用服务中间件 测试技术 Linux
Nginx 实战系列之一:Nginx 压测方法论和性能指标
Nginx 实战系列之一:Nginx 压测方法论和性能指标
|
存储 缓存 网络协议
Nginx-性能优化
Nginx-性能优化
|
缓存 网络协议 前端开发
Nginx性能优化详解
Nginx 性能优化有这篇就够了 1、Nginx运行工作进程数量 Nginx运行工作进程个数一般设置CPU的核心或者核心数x2。如果不了解cpu的核数,可以top命令之后按1看出来,也可以查看/proc/cpuinfo文件 grep ^processor /proc/cpuinfo | wc -l
396 0
|
缓存 监控 安全