Nginx并搭建Tomcat负载均衡环境

http://blog.csdn.net/zhoufoxcn/article/details/72872524

http://blog.csdn.net/duguxingfeng/article/details/78929472

安装 tomcat 为测试Nginx反向代理

1.下载

# wget http://mirrors.shuosc.org/apache/tomcat/tomcat-8/v8.0.48/bin/apache-tomcat-8.0.48.tar.gz

2.解压

# tar -zxvf apache-tomcat-8.0.48.tar.gz

3.启动测试

# cd /apache-tomcat-8.0.48/bin

# ./startup.sh

[html]  view plain  copy
  1. [root@localhost bin]# ./startup.sh   
  2. Using CATALINA_BASE:   /usr/software/apache-tomcat-8.0.48  
  3. Using CATALINA_HOME:   /usr/software/apache-tomcat-8.0.48  
  4. Using CATALINA_TMPDIR: /usr/software/apache-tomcat-8.0.48/temp  
  5. Using JRE_HOME:        /usr/java/jdk1.8.0_121/jre  
  6. Using CLASSPATH:       /usr/software/apache-tomcat-8.0.48/bin/bootstrap.jar:/usr/software/apache-tomcat-8.0.48/bin/tomcat-juli.jar  
  7. Tomcat started.  
启动成功 访问:http://localhost:8080


4.打开 nginx的配置文件

# cd /usr/local/nginx/conf

复制一份配置文件 命名为: nginx.conf.bak

#cp nginx.conf nginx.conf.bak

# vim nginx.conf

找到: location / 处,添加:  proxy_pass http://localhost:8080; http://localhost:8080;为tomcat的访问路径


[html]  view plain  copy
  1. server {  
  2.   <strong>  listen       80; #nginx的监听端口</strong>  
  3.     server_name  localhost;  
  4.   
  5.     #charset koi8-r;  
  6.   
  7.     #access_log  logs/host.access.log  main;  
  8.   
  9.     location / {  
  10.         root   html;  
  11.         index  index.html index.htm;  
  12.      <span style="color:#cc0000;">   proxy_pass http://localhost:8080;</span>  
  13.     }  
  14.   
  15.     #error_page  404              /404.html;  
  16.   
  17.     # redirect server error pages to the static page /50x.html  
  18.     #  
  19.     error_page   500 502 503 504  /50x.html;  
  20.     location = /50x.html {  
  21.         root   html;  
  22.     }  

修改之后,保存。重启nginx

# ./usr/local/nginx/sbin/nginx -s reload

访问:http://localhost:80 即可访问 tomcat的路径



四、Nginx 负载均衡

1.此处需要部署两个tomcat 进入到第四步载的tomcat的路径下

# cp -r apache-tomcat-8.0.48 tomcat1

# cp -rapache-tomcat-8.0.48 tomcat2

2.修改tomcat配置文件

tomcat1 端口设定为8080 ;tomcat1的配置无需修改

tomcat2 端口设定为8090

# cd /tomcat2/conf

# vim server.xml

修改

<Server port="8005" shutdown="SHUTDOWN">

为:<Server port="8015" shutdown="SHUTDOWN">


<Connector port="8080" protocol="HTTP/1.1"  connectionTimeout="20000"  redirectPort="8443" />

为:<Connector port="8090" protocol="HTTP/1.1" connectionTimeout="20000"  redirectPort="8443" />


<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

为: <Connector port="8019" protocol="AJP/1.3" redirectPort="8443" />


3.在tomcat1和tomcat2下创建测试站点

# cd /tomcat1/webapp

# madir test

# vi index.html

添加

this is tomcat1 index

esc :wq 保存推出。


同理在tomcat2下创建test与indexhtml,添加this is tomcat2 index

# cd /tomcat2/webapp

# madir test

# vi index.html

添加

this is tomcat2 index

esc :wq 保存推出。


同时为便于识别,修改监听8081端口的Tomcat的root目录下的index.jsp页面,将首页中的“
If you're seeing this, you've successfully installed Tomcat. Congratulations!”

改为“If you're seeing this, you've successfully installed Tomcat. Congratulations!Server port:8081!”。


4.修改Nginx的 nginx.conf配置文件

# vim /usr/local/nginx/conf/nginx.conf

[html]  view plain  copy
  1. http {  
  2.     include       mime.types;  
  3.     default_type  application/octet-stream;  
  4.   
  5.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  6.     #                  '$status $body_bytes_sent "$http_referer" '  
  7.     #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  8.   
  9.     #access_log  logs/access.log  main;  
  10.   
  11.     sendfile        on;  
  12.     #tcp_nopush     on;  
  13.   
  14.     #keepalive_timeout  0;  
  15.     keepalive_timeout  65;  
  16.   
  17.     #gzip  on;  
  18.   
  19.     <span style="color:#ff0000;">upstream mytomcat {  
  20.         server localhost:8080 weight=100;  
  21.         server localhost:8090 weight=110;  
  22.     }</span>  
[html]  view plain  copy
  1. server {  
  2.     listen       80;  
  3.     server_name  <span style="color:#ff0000;">mytomcat</span>;  
  4.   
  5.     #charset koi8-r;  
  6.   
  7.     #access_log  logs/host.access.log  main;  
  8.   
  9.     location / {  
  10.         root   html;  
  11.         index  index.html index.htm;  
  12.        <span style="color:#ff0000;"> proxy_pass http://mytomcat;</span>  
  13.     }  
  14.   
  15.     #error_page  404              /404.html;  
  16.   
  17.     # redirect server error pages to the static page /50x.html  
  18.     #  
  19.     error_page   500 502 503 504  /50x.html;  
 
保存后退出 

分别启动tomcat1与tomcat2

# /tomcat1/bin/startup.sh

# /tomcat2/bin/startup.sh

分别访问 http://localhost:8080/test1与http://localhost:8090/test2 验证是否正常

重启 nginx

# /usr/local/nginx/sbin/nginx -s reload

打开浏览器输入:http://localhost/test 多刷新几次,就会看到请求回来的内容有时不一样。说明,nginx 是随机访问的tomcat1或者tomcat2

配置文件里面的weight哪个数值越大,请求到的次数就会多。




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值