linux环境下通过nginx实现tomcat集群
安装nginx之前需要pcre依赖和jvm-remote补丁
一、准备如下软件:
1、nginx-1.1.2.tar.gz,负载均衡/反向代理服务器,可通过http://nginx.org/en/download.html获取。
2、pcre-8.10.tar.gz,正规表达式库,可通过http://sourceforge.net/projects/pcre/获取;
3、nginx-upstream-jvm-route-0.1.tar.gz,是一个 Nginx 的扩展模块,用来实现基于 Cookie 的 Session Sticky 的功能,可通过http://code.google.com/p/nginx-upstream-jvm-route/downloads/list获取;
二、安装和安装
1、解压各软件
2、安装
三、修改配置
1、修改tomcat的server.xml,服务器的tomcat的配置文件中分别找到:
<Engine name="Catalina" defaultHost="localhost" >
分别修改为:
2 | < Engine name = "Catalina" defaultHost = "localhost" jvmRoute = "a" > |
4 | < Engine name = "Catalina" defaultHost = "localhost" jvmRoute = "b" > |
6 | < Engine name = "Catalina" defaultHost = "localhost" jvmRoute = "c" > |
2、修改nginx的nginx.conf文件
03 | #nginx进程数,建议按照cpu数目来指定,一般为它的倍数,每个进程消耗约10M内存 |
07 | error_log logs/error.log; |
08 | #error_log logs/error.log notice; |
09 | #error_log logs/error.log info; |
16 | #该值受系统进程最大打开文件数限制,需要使用命令ulimit -n 查看当前设置 |
17 | worker_connections 1024; |
22 | #这里是您需要修改的地方,修改为您的服务器IP:端口号 srun_id为您在tomcat中所配置的jvmRoute |
24 | server 192.168.12.128:18080 srun_id=a; |
25 | server 192.168.12.128:28080 srun_id=b; |
26 | server 192.168.12.128:38080 srun_id=c; |
27 | jvm_route $cookie_JSESSIONID|sessionid reverse; |
30 | #设置默认类型是二进制流,若未设置时,比如未加载PHP时,是不予解析,用浏览器访问则出现下载窗口 |
31 | default_type application/octet-stream; |
33 | server_names_hash_bucket_size 128; |
34 | client_header_buffer_size 32k; |
35 | large_client_header_buffers 4 32k; |
36 | client_max_body_size 20m; |
42 | fastcgi_connect_timeout 300; |
43 | fastcgi_send_timeout 300; |
44 | fastcgi_read_timeout 300; |
45 | fastcgi_buffer_size 64k; |
46 | fastcgi_buffers 4 64k; |
47 | fastcgi_busy_buffers_size 128k; |
48 | fastcgi_temp_file_write_size 128k; |
52 | gzip_http_version 1.0; |
54 | gzip_types text/plain application/x-javascript text/css application/xml; |
56 | #limit_zone crawler $binary_remote_addr 10m; |
59 | server_name 192.168.12.128; #这里也是您所需要修改的地方,多域名用空格隔开 |
60 | index index.html index.htm index.jsp; |
62 | root /usr/local/tomcats/project/;# 这里也是您所需要修改的地方,虚拟机指向的路径(可能这里有点问题),我的web应用系统放在project下面的 |
63 | #access_log logs/host.access.log main; |
65 | #这里也是您所需要修改的地方,yourproject更换成您的项目路径 |
66 | location /yourproject/ { |
67 | proxy_pass http://backend; |
69 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
70 | proxy_set_header X-Real-IP $remote_addr; |
71 | proxy_set_header Host $http_host; |
72 | index index.html index.htm index.jsp; |
74 | location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { |
77 | location ~ .*\.(js|css)?$ { |
80 | location /Nginxstatus{ |
84 | log_format access '$remote_addr - $remote_user [$time_local] "$request" ' |
85 | '$status $body_bytes_sent "$http_referer" ' |
86 | '"$http_user_agent" $http_x_forwarded_for'; |
88 | error_page 404 /404.html; |
90 | error_page 500 502 503 504 /50x.html; |
91 | location = /50x.html { |
3、检查nginx的配置
四、启动测试
1 | /usr/ local /nginx/sbin/nginx -c /usr/ local /nginx/conf/nginx.conf |
2 | /usr/ local /tomcats/tomcat-a/bin/startup.sh |
3 | /usr/ local /tomcats/tomcat-b/bin/startup.sh |
4 | /usr/ local /tomcats/tomcat-c/bin/startup.sh |
停止服务
1 | /usr/ local /tomcats/tomcat-a/bin/ shutdown .sh |
2 | /usr/ local /tomcats/tomcat-b/bin/ shutdown .sh |
3 | /usr/ local /tomcats/tomcat-c/bin/ shutdown .sh |