使用Nginx+Tomcat7在RHEL6.3下實現負載均衡

jopen 11年前發布 | 22K 次閱讀 Nginx Web服務器

使用Nginx+Tomcat7在RHEL6.3下實現負載均衡


1、安裝Nginx服務器(使用root用戶操作)


1.1、獲取安裝Nginx需要的軟件源

軟件源可以在 http://nginx.org/en/download.html 頁面找到
如果你看不懂英文,就從以下地址下載安裝rpm包就可以搞定軟件源了:)
http://nginx.org/packages/rhel/6/noarch/RPMS/nginx-release-rhel-6-0.el6.ngx.noarch.rpm

1.2、安裝Nginx

yum install nginx


提示:
nginx-1.2.6-1.el6.ngx.x86_64      安裝成功

1.3、查詢Nginx軟件文件安裝路徑

rpm -ql nginx-1.2.6-1.el6.ngx.x86_64


查詢結果:
/etc/logrotate.d/nginx
/etc/nginx
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf
/etc/nginx/conf.d/example_ssl.conf
/etc/nginx/fastcgi_params
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/etc/nginx/win-utf
/etc/rc.d/init.d/nginx
/etc/sysconfig/nginx
/usr/sbin/nginx
/usr/share/nginx
/usr/share/nginx/html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
/var/cache/nginx
/var/log/nginx

1.4、查詢服務安裝狀態

chkconfig --list | grep nginx


顯示結果:
nginx          0:off1:off2:on3:on4:on5:on6:off

說明Nginx服務已經安裝成功,并且是開機啟動的:)

1.5、查詢新建用戶狀態

less /etc/passwd | grep nginx


顯示結果:
nginx:x:496:493:nginx user:/var/cache/nginx:/sbin/nologin

說明新建了一個nginx用戶組和一個nginx用戶,用于執行nginx服務

2、配置Nginx服務器做為負載平衡(使用root用戶操作)


2.1、修改配置文件 /etc/nginx/nginx.conf

user  nginx;
worker_processes  8;            # 工作的子進程數量,默認為1(通常設置為等于CPU數量或者2倍于CPU)
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

add by guorui

worker_rlimit_nofile 8192; # 在nginx級別上提高打開的文件句柄限制,必須大于 worker_connections 的值

events {

add by guorui

use epoll; # 使用網絡IO模型linux建議epoll,FreeBSD建議采用kqueue

worker_connections 2048; # 每個進程允許的最多連接數,必須小于 worker_rlimit_nofile 的值

                           # 服務器的最大訪問客戶數 max clients = worker_processes * worker_connections 

}

http { include /etc/nginx/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 /var/log/nginx/access.log main;

sendfile on;

tcp_nopush on;

keepalive_timeout 65;

gzip on;

add by guorui # 添加 upstream 模塊,參考PS1

upstream mtserver { # mtserver 是服務器集群名稱,后面會用到 server localhost:8081 fail_timeout=5; # 添加多個負載服務器的地址、端口及其他負載參數 server localhost:8080; }

include /etc/nginx/conf.d/*.conf; }</pre>2.2、修改配置文件 /etc/nginx/conf.d/default.conf 

location / {
   proxy_pass http://mtserver;# 添加這一行,引用上面定義的 服務器集群 mtserver
   #root   /usr/share/nginx/html;# 注釋掉
   #index  index.html index.htm;# 注釋掉
}

3、配置Tomcat7服務器


3.1、服務器A

修改 server.xml 文件,把以下內容

<Engine name="Catalina" defaultHost="localhost">

修改為

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat-a">

3.2、服務器B

修改 server.xml 文件,把以下內容

<Engine name="Catalina" defaultHost="localhost">

修改為

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat-b">

3.3、其他修改項(如果兩臺服務器在一臺機器上,需要做下面的修改,主要是為了避免端口沖突)

3.3.1、把以下內容

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


改為

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

3.3.2、把以下內容

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

改為

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

3.3.3、把以下內容

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

改為

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

4、手動操作Nginx服務器


啟動 nginx
關閉 nginx -s stop

================================完=================================

PS1:upstream 語法參考
語法: upstream name { ... }
默認值:
上下文: http
定義一組服務器。 這些服務器可以監聽不同的端口。 而且,監聽在TCP和UNIX域套接字的服務器可以混用。

例子:

upstream backend {
   server backend1.example.com weight=5;
   server 127.0.0.1:8080       max_fails=3 fail_timeout=30s;
   server unix:/tmp/backend3;
}
默認情況下,nginx按加權輪轉的方式將請求分發到各服務器。 在上面的例子中,每7個請求會通過以下方式分發: 

5個請求分到backend1.example.com, 一個請求分到第二個服務器,一個請求分到第三個服務器。 與服務器通信

的時候,如果出現錯誤,請求會被傳給下一個服務器,直到所有可用的服務器都被嘗試過。 如果所有服務器都返回

失敗,客戶端將會得到最后通信的那個服務器的(失敗)響應結果。

語法: server address [parameters];
默認值:
上下文: upstream
定義服務器的地址address和其他參數parameters。 地址可以是域名或者IP地址,端口是可選的,或者是指定“unix:”

前綴的UNIX域套接字的路徑。如果沒有指定端口,就使用80端口。 如果一個域名解析到多個IP,本質上是定義了多

個server。

你可以定義下面的參數:

weight=number
設定服務器的權重,默認是1。
max_fails=number
設定Nginx與服務器通信的嘗試失敗的次數。在fail_timeout參數定義的時間段內,如果失敗的次數達到此值,Nginx就

認為服務器不可用。在下一個fail_timeout時間段,服務器不會再被嘗試。 失敗的嘗試次數默認是1。設為0就會停止統

計嘗試次數,認為服務器是一直可用的。 你可以通過指令proxy_next_upstream、 fastcgi_next_upstream和 

memcached_next_upstream來配置什么是失敗的嘗試。 默認配置時,http_404狀態不被認為是失敗的嘗試。
fail_timeout=time
設定
統計失敗嘗試次數的時間段。在這段時間中,服務器失敗次數達到指定的嘗試次數,服務器就被認為不可用。
服務器被認為不可用的時間段。
默認情況下,該超時時間是10秒。
backup
標記為備用服務器。當主服務器不可用以后,請求會被傳給這些服務器。
down
標記服務器永久不可用,可以跟ip_hash指令一起使用。

 

PS2:nginx命令語法

Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
 -?,-h         : this help
 -v            : show version and exit
 -V            : show version and configure options then exit
 -t            : test configuration and exit
 -q            : suppress non-error messages during configuration testing
 -s signal     : send signal to a master process: stop, quit, reopen, reload
 -p prefix     : set prefix path (default: /etc/nginx//)
 -c filename   : set configuration file (default: /etc/nginx/nginx.conf)

 -g directives : set global directives out of configuration file

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!