linux環境下經過nginx實現tomcat集羣javascript
安裝nginx以前須要pcre依賴和jvm-remote補丁
1、準備以下軟件:
一、nginx-1.1.2.tar.gz,負載均衡/反向代理服務器,可經過http://nginx.org/en/download.html獲取。
二、pcre-8.10.tar.gz,正規表達式庫,可經過http://sourceforge.net/projects/pcre/獲取;
三、nginx-upstream-jvm-route-0.1.tar.gz,是一個 Nginx 的擴展模塊,用來實現基於 Cookie 的 Session Sticky 的功能,可經過http://code.google.com/p/nginx-upstream-jvm-route/downloads/list獲取;css
2、安裝和安裝
一、解壓各軟件
html
[root@localhost ~]# tar zxvf pcre-8.10.tar.gz [root@localhost ~]# tar zxvf nginx-upstream-jvm-route-0.1.tar.gz [root@localhost ~]# tar nginx-1.1.2.tar.gz
二、安裝
java
[root@localhost ~]# cd nginx-1.1.2 [root@localhost ~]# patch -p0 < ${nginx-upstream-jvm-route解壓目錄}/jvm_route.patch [root@localhost ~]# ./configure --prefix=/usr/local/nginx --with-pcre=${pcre解壓目錄} --with-http_stub_status_module --with-http_ssl_module --add-module=${nginx-upstream-jvm-route解壓目錄} [root@localhost ~]# make [root@localhost ~]# make install
3、修改配置
一、修改tomcat的server.xml,服務器的tomcat的配置文件中分別找到:
<Engine name="Catalina" defaultHost="localhost" >
分別修改成:
node
Tomcat01: <Engine name="Catalina" defaultHost="localhost" jvmRoute="a"> Tomcat02: <Engine name="Catalina" defaultHost="localhost" jvmRoute="b"> Tomcat03: <Engine name="Catalina" defaultHost="localhost" jvmRoute="c">
二、修改nginx的nginx.conf文件linux
#運行NGINX所使用的用戶和組 user root; #nginx進程數,建議按照cpu數目來指定,通常爲它的倍數,每一個進程消耗約10M內存 worker_processes 1; #日誌信息 error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; pid logs/nginx.pid; events { #使用epoll的I/O模型 use epoll; #該值受系統進程最大打開文件數限制,須要使用命令ulimit -n 查看當前設置 worker_connections 1024; } http { #這裏是您須要修改的地方,修改成您的服務器IP:端口號 srun_id爲您在tomcat中所配置的jvmRoute upstream backend{ server 192.168.12.128:18080 srun_id=a; server 192.168.12.128:28080 srun_id=b; server 192.168.12.128:38080 srun_id=c; jvm_route $cookie_JSESSIONID|sessionid reverse; } include mime.types; #設置默認類型是二進制流,若未設置時,好比未加載PHP時,是不予解析,用瀏覽器訪問則出現下載窗口 default_type application/octet-stream; charset UTF-8; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 20m; limit_rate 1024k; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; gzip on; #gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; #limit_zone crawler $binary_remote_addr 10m; server { listen 80; server_name 192.168.12.128; #這裏也是您所須要修改的地方,多域名用空格隔開 index index.html index.htm index.jsp; charset UTF-8; root /usr/local/tomcats/project/;# 這裏也是您所須要修改的地方,虛擬機指向的路徑(可能這裏有點問題),個人web應用系統放在project下面的 #access_log logs/host.access.log main; #這裏也是您所須要修改的地方,yourproject更換成您的項目路徑 location /yourproject/ { proxy_pass http://backend; proxy_redirect off; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; index index.html index.htm index.jsp; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 1h; } location /Nginxstatus{ stub_status on; access_log off; } log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for'; error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
三、檢查nginx的配置
nginx
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
4、啓動測試web
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf /usr/local/tomcats/tomcat-a/bin/startup.sh /usr/local/tomcats/tomcat-b/bin/startup.sh /usr/local/tomcats/tomcat-c/bin/startup.sh
中止服務
shell
/usr/local/tomcats/tomcat-a/bin/shutdown.sh /usr/local/tomcats/tomcat-b/bin/shutdown.sh /usr/local/tomcats/tomcat-c/bin/shutdown.sh pkill -9 nginx