[例子] nginx负载均衡搭建及测试
一、Nginx + Tomcat 负载均衡测试(负载均衡+部分静态图片处理)
ok 首先你得有一个Ubuntu或者什么其他的linux.
|
jdk /usr/java/jdk1.7
Nginx /usr/nginx-1.4.2
redis /usr/redis3.2.5-1
tomcat /usr/apache/tomcat-7-1
/usr/apache/tomcat-7-2
/usr/apache/tomcat-7-3
|
1配置tomcat
#--------------------------------------------------------------------
#set tomcat path @olddoor
### first tomcat ###
CATALINA_BASE=/usr/apache/tomcat-7-1
CATALINA_HOME=/usr/apache/tomcat-7-1
TOMCAT_HOME=/usr/apache/tomcat-7-1
export CATALINA_BASE CATALINA_HOME TOMCAT_HOME
### second tomcat ###
CATALINA_2_BASE=/usr/apache/tomcat-7-2
CATALINA_2_HOME=/usr/apache/tomcat-7-2
TOMCAT_2_HOME=/usr/apache/tomcat_7-2
export CATALINA_2_BASE CATALINA_2_HOME TOMCAT_2_HOME
### third tomcat ###
CATALINA_3_BASE=/usr/apache/tomcat-7-3
CATALINA_3_HOME=/usr/apache/tomcat-7-3
TOMCAT_HOME=/usr/apache/tomcat-7-3
export CATALINA_3_BASE CATALINA_3_HOME TOMCAT_3_HOME
#set java path--------------------------------------------------------
JAVA_HOME=/usr/java/jdk1.7
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
export JAVA_HOME=
export PATH=
export CLASSPATH=
###以上三个环境变量就是JAVA的环境变量可以不写###
export CATALINA_BASE=$CATALINA_2_BASE #配置对应的tomcat变量即可
export CATALINA_HOME=$CATALINA_2_HOME
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
hello<br/>
<img id="img1" src=/test/static/404-1.jpg />
<img id="img1" src=/test/jpgs/123.jpg />
</body>
</html>

2配置nginx
#user nobody; # user root root #Nginx所在的用户和用户组
user root root; #Nginx所在的用户和用户组
worker_processes 1;# 启动的工作进程数量
#错误日志存放路径
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
error_log logs/error.log info; #add by olddoor-----------
#pid logs/nginx.pid;
#add by olddoor-----------
#add end------------------
events {
worker_connections 1024;
}
http {
include 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"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
#add by olddor---------------------
upstream localhost {
#发到localhost上的请求,通过Nginx转发到实际处理请求的服务器
server 192.168.64.132:8080 weight=1;
server 192.168.64.132:8081 weight=1;
server 192.168.64.132:8082 weight=1;
}
#add by olddor over----------------
server {
listen 8000; #宿主机访问虚拟机的80端口好像有点问题,建议不使用80端口
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_connect_timeout 3; #add by olddoor
proxy_send_timeout 30; #add by olddoor
proxy_read_timeout 30; #add by olddoor
proxy_pass http://localhost; #add by olddoor
#root html; #ignore by olddoor
#index index.html index.htm; #ignore by olddoor
}
location /test/jpgs/ {
alias /data/imgs/;
}
#location /jpgs
#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;
}
# 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;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
3 使用nginx管理部分静态数据
4 性能测试对比
apt-get install apache2-utils
yum install httpd-tools
#用于描述每个请求处理时间的分布情况,例如:50% 28 50%请求处理时间不超过28毫秒 (这里所指的处理时间是指:Time per request )
Percentage of the requests served within a certain time (ms)
50% 28
66% 32
75% 34
80% 35
90% 53
95% 62
98% 65
99% 66
100% 1823 (longest request)
second表示当前测试的服务器每秒可以处理16.54个静态html的请求事务,后面的mean表示平均。这个数值表示当前机器的整体性能,值越大越好。
Requests per second: 547.04 [#/sec] (mean)
Time per request: 1828.016 [ms] (mean)
Time per request: 1.828 [ms] (mean, across all concurrent requests)
Transfer rate: 76.93 [Kbytes/sec] received
Percentage of the requests served within a certain time (ms)
50% 107
66% 201
75% 243
80% 257
90% 334
95% 360
98% 1116
99% 1117
100% 1118 (longest request)
second表示当前测试的服务器每秒可以处理16.54个静态html的请求事务,后面的mean表示平均。这个数值表示当前机器的整体性能,值越大越好。
Requests per second: 873.38 [#/sec] (mean)
Time per request: 1144.983 [ms] (mean)
Time per request: 1.145 [ms] (mean, across all concurrent requests)
Transfer rate: 216.83 [Kbytes/sec] received
This is ApacheBench, Version 2.3 <$Revision: 1528965 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
Total of 9967 requests completed
Server Hostname: localhost
Server Port: 80
Time taken for tests: 1.145 seconds
Complete requests: 1000
Failed requests: 551
(Connect: 0, Receive: 0, Length: 551, Exceptions: 0)
Non-2xx responses: 1000
Keep-Alive requests: 0
Total transferred: 254220 bytes
HTML transferred: 105792 bytes
second表示当前测试的服务器每秒可以处理16.54个静态html的请求事务,后面的mean表示平均。这个数值表示当前机器的整体性能,值越大越好。
Requests per second: 873.38 [#/sec] (mean)
Time per request: 1144.983 [ms] (mean)
Time per request: 1.145 [ms] (mean, across all concurrent requests)
Transfer rate: 216.83 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 16 26 4.6 27 33
Processing: 16 155 174.5 88 1101
Waiting: 13 155 174.6 88 1101
Total: 44 181 172.5 107 1118
Percentage of the requests served within a certain time (ms)
50% 107
66% 201
75% 243
80% 257
90% 334
95% 360
98% 1116
99% 1117
100% 1118 (longest request)
ab -kc 1000 -n 1000 http://192.168.64.132:80/test/ |tee /usr/111.txt #将结果保存到/usr/111.txt中
二、Nginx + Tomcat +Redis (用户session会话集中管理)
安装nginx、tomcat、redis服务
| 软件 | version | ip:port |
|---|---|---|
| Nginx | 1.4.6 | 192.168.127.58 : 8000 |
| Redis | 3.2.5-1 | 192.168.127.58 : 7001 |
| tomcat-7-1 |
7.0.73 |
192.168.127.58:8080 |
| tomcat-7-2 |
7.0.73 |
192.168.127.58:8081 |
| tomcat-7-3 |
7.0.73 |
192.168.127.58:8082 |
2、测试:
<body>
response from tomcat_3<br/>
<%=session.getId() %>
</body>
- 添加jedis-2.0.0.jar、tomcat-redis-session-manager-1.2-tomcat-7-java-7、commons-pool-1.3.jar 三个jar到tomcat的lib目录下;
注意不可用commons-pool-1.2.jar
- 修改tomcat的conf/context.xml 文件;
<!--配置对应redis的服务端口 并非后台端口-->
<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
<Manager className="com.radiadesign.catalina.session.RedisSessionManager"
host="localhost"
port="7001"
database="0"
maxInactiveInterval="60" />
response from tomcat_1
E47C459C5B9800294F408D0FFCFEFFD8
response from tomcat_2
E47C459C5B9800294F408D0FFCFEFFD8
response from tomcat_3
E47C459C5B9800294F408D0FFCFEFFD8
- 使用 redis-cli 连接 redis 服务器,查看会显示有 “E47C459C5B9800294F408D0FFCFEFFD8” key的 session 数据,value为序列化数据。
登录redis-cli
keys *
[例子] nginx负载均衡搭建及测试的更多相关文章
- 使用Nginx负载均衡搭建高性能.NETweb应用程序(转)
一.遇到的问题 当我们用IIS服务器部署了一个web应用以后,当很多用户高并发访问的时候,客户端响应就会很慢,客户的体验就会很差,由于IIS接受到客户端请求的时候,就会创建一个线程,当线程达到几千个时 ...
- 使用Nginx负载均衡搭建高性能.NETweb应用程序二
在文章<使用Nginx负载均衡搭建高性能.NETweb应用程序一>中,让我们对Nginx有了一个初步认识,下面我们将在windows平台下面使用Nginx演示集群部署我们的web应用. 一 ...
- Nginx 负载均衡搭建
配置文件Nginx/conf/nginx.conf 什么是负载均衡呢? 由于目前现有网络的各个核心部分随着业务量的提高,访问量和数据流量的快速增长,其处理能力和计算强度也相应地增大,使得单一的服务器设 ...
- Nginx负载均衡搭建(Window与Linux)
windows上搭建nginx负载均衡 1.准备几台http服务器软件,这里选用一台apache一台tomcat apache(windows)下载链接:https://www.apachehaus. ...
- nginx负载均衡 理解与测试
Nginx负载均衡概述 Web服务器,直接面向用户,往往要承载大量并发请求,单台服务器难以负荷,我使用多台WEB服务器组成集群,前端使用Nginx负载均衡,将请求分散的打到我们的后端服务器集群中,实现 ...
- 使用Nginx负载均衡搭建高性能.NETweb应用程序一
一.遇到的问题 当我们用IIS服务器部署了一个web应用以后,当很多用户高并发访问的时候,客户端响应就会很慢,客户的体验就会很差,由于IIS接受到客户端请求的 时候,就会创建一个线程,当线程达到几千个 ...
- nginx负载均衡搭建phpmyadmin加入redis了解session会话原理
myphpadmin项目理解cookie和session 当我们平时上网的时候,在刷新之后或者退出浏览器再次打开浏览器不需要登陆网页了,这就是利用了cookie和session: 环境配置 hostn ...
- linux下nginx负载均衡搭建
[一.Nginx能做什么] 1.http服务器.Nginx是一个http服务可以独立提供http服务.可以做网页静态服务器. 2.虚拟主机.可以实现在一台服务器虚拟出多个网站.例如个人网路使用的虚拟主 ...
- Centos7 minimal 系列之Nginx负载均衡搭建(四)
一.Nginx搭建请参考我的上篇文章 http://www.cnblogs.com/WJ--NET/p/8143899.html 二.在IIS上搭建2个网站 三.配置nginx 虚拟机和主机网络互通请 ...
随机推荐
- 编写一个求和函数sum,使输入sum(2)(3)或输入sum(2,3),输出结果都为5
昨天的笔试题,做的一塌糊涂,题目考的都很基础而且很细,手写代码对我来说是硬伤啊.其中有一道是这个,然而看到题目的时候,根本没有想到arguments:然后现在就恶补一下. arguments:用在函数 ...
- LINUX 学习笔记 账号与群组的管理
LINUX 账号与群组的管理 UID:UserID 保存文件:/etc/passwd GID:GroupID 保存文件:/etc/group /etc/passwd 文件结构 一行代表一个账号,里面还 ...
- nginx——优化 Nginx 连接超时时间
1. 什么是连接超时 (1) 举个例子,某饭店请了服务员招待顾客,但是现在饭店不景气,因此要解雇掉一些服务员,这里的服务员就相当于 Nginx 服务建立的连接 (2) 当服务器建立的连接没有接收处理请 ...
- JavsScript学习---快速排序
<script type="text/javascript"> /** * “快速排序”的思想很简单,整个排序过程只需要三步: * (1)在数据集之中,找一个基准点 * ...
- 集合List的排序
自从出现了泛型和LINQ,对于集合的排序变得更简单了. //倒序 list.OrderByDescending(p=> p.a).ThenByDescending(p => p.b); / ...
- oracle 根据一个表生成另一个新表和一个现有表给一个新的表赋值
1,添加表B ,和A表表结构相同(带数据) create table B as select * from A; 2,添加表B ,和A表表结构相同(不带带数据) create table B as ...
- php $_FILES错误说明 以及图片前端图片上传失败。
我的另一个原文:https://blog.csdn.net/qq_36570464/article/details/80692241 今天被一个问题弄了好久, 先看代码: 前端: <form m ...
- redis概述(一)
什么是NoSql? 为了解决高并发.高可用.高可扩展,大数据存储等一系列问题而产生的数据库解决方案,就是NoSql. NoSql,叫非关系型数据库,它的全名Not only sql.它不能替代关系型数 ...
- easyui Tree模拟级联勾选cascadeCheck,节点选择,父节点自动选中,节点取消,父节点自动取消选择,节点选择,所有子节点全部选择,节点取消,所有子节点全部取消勾选
最近项目中用到easyui tree,发现tree控件的cascadeCheck有些坑,不像miniui 的tree控件,级联勾选符合业务需求,所以就自己重新改写了onCheck事件,符合业务需求.网 ...
- 完美解决win10家庭版本系统无法远程连接问题
版权声明:本文转载.原文: https://blog.csdn.net/rainmaple20186/article/details/80913191 近期接入同一局域网的服务器,发现在连接的时候,报 ...