• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

php 搞代码 4年前 (2022-01-23) 17次浏览 已收录 0个评论

由于目录已将项目正式部署并发布了,但由于时不时地会出现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 //查看版本

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   /本¥文来源gaodai$ma#com搞$代*码*网(搞代gaodaima码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!大功告成!!!

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


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:采用nginx让多个tomcat实现负载均衡

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址