公司要上线一个新应用,需要部署在两台服务器上(都是物理机),并且需要使用nginx做负载均衡,分担压力,因为访问量可能会很多。可惜只有两台服务器,要是有三台的话,那我就其中2台装tomcat,用来部署应用,剩下一台装nginx;不过两台也行,把nginx装在其中一台tomcat服务器上就行了,完全没影响。我用的是最新的稳定版nginx1.10.2.tar.gz,tomcat为6.0.44.接下来,就开始记录在两台服务器上搭建tomcat+nginx负载均衡所遇到的问题:
1.安装方式有两种,一种是在linux终端在线安装(通过yum 源);另一种是在nginx官网下载好tar包,解压后编译,编译之前确保安装了openssl和pcre
我采用的是第二种方式,具体步骤参考这里http://jingyan.baidu.com/article/1974b2898f5eadf4b1f774de.html
有个地方需要注意:编译时可能会报错“./configure error:invalid option ”--with-http_spdy_module",这是因为从nginx 1.9.5开始
已经没有了 --with-http_spdy_module ,取代的是 --with-http_v2_module
2.编译好后,修改conf文件夹下的nginx.conf文件,特别要注意画横线部分就是我要负载均衡的两台服务器的ip地址,你改成你自己的,
修改完后 ,重新加载命令为 在nginx的安装目录下的sbin文件夹下执行 ./nginx -s reload
#user nobody;
worker_processes 4;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 8096;
multi_accept on;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 120;
gzip on;
upstream orderapp{
server 10.140.33.80:8080 weight=1;
server 10.140.33.129:8080 weight=1;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://orderapp;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
root html;
index index.html index.htm;
}
#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 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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
3.分别启动两台服务器上的tomcat,上述配置文件中有一行可以修改访问域名(即域名要与ip绑定,配置hosts文件),我用的是默认的(即localhost),比如我nginx放在10.140.33.80上,所以我在任意一台电脑上浏览器中输入这个ip地址,就可以随机访问其中一台tomcat下的应用,从而实现负载均衡
4.以上只能看到不同的tomcat页面,那么如何访问tomcat下的web应用呢
(1)将war包或者项目源码放到两个tomcat的webapps文件夹下,如果你更改了tomcat的部署目录(即你在tomcat的配置文件server.xml中的<Host/>标签中加了<Context path="" docBase=""/>这个标签的话,证明你自定义了部署目录,那么你就还要修改nginx.conf里面的内容,要不然访问不到,如果你没更改部署目录,那nginx.conf就不用动了)
(2)假如我部署的项目名为spring,里面有个controller,内容如下:
@Controller
@RequestMapping("/easy")
public class HelloController2 {
@RequestMapping("say/{tid}/{number}")
@ResponseBody
public Student excute(@PathVariable String tid,@PathVariable Integer number) throws IOException{
System.out.println("执行HelloController2");
ModelAndView a=new ModelAndView();
a.addObject("yyy", 4577);
Student student=new Student();
student.setAge(23);
student.setId(2);
student.setName("steven");
List<String> list=new ArrayList<String>();
String ids[]=tid.split("-");
for(String id:ids){
list.add(id);
}
student.setList(list);
student.setNumber(number);
return student;
}
}
(3)那么现在我要让别人访问我controller里面的execute()方法,那他就只需要在浏览器地址栏里输入
http://10.140.33.80/spring/easy/say/1010234/20 ,回车成功后,页面会显示返回的信息,你让两台tomcat都部署这哥spring应用,然后,为了区分,你让每个spring项目里面的这个execute方法返回不同的信息,结果证明负载均衡,因为两台机器的weight=1
贴上其他相关参考地址
http://blog.csdn.net/z915412321/article/details/52261306
http://www.cnblogs.com/huangye-dream/p/3777004.html
http://liuhonghe.me/nginx-setmodule-spdy.html
https://liuzhichao.com/p/1774.html(想了解什么是spdy可以看这里)
http://www.tuicool.com/articles/I7ryYf
http://www.linuxidc.com/Linux/2016-01/127255.htm
http://blog.csdn.net/wang379275614/article/details/47778201(这篇文章有nginx.conf配置文件各个参数的解释)
http://aijuans.iteye.com/blog/2154373