nginx的性能测试及常用优化手段

一、nginx的性能测试及对比
1.环境准备
[root@test8_hadoop_kaf ~]# yum install -y httpd-tools
[root@test8_hadoop_kaf conf.d]# cat ab_xingneng.conf
server { listen 80; server_name testserver01 es.chinasoft.com; root /opt/app; location / {
root /opt/app/code/cache;
try_files $uri @java_page;
} location @java_page {
proxy_pass http://127.0.0.1:9090;
} } 2.测试由nginx提供服务的静态资源
ab -n -c 2 http://es.chinasoft.com/jack.html
-n 总的请求数
-c 并发数
-k 是否开启长连接 [root@test8_hadoop_kaf ~]# ab -n 2000 -c 2 http://es.chinasoft.com/jack.html
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 es.chinasoft.com (be patient)
Completed 200 requests
Completed 400 requests
Completed 600 requests
Completed 800 requests
Completed 1000 requests
Completed 1200 requests
Completed 1400 requests
Completed 1600 requests
Completed 1800 requests
Completed 2000 requests
Finished 2000 requests Server Software: nginx/1.12.2 # 服务器程序
Server Hostname: es.chinasoft.com # 域名
Server Port: 80 # 端口 Document Path: /jack.html # 访问的页面
Document Length: 114 bytes # 内容的长度 Concurrency Level: 2
Time taken for tests: 1.777 seconds # 测试的时间
Complete requests: 2000 # 完成的请求数量
Failed requests: 0 # 失败的请求数量
Write errors: 0 # 写失败的格式
Total transferred: 708000 bytes # 总共传输的字节数
HTML transferred: 228000 bytes # html传输的字节数
Requests per second: 1125.37 [#/sec] (mean) # 每秒的请求数
Time per request: 1.777 [ms] (mean) # 每个请求耗费的时间
Time per request: 0.889 [ms] (mean, across all concurrent requests) # 服务器响应请求花费的时间
Transfer rate: 389.05 [Kbytes/sec] received # 传输速度 Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 0.3 1 7
Processing: 1 1 0.6 1 16
Waiting: 1 1 0.6 1 16
Total: 1 2 0.7 2 17 Percentage of the requests served within a certain time (ms)
50% 2
66% 2
75% 2
80% 2
90% 2
95% 2
98% 3
99% 4
100% 17 (longest request) 3.测试java的动态接口程序
[root@test8_hadoop_kaf ROOT]# cat /data/yunva/test_tomcat8.0.37_9090/webapps/ROOT/test_sleep.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<HTML>
<HEAD>
<TITLE>JSP TEST PAGE</TITLE>
</HEAD>
<BODY>
<%
Thread.sleep(5000);
Random rand = new Random();
out.println("<h1>Random number:</h1>");
out.println(rand.nextInt(99) + 100);
%>
</BODY>
</HTML> [root@test8_hadoop_kaf bin]# curl -I http://es.chinasoft.com/test_sleep.jsp
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 17 Jan 2018 11:32:14 GMT
Content-Type: text/html;charset=utf-8
Connection: keep-alive
Set-Cookie: JSESSIONID=0B5052BEF444639173C600EB08A87980; Path=/; HttpOnly # 对动态接口进行压测,可以看到每秒完成的请求数是0.36个,性能比较差
[root@test8_hadoop_kaf ROOT]# ab -n 20 -c 2 http://es.chinasoft.com/test_sleep.jsp
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 es.chinasoft.com (be patient).....done Server Software: nginx/1.12.2
Server Hostname: es.chinasoft.com
Server Port: 80 Document Path: /test_sleep.jsp
Document Length: 112 bytes Concurrency Level: 2
Time taken for tests: 55.033 seconds
Complete requests: 20
Failed requests: 0
Write errors: 0
Total transferred: 6880 bytes
HTML transferred: 2240 bytes
Requests per second: 0.36 [#/sec] (mean)
Time per request: 5503.345 [ms] (mean)
Time per request: 2751.672 [ms] (mean, across all concurrent requests)
Transfer rate: 0.12 [Kbytes/sec] received Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 0.0 1 1
Processing: 5002 5002 0.2 5002 5003
Waiting: 5002 5002 0.2 5002 5003
Total: 5003 5003 0.3 5003 5003 Percentage of the requests served within a certain time (ms)
50% 5003
66% 5003
75% 5003
80% 5003
90% 5003
95% 5003
98% 5003
99% 5003
100% 5003 (longest request) 4.测试由tomcat提供的静态资源
[root@test8_hadoop_kaf ROOT]# cd /opt/app/code/cache
[root@test8_hadoop_kaf cache]# mv jack.html jack.html.bak 这时访问jack.html就会转到tomcat去处理 [root@test8_hadoop_kaf conf.d]# ab -n 2000 -c 2 http://es.chinasoft.com/jack.html
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 es.chinasoft.com (be patient)
Completed 200 requests
Completed 400 requests
Completed 600 requests
Completed 800 requests
Completed 1000 requests
Completed 1200 requests
Completed 1400 requests
Completed 1600 requests
Completed 1800 requests
Completed 2000 requests
Finished 2000 requests Server Software: nginx/1.12.2
Server Hostname: es.chinasoft.com
Server Port: 80 Document Path: /jack.html
Document Length: 114 bytes Concurrency Level: 2
Time taken for tests: 1.561 seconds
Complete requests: 2000
Failed requests: 0
Write errors: 0
Total transferred: 708000 bytes
HTML transferred: 228000 bytes
Requests per second: 1281.20 [#/sec] (mean)
Time per request: 1.561 [ms] (mean)
Time per request: 0.781 [ms] (mean, across all concurrent requests)
Transfer rate: 442.92 [Kbytes/sec] received Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 0.3 0 7
Processing: 1 1 0.5 1 9
Waiting: 1 1 0.5 1 9
Total: 1 2 0.6 1 16
ERROR: The median and mean for the initial connection time are more than twice the standard
deviation apart. These results are NOT reliable.
WARNING: The median and mean for the total time are not within a normal deviation
These results are probably not that reliable. Percentage of the requests served within a certain time (ms)
50% 1
66% 1
75% 2
80% 2
90% 2
95% 2
98% 3
99% 5
100% 16 (longest request) 二、系统与nginx的性能优化 主要考虑的方面:
网络、系统、服务、程序、数据库、底层服务 系统与nginx的性能优化 文件句柄 linux\unix 一切皆文件,文件句柄就是一个索引 设置方式
系统全局性修改、用户局部性修改、进程局部性修改 soft是不强制限制,超过发邮件通知
hard是操作系统强制限制,请求会受到影响 # 针对用户root的限制
root hard nofile 1000000
root soft nofile 1000000 # 全局限制,针对所有用户的限制
* hard nofile 1000000
* soft nofile 1000000 针对Nginx进程的文件句柄限制
[root@test8_hadoop_kaf conf.d]# vim /etc/nginx/nginx.conf
worker_rlimit_nofile 655350; cpu的性能
top
按住1 可以显示cpu的核心数 1.nginx绑定cpu,每个进程指定cpu 表示cpu的方法:
有多少核就用多少个0表示,4核用4个0表示 0000,8核用8个0表示 00000000 # 修改nginx配置,这里是4核的CPU,开放4个进程
[root@node1 ~]# cat /etc/nginx/nginx.conf user nginx;
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000; error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid; worker_rlimit_nofile 35535;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$request_uri"'; access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65; include /etc/nginx/conf.d/*.conf;
} [root@node1 ~]# /usr/sbin/nginx -s reload
[root@node1 ~]# ps -ef|grep nginx
root 18106 1 0 01:54 ? 00:00:00 nginx: master process /usr/sbin/nginx
nginx 20301 18106 0 16:46 ? 00:00:00 nginx: worker process
nginx 20302 18106 0 16:46 ? 00:00:00 nginx: worker process
nginx 20303 18106 0 16:46 ? 00:00:00 nginx: worker process
nginx 20304 18106 0 16:46 ? 00:00:00 nginx: worker process
root 20306 18433 0 16:46 pts/0 00:00:00 grep --color=auto nginx # 可以看到不同的进程绑定在了不同的cpu上面
[root@node1 ~]# ps -eo pid,args,psr |grep [n]ginx
18106 nginx: master process /usr/ 2
20301 nginx: worker process 0
20302 nginx: worker process 1
20303 nginx: worker process 2
20304 nginx: worker process 3 2.让work1和work2共用一个cpu 修改配置
# cat nginx.conf user nginx;
worker_processes 4;
worker_cpu_affinity 0010 0010 0100 1000 [root@node1 nginx]# ps -ef|grep nginx
root 18106 1 0 01:54 ? 00:00:00 nginx: master process /usr/sbin/nginx
nginx 20322 18106 0 16:51 ? 00:00:00 nginx: worker process
nginx 20323 18106 0 16:51 ? 00:00:00 nginx: worker process
nginx 20324 18106 0 16:51 ? 00:00:00 nginx: worker process
nginx 20325 18106 0 16:51 ? 00:00:00 nginx: worker process
root 20327 18433 0 16:51 pts/0 00:00:00 grep --color=auto nginx # 可以看到20322和20323都绑定在了编号为1的处理器上
[root@node1 nginx]# ps -eo pid,args,psr |grep [n]ginx
18106 nginx: master process /usr/ 2
20322 nginx: worker process 1
20323 nginx: worker process 1
20324 nginx: worker process 2
20325 nginx: worker process 3 3.配置nginx的worker交叉绑定cpu,很少使用
其中一个worker可以使用1,3...的cpu
另外一个使用2,4的cpu [root@node1 nginx]# ps -ef|grep nginx
root 18106 1 0 01:54 ? 00:00:00 nginx: master process /usr/sbin/nginx
nginx 20339 18106 0 16:57 ? 00:00:00 nginx: worker process
nginx 20340 18106 0 16:57 ? 00:00:00 nginx: worker process
root 20342 18433 0 16:57 pts/0 00:00:00 grep --color=auto nginx
[root@node1 nginx]# ps -eo pid,args,psr |grep [n]ginx
18106 nginx: master process /usr/ 2
20339 nginx: worker process 1
20340 nginx: worker process 0 4.自动选择cpu
配置为auto
nginx1.9以后新增的配置 # nginx配置
worker_processes 4;
worker_cpu_affinity auto; [root@node1 nginx]# ps -ef|grep nginx
root 18106 1 0 01:54 ? 00:00:00 nginx: master process /usr/sbin/nginx
nginx 20362 18106 0 17:03 ? 00:00:00 nginx: worker process
nginx 20363 18106 0 17:03 ? 00:00:00 nginx: worker process
nginx 20364 18106 0 17:03 ? 00:00:00 nginx: worker process
nginx 20365 18106 0 17:03 ? 00:00:00 nginx: worker process
root 20367 18433 0 17:03 pts/0 00:00:00 grep --color=auto nginx
[root@node1 nginx]# ps -eo pid,args,psr |grep [n]ginx
18106 nginx: master process /usr/ 2
20362 nginx: worker process 0
20363 nginx: worker process 1
20364 nginx: worker process 2
20365 nginx: worker process 3 建议worker数量和cpu核心一致即可 常用的优化配置项 [root@node1 nginx]# cat nginx.conf user nginx;
worker_processes 4; # 4个进程,建议和cpu核心一致
worker_cpu_affinity auto; # 自动绑定cpu error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid; worker_rlimit_nofile 35535; # 文件句柄限制
events {
worker_connections 10240; # 每个进程最大10240个连接
} http {
include /etc/nginx/mime.types;
default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$request_uri"'; #access_log /var/log/nginx/access.log main;
access_log off; sendfile on; # 开启buffer缓存
#tcp_nopush on;
#tcp_nodeny on;
keepalive_timeout 65; # 长连接超时时间 gzip on; # gzip压缩
gzip_disable "MSIE [1-6]\."; # ie6不兼容gzip,需要disable,否则可能无法显示网页内容
gzip_http_version 1.1; include /etc/nginx/conf.d/*.conf;
}

三、工具使用
1、tcpdump 网络跟踪(所有软件的运行原理,故障排除)
2、strace 进程跟踪(跟踪进程的行为,分析故障)

######################################################################

linux下nginx日志误删后恢复

要想恢复的话,前提是没有重启nginx服务。

首先要来介绍下/proc目录。

/proc 是一个虚拟的目录,不占用实际的存储空间,其实存在于系统的内存中。其实以文件系统的方式为访问系统内核的操作提供接口,是动态从系统内核当中读取所需信息的。

下面就介绍下回复步骤,模拟下恢复的过程。但是前提是没有重启nginx服务

1,备份nginx的error日志,然后删除error日志

2,查看nginx的pid,然后到/proc/pid/fd  目录下

可以看到文件名:2(昨天测试的),3   他们的链接指向是errorlog,但是后面会显示已deleted了。我们用tail看下能看到3还在不停的在刷日志

3,我们直接把文件3重定向到errorlog就可恢复nginx的日志了。

具体的想看进程打开了哪些文件,可以使用lsof命令。

至此,nginx日志已经恢复,恢复以后看到好像不会继续往error.log 里面继续写日志了,需要重启下。不知道这个重启是不是必须的。知道可以留言告知下。其他的文件也可以按照此方法来试试。

学习Nginx(三)的更多相关文章

  1. Docker学习(三): Dockerfile指令介绍

    特别声明: 博文主要是学习过程中的知识整理,以便之后的查阅回顾.部分内容来源于网络(如有摘录未标注请指出).内容如有差错,也欢迎指正! =============系列文章============= 1 ...

  2. Oracle学习笔记三 SQL命令

    SQL简介 SQL 支持下列类别的命令: 1.数据定义语言(DDL) 2.数据操纵语言(DML) 3.事务控制语言(TCL) 4.数据控制语言(DCL)  

  3. 从零开始学习jQuery (三) 管理jQuery包装集

    本系列文章导航 从零开始学习jQuery (三) 管理jQuery包装集 一.摘要 在使用jQuery选择器获取到jQuery包装集后, 我们需要对其进行操作. 本章首先讲解如何动态的创建元素, 接着 ...

  4. 前端学习 第三弹: JavaScript语言的特性与发展

    前端学习 第三弹: JavaScript语言的特性与发展 javascript的缺点 1.没有命名空间,没有多文件的规范,同名函数相互覆盖 导致js的模块化很差 2.标准库很小 3.null和unde ...

  5. Android Animation学习(三) ApiDemos解析:XML动画文件的使用

    Android Animation学习(三) ApiDemos解析:XML动画文件的使用 可以用XML文件来定义Animation. 文件必须有一个唯一的根节点: <set>, <o ...

  6. 三、Android学习第三天——Activity的布局初步介绍(转)

    (转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 三.Android学习第三天——Activity的布局初步介绍 今天总结下 ...

  7. JavaWeb学习总结(三)——Tomcat服务器学习和使用(二) 包含https 非对称秘钥 NB

    JavaWeb学习总结(三)--Tomcat服务器学习和使用(二) 一.打包JavaWeb应用 在Java中,使用"jar"命令来对将JavaWeb应用打包成一个War包,jar命 ...

  8. MyEclipse Spring 学习总结三 SpringMVC

    MyEclipse Spring 学习总结三 SpringMVC 一.SpringMVC原理 1.Springmvc 框架介绍 1)Spring 框架停工了构建Web应用程序的全功能MVC模块.Spr ...

  9. Quartz定时任务学习(二)web应用/Quartz定时任务学习(三)属性文件和jar

    web中使用Quartz 1.首先在web.xml文件中加入 如下内容(根据自己情况设定) 在web.xml中添加QuartzInitializerServlet,Quartz为能够在web应用中使用 ...

  10. MyBatis学习系列三——结合Spring

    目录 MyBatis学习系列一之环境搭建 MyBatis学习系列二——增删改查 MyBatis学习系列三——结合Spring MyBatis在项目中应用一般都要结合Spring,这一章主要把MyBat ...

随机推荐

  1. 还在被数据分析报告折磨?Smartbi让你解放双手

    数据分析报告贯穿了企业经营的时时刻刻,方方面面. 数据分析报告最常用于汇报分享:团队需要分享.沟通,数据分析师需要洞察数据.分析结果分享给企业领导.团队同事.大众媒体及更多的利益相关方. 数据分析报告 ...

  2. 更快的网络文件系統 — Oxfs

    什麽时候需要网络文件系统 ? 做嵌入式的同学经常会使用 NFS 将 host 上的某个目录挂载到开发板上,方便 host 上编译构建后能直接在板子上运行,减少手工拷贝操作.网站开发时,在 host 上 ...

  3. 【C#单元测试】 开篇

    官方:https://docs.microsoft.com/zh-cn/visualstudio/test/install-third-party-unit-test-frameworks?view= ...

  4. 【C# 反射】使用 Activator 类 -激活器

    创建类的实例: //需要添加对Education.dll的引用才能正确执行 object CreateInstanceKind1 = Activator.CreateInstance("Ed ...

  5. 深度剖析数仓CN增量备份技术

    摘要:为了解决Roach的性能问题,提出了CN增量备份手段,从而达到进一步优化RPO目的. 本文分享自华为云社区<GaussDB(DWS)备份容灾之CN增量备份>,作者: zxy_db . ...

  6. MySQL Community Server安装

    MySQL Community Server安装 下载地址:https://dev.mysql.com/downloads/mysql/,zip安装,解压缩之后其实就可以用了,但是要进行配置. 1,解 ...

  7. JQuery常见方法

    <!DOCTYPE htmi> <html> <head> <meta charset="UTF-8"> <title> ...

  8. 05-LoadBalancer负载均衡

    1.介绍 目前主流的负载方案分为以下两种: 集中式负载均衡,在消费者和服务提供方中间使用独立的代理方式进行负载,有硬件的(比如 F5),也有软件的(比如 Nginx). 客户端根据自己的请求情况做负载 ...

  9. linux作业--第四周

    1.自建yum仓库,分别为网络源和本地源 所有Yum仓库的配置文件均需以 .repo 结尾并存放在/etc/yum.repos.d/目录中的 [base] : yum仓库唯一标识符,避免与其它仓库冲突 ...

  10. 06 for循环 增强for循环 练习

    for循环 //九九乘法表 //1.打印第一列 //2.我们固定的1再用一个循环包起来 //3.去掉重复项,i <= j //4.调整样式 for(int j=1;j <=9;j++){ ...