本文共 5932 字,大约阅读时间需要 19 分钟。
点关注,不迷路!如果本文对你有帮助的话不要忘记点赞支持哦!
a. 占有内存少 (在3W并发连接中,开启的10个nginx进程消耗内存大约150M)
b. 高并发能力强 (官方测试能够支撑5W并发连接,在实际生产环境中能到2-3W并发连接数) c . 简单 (配置文件通俗易懂) d . 价格 (免费、开源) e. 支持Rewriter重写 (能够根据域名、URL的不同,将HTTP请求分到不同的后端服务器群组) f. 内置健康检查 (如果nginx后端有几个服务宕机了,不会影响前端访问,能自动检测服务状态) g. 节省带宽 (支持GZIP压缩,可以添加浏览器本地缓存的Header头) h. 稳定性高,反向代理,很少宕机 i .中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等
1、通过访问日志,你可以得到用户地域来源、跳转来源、使用终端、某个URL访问量等相关信息。
2、通过错误日志,你可以得到系统某个服务或server的性能瓶颈等
[root@bogon logs]# ll总用量 24-rw-r--r--. 1 root root 17403 2月 8 17:09 access.log-rw-r--r--. 1 root root 186 2月 7 17:39 error.log[root@bogon logs]# ll总用量 24-rw-r--r--. 1 root root 17628 2月 9 14:38 access.log-rw-r--r--. 1 root root 186 2月 7 17:39 error.log
#!/bin/bash#设置日志文件存放目录LOG_HOME="/data/nginx/logs/"#备分文件名称LOG_PATH_BAK="$(date -d yesterday +%Y%m%d%H%M)".access.log#重命名日志文件mv ${LOG_HOME}/access.log ${LOG_HOME}/${LOG_PATH_BAK}.log#向nginx主进程发信号重新打开日志kill -USR1 `cat /opt/nginx/logs/nginx.pid
*/1 * * * * sh /data/nginx/nginx_log.sh
[root@bogon logs]# ll总用量 16-rw-r--r--. 1 root root 1238 2月 9 17:44 202002081743.access.log-rw-r--r--. 1 root root 2588 2月 9 17:45 202002081745.access.log-rw-r--r--. 1 root root 0 2月 9 17:46 access.log-rw-r--r--. 1 root root 0 2月 9 17:10 error.log
key | 含义 |
---|---|
Document Path | 测试的页面 |
Document Length | 页面的大小 |
Concurrency Level | 并发数量、并发用户数 |
Time taken for tests | 测试耗费总时间 |
Complete requests | 请求总量、并发连接数 |
Failed requests | 请求失败的数量 |
Write errors | 错误数量 |
Requests per second | 每秒钟的请求量、吞吐率 |
Time per request | 每次请求需要时间、响应时间 |
yum install httpd-tools –y
ab -c 1 -n 6 http://192.168.1.90:8070/index/index.html192.168.1.91 - - [09/Feb/2020:11:55:23 +0000] "GET /index/index.html HTTP/1.0" 200 20 "-" "ApacheBench/2.3" "-"192.168.1.91 - - [09/Feb/2020:11:55:23 +0000] "GET /index/index.html HTTP/1.0" 200 150 "-" "ApacheBench/2.3" "-"192.168.1.91 - - [09/Feb/2020:11:55:23 +0000] "GET /index/index.html HTTP/1.0" 200 20 "-" "ApacheBench/2.3" "-"192.168.1.91 - - [09/Feb/2020:11:55:23 +0000] "GET /index/index.html HTTP/1.0" 200 150 "-" "ApacheBench/2.3" "-"
• limit_conn_zone
• limit_req_zone • ngx_http_upstream_module 前两种只能对客户端(即单一ip限流)
upstream localhost{server 192.168.1.90:8071 max_conns=5;server 192.168.1.90:8072 max_conns=5;}
[root@bogon ~]# ab -c 1 -n 6 http://192.168.1.90:8070/index/index.htmlThis 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 192.168.1.90 (be patient).....doneServer Software: nginx/1.17.8Server Hostname: 192.168.1.90Server Port: 8070Document Path: /index/index.htmlDocument Length: 150 bytesConcurrency Level: 1Time taken for tests: 0.041 secondsComplete requests: 6Failed requests: 0 Write errors: 1Total transferred: 2328 bytesHTML transferred: 900 bytesRequests per second: 146.03 [#/sec] (mean)Time per request: 6.848 [ms] (mean)Time per request: 6.848 [ms] (mean, across all concurrent requests)Transfer rate: 55.33 [Kbytes/sec] received
Accept-Ranges: bytesConnection: keep-aliveContent-Length: 20Content-Type: text/htmlDate: Sun, 09 Feb 2020 11:42:08 GMTETag: W/"20-1581064798000"Last-Modified: Fri, 07 Feb 2020 08:39:58 GMTServer: nginx/1.17.8
http { server_tokens off;...Accept-Ranges: bytesConnection: keep-aliveContent-Length: 150Content-Type: text/htmlDate: Sun, 09 Feb 2020 11:45:09 GMTETag: W/"150-1581063242000"Last-Modified: Fri, 07 Feb 2020 08:14:02 GMTServer: nginx
白名单配置:location / {allow 192.168.136.1;deny all;}黑名单设置:location / {deny 192.168.136.1;allow all;}
location /logs {autoindex on;root /opt/nginx/;}location ^/logs~*\.(log|txt)$ {add_header Content-Type text/plain;root /opt/nginx/;}
worker_processes 2; #调整到与CPU数量一致events {worker_connection 65535; #每个worker最大并发连接数}
[root@proxy ~]# ulimit -a #查看所有的属性值[root@proxy ~]# ulimit -Hn 10000 #临时设置硬限制[root@proxy ~]# ulimit -Sn 10000 #设置软限制[root@proxy ~]# vim /etc/security/limits.conf...* soft nofile 100000* hard nofile 100000用户/组 软/硬限制 需要限制的项目 限制的值
ab -n 1 -c 20000 http://192.168.1.131/
#设定Nginx状态访问地址location /NginxStatus {stub_status on;access_log off;}
./configure --prefix=/opt/nginx/ --with-http_stub_status_module
活跃的连接数量
active connections 总共处理了n个连接 , 成功创建n次握手, 总共处理了n个请求 server accepts handled requests 每个连接有三种状态waiting、reading、writing reading —读取客户端的Header信息数.这个操作只是读取头部信息,读取完后马上进入writing状态,因此时间很短 writing — 响应数据到客户端的Header信息数.这个操作不仅读取头部,还要等待服务响应,因此时间比较长。 waiting — 开启keep-alive后等候下一次请求指令的驻留连接. 正常情况下waiting数量是比较多的,并不能说明性能差。反而如果reading+writing数量比较多说明服务并发有问题。 查看Nginx并发进程数:ps -ef|grep nginx | wc -l 查看Web服务器TCP连接状态:netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
CLOSED //无连接是活动的或正在进行
LISTEN //服务器在等待进入呼叫 SYN_RECV //一个连接请求已经到达,等待确认 SYN_SENT //应用已经开始,打开一个连接 ESTABLISHED //正常数据传输状态/当前并发连接数 FIN_WAIT1 //应用说它已经完成 FIN_WAIT2 //另一边已同意释放 ITMED_WAIT //等待所有分组死掉 CLOSING //两边同时尝试关闭 TIME_WAIT //另一边已初始化一个释放 LAST_ACK //等待所有分组死掉 查看Web服务器TCP连接状态:netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
最后并发超过峰值后的服务端jvm监控显正常
到此这篇关于文章就结束了!
点关注,不迷路!如果本文对你有帮助的话不要忘记点赞支持哦
希望对大家有所帮助,有用的话点赞给我支持!
转载地址:http://uxfvi.baihongyu.com/