比较nginx和Tomcat处理静态资源能力

机器

192.168.64.135(安装nginx)

192.168.64.140(安装Tomcat)

环境搭建

135机器

Nginx性能优化(五)_Nginx

index.html页面

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1 style="color:blue">this is sb wrz 135 nginx</h1>
<img src="/images/katong.jpeg" height="200" width="200" />
<img src="/images/shu.jpeg" height="200" width="200" />
</body>
</html>


image目录文件

katong.jpeg  shu.jpeg (你换成你的图片文件)

配置文件

/usr/local/nginx/conf/test/test.conf

server {
listen 85;
server_name 192.168.64.135;

location / {
root html;
try_files $uri $uri/ @java;
}

location @java {
proxy_pass http://192.168.64.140:8080;
}
}


主配置文件记得引用test.conf

include test/*.conf;

重启服务

./sbin/nginx -s reload

浏览器测试

Nginx性能优化(五)_Nginx_02

 

140机器

/usr/local/tomcat/tomcat8080/webapps/ROOT

下有文件index.jsp 目录images

index.jsp页面

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body>

<h1 style="color:blue">this is sb wrz 140 tomcat</h1>
<img src="/images/katong.jpeg" height="200" width="200" />
<img src="/images/shu.jpeg" height="200" width="200" />

</body>
</html>


images目录文件

katong.jpeg  shu.jpeg (你换成你的图片文件)

浏览器测试

Nginx性能优化(五)_Nginx_03

压力测试

135 140安装yum install -y httpd-tools 用法等百度一下

135 140 都是静态资源 内容一样 测试nginx和Tomcat处理静态资源能力

140 Tomcat测试

ab -n 10000 -c 200 http://192.168.64.140:8080/

Nginx性能优化(五)_nginx_04

135nginx测试

ab -n 10000 -c 200 http://192.168.64.135:85/

Nginx性能优化(五)_nginx_05

结论 

nginx处理静态资源强于Tomcat处理静态资源能力

影响性能的指标

1、网络 (1)网络的流量 (2)网络是否丢包 (3)这些会影响http的请求与调用

2、系统 (1)硬件有没有磁盘损坏,磁盘速率 (2)系统的负载、内存、系统稳定性

3、服务 (1)连接优化。请求优化 (2)根据业务形态做对应的服务设置

4、程序 (1)接口性能 (2)处理速度 (3)程序执行效率

5、数据库

系统性能优化

文件句柄,Linux一切皆文件,文件句柄可以理解为就是一个索引,文件句柄会随着我们进程的调用频繁增加, 系统默认文件句柄是有限制的,不能让一个进程无限的调用,所以我们需要限制每个 进程和每个服务使用多大的文件句柄,文件句柄也是必须要调整的优化参数。

#查看文件句柄数 ulimit -n

#查看一个服务打开多少文件句柄 lsof -p pid

#查看一共打开多少文件句柄数 lsof -i | awk '{print $2}' |wc -l

1.设置文件句柄数

vim /etc/security/limits.conf

1.系统全局修改文件句柄

# * 代表所有用户,soft代表提醒,hard直接限制,nofile指定打开最大文件数量

* - nofile 65535

* soft nofile 65535

* hard nofile 65535

2.用户局部修改

root - nofile 65535

root soft nofile 65535

root hard nofile 65535

3.进程指定修改 #针对nginx进程,nginx自带配置,

nginx.conf worker_rlimit_nofile 65535;

4.调整内核参数:让time_wait状态重用(端口重用)[flag]

vim /etc/sysctl.conf

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_timestamps = 1

sysctl -p #可以查看我们添加的内核参数 

sysctl -a #可以查看所有内核参数

代理服务优化

1开启长链接

upstream http_backend {

server 127.0.0.1:8080;

keepalive 16; #长连接

}

2静态资源缓存

server {

listen 80;

server_name static.test.com;

 

location ~ .*\.(jpg|gif|png)$ {

expires 7d;

}

location ~ .*\.(js|css)$ {

expires 30d;

}

}

3静态资源读取

sendfile on | off;

4静态资源压缩

gzip on | off;

5CPU亲和配置

worker_processes auto;

worker_cpu_affinity auto;

 

通用化配置

#nginx优化总结,nginx通用优化配置文件

 

[root@nginx ~]# cat nginx.conf

user www; # nginx进程启动用户

worker_processes auto; #与cpu核心一致即可

worker_cpu_affinity auto; # cpu亲和

 

error_log /var/log/nginx/error.log warn; # 错误日志

pid /run/nginx.pid;

worker_rlimit_nofile 35535; #每个work能打开的文件描述符,调整至1w以上,负荷较高建议2-3w

 

events {

use epoll; # 使用epoll高效网络模型

worker_connections 10240; # 限制每个进程能处理多少个连接,10240x[cpu核心]

}

 

http {

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"';

 

#定义json日志格式

log_format json_access '{"@timestamp":"$time_iso8601",'

'"host":"$server_addr",'

'"clientip":"$remote_addr",'

'"size":$body_bytes_sent,'

'"responsetime":$request_time,'

'"upstreamtime":"$upstream_response_time",'

'"upstreamhost":"$upstream_addr",'

'"http_host":"$host",'

'"url":"$uri",'

'"domain":"$host",'

'"xff":"$http_x_forwarded_for",'

'"referer":"$http_referer",'

'"status":"$status"}';

 

access_log /var/log/nginx/access.log main; # 访问日志

 

server_tokens off; # 禁止浏览器显示nginx版本号

client_max_body_size 200m; # 文件上传大小限制调整

 

# 文件高效传输,静态资源服务器建议打开

sendfile on;

tcp_nopush on;

# 文件实时传输,动态资源服务建议打开,需要打开keepalive

tcp_nodelay on;

keepalive_timeout 65;

 

# Gzip 压缩

gzip on;

gzip_disable "MSIE [1-6]\."; #针对IE浏览器不进行压缩

gzip_http_version 1.1;

gzip_comp_level 2; #压缩级别

gzip_buffers 16 8k; #压缩的缓冲区

gzip_min_length 1024; #文件大于1024字节才进行压缩,默认值20

gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript image/jpeg image/jpg image/gif image/png;

 

# 虚拟主机

include /etc/nginx/conf.d/*.conf;

}