Nginx优化
一、优化概述
1.需要了解
1、首先需要了解我们当前系统的结构和瓶颈,了解当前使用的是什么,运行的是什么业务,都有哪些服务,了解每个服务最大能支撑多少并发。比如nginx作为静态资源服务并发是多少,最高瓶颈在哪里,能支持多少qps(每秒查询率)的访问请求,那我们怎么得出这组系统结构瓶颈呢,比如top查看系统的CPU负载、内存使用率、总得运行进程等,也可以通过日志去分析请求的情况,当然也可以通过我们前面介绍到的stub_status模块查看当前的连接情况,也可以对线上的业务进行压力测试(低峰期),去了解当前这套系统能承担多少的请求和并发,以做好响应的评估。这个是我们做性能优化最先考虑的地方。
2、其次我们需要了解业务模式,虽然我们是做性能优化,但每一个性能的优化都是为业务所提供的服务的,我们需要了解每个业务接口的类型,比如:电商网站中的抢购模式,这种情况下,平时没什么流量,但到了抢购时间流量会突增。我们还需要了解系统层次化的结构,比如:我们使用nginx做的是代理、还是动静分离、还是后端直接服务用户,那么这个就需要我们对每一层做好相应的梳理。以便更好的服务业务。
3、最后我们需要考虑性能与安全,往往注重了性能,但是忽略了安全。往往过于注重安全,对性能又会产生影响。比如:我们在设计防火墙功能时,检测过于严密,这样就会给性能带来影响。那么如果对于性能完全追求,却不顾服务的安全,这个也会造成很大的隐患,所以需要评估好两者的关系,把握好两者的孰重孰轻。以及整体的相关性,权衡好对应的点。
1.首先需要了解我们当前系统的结构和瓶颈
2.其次我们需要了解业务模式
3.我们还需要了解系统层次化的结构
4.最后我们需要考虑性能与安全
2.从哪些方面入手
OSI七层模型:物理层,数据链路层,网络层,传输层,会话层,表示层,应用层
1.硬件:
1)nginx做负载均衡只需要cpu核心多一些
2)nginx做静态资源存储,磁盘大一些
3)nginx做动态资源代理,CPU
4)ES,redis服务内存需要大一些
2.网络层:
1)丢包、延迟
2)带宽
3.系统:
1)文件描述符
2)端口复用
4.应用:tcp长连接
5.服务:服务的针对性优化
3.影响性能的指标
1.网络
2.系统
3.服务
4.程序
5.数据库
二、ab测试工具
1.安装ab测试工具
#查看命令所在的包
[root@web01 ~]# yum provides ab
#安装ab
[root@web01 ~]# yum install -y httpd-tools
2.工具使用
[root@web01 ~]# ab -n 200 -c 2 http://www.baidu.com/
-n 请求的次数
-c 请求的并发数
-k 开启长连接
Usage: ab [options] [http[s]://]hostname[:port]/path
3.配置nginx网站
[root@web01 conf.d]# vim linux.ab.com.conf
server {
listen 80;
server_name linux.ab.com;
root /code;
location / {
try_files $uri $uri/ @java;
}
location @java {
proxy_pass http://172.16.1.7:8080;
}
}
[root@web01 conf.d]# echo "test nginx web" > /code/index.html
4.配置hosts压测nginx访问静态资源
[root@web01 conf.d]# vim /etc/hosts
10.0.0.7 linux.ab.com
[root@web01 conf.d]# ab -n 10000 -c 200 http://linux.ab.com/
Server Software: nginx/1.18.0
Server Hostname: linux.ab.com
Server Port: 80
Document Path: /
Document Length: 15 bytes
Concurrency Level: 200
Time taken for tests: 1.084 seconds
Complete requests: 10000
Failed requests: 0
Write errors: 0
Total transferred: 2590000 bytes
HTML transferred: 150000 bytes
Requests per second: 9226.11 [#/sec] (mean)
Time per request: 21.678 [ms] (mean)
Time per request: 0.108 [ms] (mean, across all concurrent requests)
Transfer rate: 2333.56 [Kbytes/sec] received
5.压测tomcat访问静态资源
[root@web01 conf.d]# mv /code/ /codebak
[root@web01 conf.d]# ab -n 10000 -c 200 http://linux.ab.com/
Server Software: nginx/1.18.0
Server Hostname: linux.ab.com
Server Port: 80
Document Path: /
Document Length: 20 bytes
Concurrency Level: 200
Time taken for tests: 3.025 seconds
Complete requests: 10000
Failed requests: 0
Write errors: 0
Total transferred: 2720000 bytes
HTML transferred: 200000 bytes
Requests per second: 1739.70 [#/sec] (mean)
Time per request: 114.962 [ms] (mean)
Time per request: 0.575 [ms] (mean, across all concurrent requests)
Transfer rate: 462.11 [Kbytes/sec] received
6.压测httpd访问静态资源
[root@web01 conf.d]# yum install -y httpd
[root@web01 conf.d]# echo "test httpd" > /var/www/html/index.html
[root@web01 conf.d]# systemctl stop nginx
[root@web01 conf.d]# systemctl start httpd
[root@web01 conf.d]# ab -n 10000 -c 200 http://10.0.0.7/
Server Software: Apache/2.4.6
Server Hostname: 10.0.0.7
Server Port: 80
Document Path: /
Document Length: 11 bytes
Concurrency Level: 200
Time taken for tests: 2.888 seconds
Complete requests: 10000
Failed requests: 0
Write errors: 0
Total transferred: 2810000 bytes
HTML transferred: 110000 bytes
Requests per second: 3462.37 [#/sec] (mean)
Time per request: 57.764 [ms] (mean)
Time per request: 0.289 [ms] (mean, across all concurrent requests)
Transfer rate: 950.12 [Kbytes/sec] received
7.结论
nginx处理静态资源的速度比其他web服务要快
8.tomcat正经安装方式
#1.下载或上传tomcat包
[root@web01 ~]# mkdir /service
[root@web01 ~]# cd /service
[root@web01 service]# rz apache-tomcat-8.5.51.tar.gz
#2.解压代码包
[root@web01 service]# tar xf apache-tomcat-8.5.51.tar.gz
#3.配置java环境
1.上传并解压至指定文件夹
[root@web01 service]# tar xf jdk-8u40-linux-x64.gz -C /service/
[root@web01 service]# mv jdk1.8.0_40 java1.8
2.修改添加环境变量
[root@web01 service]# vim /etc/profile.d/java.sh
export JAVA_HOME=/service/java1.8
export JRE_HOME=/service/java1.8/jre
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
export PATH=$PATH:$JAVA_HOME/bin
[root@web01 service]# source /etc/profile
#4.配置tomcat页面
[root@web01 service]# echo "linux_tomcat" > apache-tomcat-8.5.51/webapps/ROOT/index.html
#5.启动tomcat,启动的时候最好看着日志
[root@web01 service]# /service/apache-tomcat-8.5.51/bin/startup.sh && tail -f /service/apache-tomcat-8.5.51/logs/catalina.out
三、系统优化
1.查看文件句柄数
#1.查看文件句柄数设置
[root@lb01 ~]# ulimit -n
65535
#2.查看打开的文件句柄数
[root@lb01 ~]# lsof | wc -l
2945
[root@web01 ~]# lsof | wc -l
12490
#3.查看指定服务的打开文件句柄数
[root@web01 ~]# lsof -p 13592 | wc -l
2.设置文件句柄数
1)系统全局的设置
[root@web01 ~]# vim /etc/security/limits.conf
* - nofile 65535
* soft nofile 65535
* hard nofile 65535
* #所有用户
- #当超过设置的文件句柄数时,什么都不干
soft #当超过设置的文件句柄数时,仅提示
hard #当超过设置的文件句柄数时,直接限制
2)用户局部设置
[root@web01 ~]# vim /etc/security/limits.conf
root - nofile 65535
root soft nofile 65535
root hard nofile 65535
3)针对服务设置
[root@lb01 ~]# vim /etc/nginx/nginx.conf
user www;
worker_processes 1;
worker_rlimit_nofile 65535;
4)系统通用优化
[root@lb01 ~]# vim /etc/sysctl.conf
net.ipv4.tcp_fin_timeout = 2
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_keepalive_time = 600
net.ipv4.ip_local_port_range = 4000 65000
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_max_tw_buckets = 36000
net.ipv4.route.gc_timeout = 100
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_synack_retries = 1
net.core.somaxconn = 16384
net.core.netdev_max_backlog = 16384
net.ipv4.tcp_max_orphans = 16384
net.ipv4.ip_forward = 1
四、代理服务优化
1.配置用户访问负载均衡的长连接
[root@lb01 ~]# vim /etc/nginx/nginx.conf
... ...
http {
... ...
keepalive_timeout 65;
... ...
}
2.配置负载均衡代理到web的长连接
[root@lb01 ~]# vim /etc/nginx/conf.d/linux.keep.com.conf
upstream tomcat {
server 172.16.1.7:8080;
keepalive 8; #配置开启长连接
}
server {
listen 80;
server_name linux.keep.com;
location / {
proxy_pass http://tomcat;
proxy_http_version 1.1; #代理带后端的http版本
proxy_set_header Connection ""; #清除请求头字段
include proxy_params;
}
}
3.配置web代理到php保持长连接
[root@web01 ~]# vim /etc/nginx/conf.d/linux.wp.com.conf
upstream php_server {
server 127.0.0.1:9000;
}
server {
listen 80;
server_name linux.wp.com;
location / {
root /code/wordpress;
index index.php;
}
location ~* \.php$ {
fastcgi_pass php_server;
fastcgi_param SCRIPT_FILENAME /code/wordpress/$fastcgi_script_name;
fastcgi_param HTTPS on;
fastcgi_keep_conn on;
include fastcgi_params;
}
}
4.代理优化配置
[root@lb01 ~]# cat /etc/nginx/proxy_params
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
proxy_send_timeout 60s;
proxy_buffering on;
proxy_buffer_size 8k;
proxy_buffers 8 8k;
proxy_next_upstream http_500 http_502 http_503 http_504;
五、静态资源优化