天天看點

nginx配置tomcat叢集

配置的平台是windows10環境下,這裡tomcat叢集簡化為數量為2

首先在進階設定裡配置

CATALINA_HOME=${tomcat1}

CATALINA_BASE=${tomcat1}

TOMCAT_HOME=${tomcat1}

CATALINA_2_HOME=${tomcat2}

CATALINA_2_BASE=${tomcat2}

TOMCAT_HOME=${tomcat2}

tomcat1和tomcat2中conf/server.xml中Connector 中添加URIEncoding="UTF-8" 保證tomcat編碼

可替換tomcat2中/webapps/ROOT下tomcat.png圖檔,友善觀察。

tomcat2叢集中對應的三個地方修改端口号,端口号修改分别在原先的基礎上加1000,下面是修改後的效果圖

nginx配置tomcat叢集
nginx配置tomcat叢集
nginx配置tomcat叢集

接下來配置/bin檔案下配置檔案:

windows 10 下不同于linux和mac的修改處為

将tomcat2中catalina.bat跟startup.bat中所有CATALINA_HOME替換為CATALINA_2_HOME,将CATALINA_BASE替換為CATALINA_2_BASE。

在linux和mac環境下修改為将catalina.sh中添加如下兩個export語句

nginx配置tomcat叢集

修改hosts檔案,在裡面添加127.0.0.1  www.imooc.com。

接下來在nginx的/conf下建立vhost檔案夾建立imooc.com.conf檔案,裡面的配置語句為:

upstream www.imooc.com{

    server 127.0.0.1:8080 weight=1;

    server 127.0.0.1:9080 weight=3;

    #server www.imooc.com:8080;

    #server www.imooc.com:9080;

}

server {

    listen 80;

    autoindex on; 

    server_name imooc.com www.imooc.com;

    access_log c:/access.log combined;

    index index.html index.htm index.jsp index.php;

    #error_page 404 /404.html;

    if ( $query_string ~* ".*[\;'\<\>].*" ){ 

        return 404;

    }

    location / {

       proxy_pass http://www.imooc.com;

    } 

}

其中server_name 有imooc.com,www.imooc.com,以空格分割,upstream後面是需要轉發的域名,大括号裡是轉發的位址。

location中proxy_pass 就是需要nginx轉發的域名.

然後在/conf/nginx.conf下添加如下語句,與該語句平行的是server{}

nginx配置tomcat叢集

windows下操作nginx(版本為1.14.0)指令如下:

nginx cmd指令

start nginx //啟動nginx

nginx -s stop // 停止nginx

nginx -s reload // 重新加載配置檔案

nginx -s quit // 退出nginx

nginx -t    //檢查配置檔案是否正确

         nginx -v //檢視nginx版本号

然後啟動兩個tomcat,并啟動nginx進行測試;

注意:此處有坑,如果打開任務管理器有多個nginx程序的話,會出現錯誤,可通過關閉程序解決,此前卡在這裡一直好久。

建議使用火狐浏覽器,谷歌浏覽器(可能因為緩存?)效果不是很明顯。

繼續閱讀