Nginx+Tomcat集羣配置

Nginx+Tomcat集羣配置

一臺虛擬機做爲Nginx服務nginx

兩太虛擬機配置Tomcat+jdk環境shell

Nginx測試

啓動:瀏覽器

cd usr/local/nginx/sbin
./nginx --->該啓動默認使用(/usr/local/nginx/conf/nginx.conf)的配置文件
	
#若是要指定本身的配置文件路徑
./nginx -c /x/xx/xxx/nginx.conf

檢測配置文件是否正確:/xx/sbin/nginx -ttomcat

瀏覽器測試:http://ip:80http://ip (nginx的默認端口80)服務器

關閉:./nginx -s stopsession

從新載入配置文件:/xx/sbin/nginx -s reload負載均衡

重啓Nginx:/xx/sbin/nginx -s reopen測試

查看線程 ps -ef | grep nginx (查詢主進程號) kill -9 主進程號 (強制關閉)url

配置集羣環境

修改配置文件spa

/usr/local/nginx/conf/nginx.conf

1)配置Tomcat集羣節點

在http節點內部,server節點外部添加如下內容

upstream tomcat-servers {
	server 192.168.1.103:8080;
	server 192.168.1.104:8080;
}

#反向代理
		在server節點內部:
        location / {
        	proxy_pass http://tomcat-servers;
        	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;
        	proxy_next_upstream http_502 http_504 error timeout invalid_header;
        }

啓動全部tomcat節點,啓動nginx,測試

負載均衡解決方案:

1)輪詢,該方式是默認的

upstream tomcat-servers {
			server 192.168.1.103:8080;
			server 192.168.1.104:8080;
		}

2)iphash,能夠有效的解決session問題

該方案將用戶IP地址與第一次訪問到達的真正服務器作了綁定,session不會丟失

upstream tomcat-servers {
	ip_hash;
	server 192.168.1.103:8080;
	server 192.168.1.104:8080;
}

3)weight:權重輪詢,如下方案訪問服務器時以1:2比例訪問

upstream tomcat-servers {
	server 192.168.1.103:8080 weight=1;
	server 192.168.1.104:8080 weight=2;
}
相關文章
相關標籤/搜索