当前服务器已经配置了三台tomcat服务器,端口分别是8080,,8081,和8082,并且已经安装了nginx 服务器
目标:通过访问www.365codemall.com可以随机访问到这三台服务中的一个
1 配置三台tomcat 服务器,需要注意端口,端口不能重复
需要配置三个端口
(1)停止端口
(2)连接端口(主要监听来自浏览器的请求)
(3) AJP 1.3 端口(也是一个连接端口,负责和其他HTTP服务器建立连接,在tomcat和其他http服务器集成时,就会用到这个连接器,详细介绍请看 https://blog.csdn.net/qq_29832217/article/details/88668570)
2 修改 /etc/nginx/conf.d 文件下的 default.conf 文件,将我们的服务器列表加入到配置文件中
具体代码如下
upstream myproject {
server 127.0.0.1:8080 weight=3;
server 127.0.0.1:8081;
server 127.0.0.1:8082;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
#root /usr/share/nginx/html;
# index index.html index.htm;
proxy_pass http://myproject;
}
#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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
3 为了使我们更好的看到效果,我们修改一下tomcat 的主页,三个tomcat 的主页分别显示其端口,以8080 端口的tomcat为例
进入到webapps 目录下ROOT目录
cd /usr/etc/tomcate8/apache-tomcat-8.5.37-8080/webapps/ROOT
修改 ROOT目录下 index.jsp 文件
在如下图的位置加上一句 我是8080的猫,其他两个tomcat 也是做相同的修改,然后启动三个tomcat
4 启动nginx 服务
/usr/sbin/nginx
5 在地址栏输入 http://www.365codemall.com/,然后不断的刷新,我们就可以看到每次访问都是随机访问不同的tomcat