您的位置:首页 > 运维架构 > Tomcat

采用nginx让多个tomcat实现负载均衡

2016-03-03 20:03 567 查看
由于目录已将项目正式部署并发布了,但由于时不时地会出现bug,修复bug再次提交后,会让项目出现短时间的无法访问的问题,虽然时间虽短,但还是会影响用户的体验。为了不让用户察觉出项目的变动,于是我便采用了用nginx来实现负载均衡,主要步骤记录如下:

1.配置nginx安装源

vim /etc/yum.repos.d/nginx.repo

#nginx.repo

<span style="white-space:pre">	</span>[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

2.安装nginx

如已安装了nginx,需要升级,执行update即可

yum update nginx

如未安装,执行如下命令,安装即可

yum install nginx -y

nginx -v //查看版本





3.查看nginx的安装目录

whereis nginx

4.编辑nginx配置文件

cd /etc/nginx

vim nginx.conf

在此,附上nginx.conf的源码:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

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

access_log  /var/log/nginx/access.log  main;

sendfile        on;
#tcp_nopush     on;

keepalive_timeout  65;

#gzip  on;

include /etc/nginx/conf.d/*.conf;
}
从上面可以看出,还调用了/etc/nginx.conf.d/*.conf文件



再附上default.conf源码:

upstream school {
server 192.168.1.103:80;
server 192.168.1.100:8080;
server 192.168.1.101:80;
}

server {
listen       80;
server_name  school;

#charset koi8-r;
#access_log  /var/log/nginx/log/host.access.log  main;

location / {
# root   /usr/share/nginx/html;

proxy_pass http://school; 
#  index  index.html index.htm;
}

#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   /usr/share/nginx/html;
}

}
如此一来,就完成了3台服务器的简单负载了。

5.测试nginx配置是否正确

nginx -t





都显示的ok和successful表示配置没得什么问题

6.启动nginx

启动下nginx就可以了
service nginx start
再输入nginx的地址测试一下:





由此看出,每执行一次,所到达的tomcat服务都是不一样的,OK!大功告成!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: