nginx+tomcat實現主備切換
一、準備工作
1、nginx安裝
1.1、準備工作
選首先安裝這幾個軟體:GCC,PCRE(Perl Compatible Regular Expression),zlib,OpenSSL。
Nginx是C寫的,需要用GCC編譯;Nginx的Rewrite和HTTP模組會用到PCRE;Nginx中的Gzip用到zlib;
用命令“# gcc”,檢視gcc是否安裝;如果出現“gcc: no input files”資訊,說明已經安裝好了。
否則,就需要用命令“# yum install gcc”,進行安裝了!一路可能需要多次輸入y,進行確認。
安裝好後,可以再用命令“#gcc”測試,或者用命令“# gcc -v”檢視其版本號。
同樣方法,用如下命令安裝PCRE,zlib,OpenSSL(其中devel,是develop開發包的意思):
- # yum install -y pcre pcre-devel
- # yum install -y zlib zlib-devel
- # yum install -y openssl openssl-devel
1.2、下載並安裝
建立目錄(nginx-src)並進去;然後,從官方地址(http://nginx.org/)下載,解壓,配置,編譯,安裝:
- # mkdir nginx-src && cd nginx-src
- # wget http://nginx.org/download/nginx-1.7.3.tar.gz
- # tar xzf nginx-1.7.3.tar.gz
- # cd nginx-1.7.3
- # ./configure
- # make
- # make install
- # whereis nginx
- nginx: /usr/local/nginx
預設的安裝路徑為:/usr/local/nginx;跳轉到其目錄下sbin路徑下,便可以啟動或停止它了。
- # ./nginx -h
- nginx version: nginx/1.7.3
- 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: /usr/local/nginx/)
- -c filename : set configuration file (default: conf/nginx.conf)
- -g directives : set global directives out of configuration file
啟動:nginx
停止:nginx -s stop
1.3、新增到系統服務
使用命令“# vi /etc/init.d/nginx”,開啟編輯器,輸入如下內容:
- #!/bin/sh
- # chkconfig: 2345 85 15
- # Startup script for the nginx Web Server
- # description: nginx is a World Wide Web server.
- # It is used to serve HTML files and CGI.
- # processname: nginx
- # pidfile: /usr/local/nginx/logs/nginx.pid
- # config: /usr/local/nginx/conf/nginx.conf
- PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
- DESC="nginx deamon"
- NAME=nginx
- DAEMON=/usr/local/nginx/sbin/$NAME
- SCRIPTNAME=/etc/init.d/$NAME
- test -x $DAEMON || exit 0
- d_start(){
- $DAEMON || echo -n "already running"
- }
- d_stop(){
- $DAEMON -s quit || echo -n "not running"
- }
- d_reload(){
- $DAEMON -s reload || echo -n "can not reload"
- }
- case "$1" in
- start)
- echo -n "Starting DESC:NAME"
- d_start
- echo "."
- ;;
- stop)
- echo -n "Stopping DESC:NAME"
- d_stop
- echo "."
- ;;
- reload)
- echo -n "Reloading $DESC conf..."
- d_reload
- echo "reload ."
- ;;
- restart)
- echo -n "Restarting DESC:NAME"
- d_stop
- sleep 2
- d_start
- echo "."
- ;;
- *)
- echo "Usage: $ScRIPTNAME {start|stop|reload|restart}" >&2
- exit 3
- ;;
- esac
- exit 0
儲存退出後,再使用下面的命令,使其可執行;然後,新增配置並檢視。
可用chkconfig修改其值,也可用ntsysv工具改變是否自啟動。
- # chmod +x /etc/init.d/nginx
- # chkconfig --add nginx
- # chkconfig nginx on/off
- # chkconfig --list nginx
- nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
2.1下載tomcat安裝包,解壓,可以修改webapps/ROOT之下的index.jsp檔案,來標記到底是那個tomcat。
2.2修改tomcat的監聽埠:此處為:8080,8180
2.3啟動tomcat
二、配置nginx,實現主備切換:
配置檔案如下:
upstream 192.168.0.102 {
server 192.168.0.102:8080 max_fails=1;#max_fails 表示健康檢查失敗的次數,這裡表示次數為一次,即標記該伺服器down了
server 192.168.0.102:8180 max_fails=1;
}
server
{
listen 80;
server_name 192.168.0.102;
location / {
proxy_next_upstream error timeout http_500 http_502 http_504; #這裡表示健康檢查涉及到的情形,有這些情形的,都切換到另外的web伺服器訪問
proxy_read_timeout 10s; #這裡表示程式返回的時間,請參考php.ini的max_exe_time來設定。
proxy_pass http://192.168.0.102;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
注意:將這段配置放在http{}中。
三、測試
1.瀏覽器輸入:http://192.168.0.102,觀察訪問的是哪一個tomcat
2.停掉正在訪問的這個tomcat服務,然後重新訪問,觀察訪問的是哪一個tomcat。