歡迎您光臨本站 註冊首頁

nginx+tomcat負載均衡

←手機掃碼閱讀     火星人 @ 2014-03-04 , reply:0

nginx+tomcat負載均衡

[本文作者:擦肩而過         完成日期:2009.08.20
本文原址連接:http://bbs.yahunet.com/thread-10029-1-1.html       轉載請註明!]

前端一台nginx伺服器做調度. 後端兩台tomcat做WEB伺服器. 這裡動態頁與靜態頁都由tomcat來處理.

軟體:
引用:
nginx-0.7.28.tar.gz
pcre-7.8.tar.gz
apache-tomcat-6.0.20.tar.gz
jdk-6u14-linux-i586-rpm.bin
架構說明

   Nginx+Tomcat.jpg (55.9 KB)

2009-8-25 11:43
三台伺服器  一台調度也就是nginx伺服器,它還是NFS伺服器
我們把2個tomcat的webapps目錄掛載到NFS伺服器上,這樣數據程序是同步的了。

配置步驟:
一、web伺服器的配置
首先安裝兩台tomcat,兩台安裝都一樣
1.安裝JDK
引用:
# pwd
/usr/local
# chmod a+x jdk-6u14-linux-i586-rpm.bin
# ./jdk-6u14-linux-i586-rpm.bin
# ln -s /usr/java/jdk1.6.0_14 /usr/local/java
設置環境變數
# vi profile
末尾增加如下內容
引用:

JAVA_HOME=/usr/java/jdk1.6.0_14
CLASSPATH=/usr/java/jdk1.6.0_14/lib/dt.jar:/usr/java/jdk1.6.0_14/lib/tools.jar
PATH=/usr/java/jdk1.6.0_14/bin:$PATH
export PATH JAVA_HOME CLASSPATH
2.安裝tomcat
安裝Tomcat。
引用:
# cp apache-tomcat-6.0.20.tar.gz /usr/local/
# cd /usr/local/
# tar xzvf apache-tomcat-6.0.20.tar.gz
# ln -s apache-tomcat-6.0.20 tomcat
# vi /usr/local/tomcat/bin/catalina.sh
加入一行:
JAVA_HOME=/usr/java/jdk1.6.0_14
# /usr/local/tomcat/bin/startup.sh
啟動服務后訪問本地的8080埠可以看到對應apache tomcat頁面了
把Tomcat加到自啟動:
# vi /etc/rc.d/rc.local
在裡面加入如下代碼:
引用:
export JDK_HOME=/usr/java/jdk1.6.0_14
export JAVA_HOME=/usr/java/jdk1.6.0_14
/usr/local/tomcat/bin/startup.sh
至此tomcat已安裝成功了
優化: tomcat 比如防止內存溢出; TCP/IP  比如time_wait與closed_wait等等
--------------------------------------------------------------------------------------------------
二、 安裝 nginx、nfs 注意它們是一台伺服器上
1.安裝 nginx
在安裝之前首先要安裝pcre-7.9.tar.gz
引用:

# tar zxvf pcre-7.9.tar.gz
# cd pcre-7.9
# ./configure
# make && make install
安裝nginx
引用:

# tar zxvf nginx-0.7.61.tar.gz
# cd nginx-0.7.61
# ./configure --with-http_stub_status_module --prefix=/usr/local/nginx
# make && make install
修改nginx的配置文件
我這裡是把原先的重命名然後新建了一個nginx.conf
引用:

#vi nginx.conf
user nobody nobody;
worker_processes  8;
pid  /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
}
http{
include       mime.types;
default_type  application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
     
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;
#設定負載均衡列表  
upstream  backend
{  
server 192.168.100.89:8080;
server 192.168.100.90:8080;
}
#設定虛擬主機
server {
listen 80;
server_name  www.syitren.com;
#對 / 所有做負載均衡 (本機nginx採用完全轉發,所有請求都轉發到後端的tomcat集群)
location / {  
root /var/www ;
index index.jsp index.htm index.html;
proxy_redirect off;
#保留用戶真實信息
proxy_set_header Host $host;
proxy_set_header  X-Real-IP  $remote_addr;
proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass  http://backend;
}

#location /nginx {  
#access_log  on;
#auth_basic  "NginxStatus";
#auth_basic_user_file  /usr/local/nginx/htpasswd;
#}
log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log  /var/log/access.log  access;
}
}
檢查nginx的配置文件
引用:
# /usr/local/webserver/nginx/sbin/nginx -t
啟動nginx
引用:
# /usr/local/webserver/nginx/sbin/nginx
重啟nginx
引用:
# kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
補:
(1)查看負載均衡信息
引用:

location /nginx {  
stub_status on;
access_log  on;
auth_basic  "NginxStatus";
auth_basic_user_file  /usr/local/nginx/htpasswd;
}
其中/usr/local/nginx/htpasswd可以用apache自帶的功能生成。

最後在IE里訪問:
http://www.syitren.com/nginx,然後輸入用戶名密碼就進入了。
進入之後的說明
輸入地址 http://www.syitren.com/nginx/,輸入驗證帳號密碼,即可看到類似如下內容:
引用:
Active connections: 328
server accepts handled requests
9309 8982  28890
Reading: 1 Writing: 3 Waiting: 324
第一行表示目前活躍的連接數
第三行的第三個數字錶示Nginx運行到當前時間接受到的總請求數,如果快達到了上限,就需要加大上限值了。
第四行是Nginx的隊列狀態
(2)負載均衡方法參考《nginx的upstream目前支持的幾種分配方式》一文
http://bbs.yahunet.com/thread-10028-1-1.html
引用:
upstream  backend
{  
server 192.168.100.89:8080;
server 192.168.100.90:8080;
}
--------------------------------------------------------------------------------------------------

三、 配置NFS 了
需要安裝rpm包portmap、nfs
引用:

# vi /etc/export
/var/www  192.168.100.89(rw,sync,no_root_squash),192.168.100.90(rw,sync,no_root_squash)
# service portmap restart
# service nfs start
# exportfs -rv
重新輸出共享目錄
引用:
# showmoun -e
查看本機共享的目錄

然後tomcat的兩台伺服器掛載
引用:
# mount 192.168.100.88:/var/www  /usr/local/tomcat/weapps
然後我們要在兩台tomcat的配置文件中即server.xml中做虛擬主機要與nginx.conf里的一致才OK
引用:

# vi server.xml
<Host name="www.syitren.com" debug="0" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false"  xmlNamespaceAware="false">
  <Context path="" docBase="/usr/local/tomcat/webapps/" debug="0" reloadable="true" crossContext="true"/>  
  <Logger className="org.apache.catalina.logger.FileLogger" directory="logs" prefix="www.syitren.com_log." suffix=".txt"
timestamp="true"/>
</Host>
--------------------------------------------------------------------------------------------------
四、測試
session.rar (290 Bytes)  session.rar (290 Bytes)
下載次數: 0

2009-8-25 11:43

寫了一個測試session的頁,上傳到虛擬主機對應的目錄。

(1)訪問一次刷新一次再刷新發現時間每次都不一樣,就是來回出現兩台tomcat的系統時間,說明成功了。
(2)然後把其中的一台tomcat停掉,這時我們再刷新其中有一個就沒有響應了,但過了一分鐘左右就是一台機器提供服務了,說明,nginx可以自動把down的伺服器去除,從而使客戶端透明。
(3)然後再把停掉的tomcat伺服器開啟,過一段時間后,nginx伺服器又把它加入調度行列。這都是自動的。

[火星人 ] nginx+tomcat負載均衡已經有593次圍觀

http://coctec.com/docs/service/show-post-21046.html