0%

配置Mac的双tomcat+nginx实现负载均衡

2018年3月8日 下午2:48

参看文章:

centos安装nginx:在配置文件中实现反向代理
2017/10/2 Mac中配置php的步骤记录
我Mac上nginx路径:_usr_local_etc_nginx

重要的分析:

  1. 这里一步是在没有开启nginx的时候,通过域名去访问8080,9080端口,也就说访问tomcat没有通过nginx。
  2. 对tomcat来说,到达他的方式有两种,不一定非得nginx启动

第一步:操作tomcat

  1. 删除原先的tomcat
    1. 我没有删,直接在~路径下创建了两个新的
  2. 解压两个tomcat,tomcat1、tomcat2
  3. 修改两个tomcat的URLEncoding=utf-8
    1. 参考centos安装配置apache
  4. 修改tomcat2的图标
    1. ~_tomcat2_webapps_ROOT_中
  5. 开启tomcat进行测试
    1. chmod u+x *.sh
  6. 修改mac环境变量_etc_profile
    1. 我在这里使用 sudo open -e ~/.bash_profile
    2. 因为sudo都没有权限操作_etc_profile
  7. 使环境变量生效
    1. source ~/.bash_profile
  8. 检验是否生效
    1. localhost:bin czh$ echo $CATALINA_BASE
  9. 修改tomcat2的catalina.sh文件的配置
    1. 防止:在tomcat2目录启动的时候catalina_base和catalina_home用的是tomcat1的
    2. OS后面两句话:

      1. export CATALINA_BASE=$CATALINA_2_BASE
      2. export CATALINA_HOME=$CATALINA_2_HOME
  10. 修改tomcat2的conf/server.xml的三个端口号
    1. 看这个tomcat集群的详细ppt
  11. 同时启动tomcat1,2
    1. 一个端口8080,一个端口9080

配置host,启动nginx

  1. 配置Mac的host,将域名指向127.0.0.1
  2. 测试通过域名+端口号,进入tomcat1,Tomcat2
    1. 注意这里并有启动nginx,也就说访问tomcat没有通过nginx
  3. 启动nginx
  4. 访问域名,不加端口号,看到nginx页面

配置nginx,实现负载均衡

  1. 配置nginx负载均衡
    1. 通过nginx的反向代理去访问nginx
    2. nginx -t
    3. sudo nginx -s reload
      1. 出现这个警告不用管:nginx: [warn] conflicting server name “localhost” on 0.0.0.0:80, ignored

重点提取:

下载tomcat7,我放在了~路径下

1
2
3
4
5
6
7
export CATALINA_BASE=~/tomcat1
export CATALINA_HOME=~/tomcat1
export TOMCAT_HOME=~/tomcat1

export CATALINA_2_BASE=~/tomcat2
export CATALINA_2_HOME=~/tomcat2
export TOMCAT_2_HOME=~/tomcat2

nginx的地址:_usr_local_etc_nginx


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
upstream www.imooc.com{
server 127.0.0.1:8080;
server 127.0.0.1:9080;
}

server {
listen 80;
autoindex on;
server_name www.imooc.com imooc.com;
access_log /usr/local/nginx/logs/access.log combined;
index index.html index.htm index.jsp index.php;
if ( $query_string ~* ".*[\;'\<\>].*" ){
return 404;
}

location / {
proxy_pass http://www.imooc.com;
}
}