正向代理纸颜,反向代理
配置Nginx
配置文件目錄:/usr/local/nginx-1.6.1/conf/nginx.conf
cd /usr/local/nginx-1.6.1/conf
vi nginx.conf
修改后的配置文件如下:
在#gzip on;后面加入upstream
gzip on;
upstream tomcat {
ip_hash;
server 192.168.1.249:8080 weight=1;
server 127.0.0.1:8082 weight=1;
server 192.168.1.248:8081 weight=1;
server 192.168.1.248:8083 weight=1;
}
server {
listen 8088;
server_name localhost;
location / {
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;
root /usr/share/nginx/html;
index index.html index.htm ;
proxy_pass http://tomcat;
client_max_body_size 5m; #表示最大上傳5M焦履,需要多大就設(shè)置多大
proxy_connect_timeout 10;
proxy_read_timeout 10;
proxy_send_timeout 10;
}
}
此處nginx配置的listen監(jiān)聽端口是8088榕堰,那么等配置好后訪問nginx就是192.168.1.249:8088,這樣就會(huì)顯示nginx歡迎頁
在與tomcat集群配好后在訪問192.168.1.249:8088,會(huì)顯示tomcat的首頁,
要是測(cè)試集群是否正常,在每個(gè)tomcat的webapps目錄下建個(gè)test目錄黄痪,將測(cè)試用的index.jsp放在此目錄下即可,此時(shí)訪問http://192.168.1.249:8088/test/
就會(huì)顯示index.jsp內(nèi)容盔然。以上說的192.167.1.249是我nginx安裝目錄所在桅打,自己測(cè)試時(shí)要根據(jù)自己實(shí)際的nginx所在服務(wù)器的ip來訪問是嗜。
效果如下圖:
訪問測(cè)試用tomcat的webapps目錄下的test/index.jsp的頁面
upstream tomcat{ # 負(fù)載均衡站點(diǎn)的名稱為tomcat,可以自己取
ip_hash; # 可選挺尾,根據(jù)來源IP方式選擇web服務(wù)器鹅搪,省略的話按默認(rèn)的輪循方式選擇web服務(wù)器
server 127.0.0.1:8080 weight=1; # web服務(wù)器的IP地址及tomcat發(fā)布端口, #weigth參數(shù)表示權(quán)值潦嘶,權(quán)值越高被分配到的幾率越大
server 127.0.0.1:8082 weight=1;
server 192.168.1.248:8081 weight=1;
server 192.168.1.248:8083 weight=1;
}
說明:紅色字體的tomcat這個(gè)名稱隨便命名涩嚣,但在location中proxy_pass 崇众,http://后面的名稱要一致
weigth參數(shù)表示權(quán)值掂僵,權(quán)值越高被分配到的幾率越大
location 中加入有下劃線的代碼
location / {
root /usr/share/nginx/html;
index index.html index.htm;
proxy_pass http://tomcat; # 負(fù)載均衡指向的發(fā)布服務(wù)tomcat
proxy_redirect default;
proxy_connect_timeout 10; #跟代理服務(wù)器連接的超時(shí)時(shí)間,必須留意這個(gè)time out時(shí)間不能超過75秒顷歌,當(dāng)一臺(tái)服務(wù)器當(dāng)?shù)魰r(shí)锰蓬,過10秒轉(zhuǎn)發(fā)到另外一臺(tái)服務(wù)器。
}
proxy_pass http://tomcat; #是必須要加的
proxy_connect_timeout 10; #這個(gè)參數(shù)是連接的超時(shí)時(shí)間眯漩。 我設(shè)置成10芹扭,表示是10秒后超時(shí)會(huì)連接到另外一臺(tái)服務(wù)器
tomcat209
<%@ page import="com.neuedu.bean.Person" %><%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2018/7/17
Time:
13:57
To change this template use File | Settings | File Templates.
--%>
<%@ page
contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<%=session.getId()%>
tomcat209
</body>
</html>
tomcat141
<%@ page import="com.neuedu.bean.Person" %><%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2018/7/17
Time:
13:57
To change this template use File | Settings | File Templates.
--%>
<%@ page
contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<%=session.getId()%>
tomcat141
</body>
</html>