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

原创
2016-07-29 09:00:50 679浏览

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

1.配置nginx安装源

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

	[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 //查看版本

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

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


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文件

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

再附上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

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

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

6.启动nginx

启动下nginx就可以了

service nginx start

再输入nginx的地址测试一下:

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

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

由此看出,每执行一次,所到达的tomcat服务都是不一样的,OK!大功告成!!!


以上就介绍了采用nginx让多个tomcat实现负载均衡,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。