Nginx配置实战(通过nginx随机访问本地的三台tomcat)

 当前服务器已经配置了三台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

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

www.365codemall.com

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值