一、反向代理
1.什么是反向代理
通常的代理服務器,只用於代理內部網絡對Internet的連接請求,客戶機必須指定代理服務器,並將本來要直接發送到Web服務器上的http請求發送到代理服務器中由代理服務器向Internet上的web服務器發起請求,最終達到客戶機上網的目的(也就是正向代理)。
而反向代理(Reverse Proxy)方式是指以代理服務器來接受internet上的連接請求,然后將請求轉發給內部網絡上的服務器,並將從服務器上得到的結果返回給internet上請求連接的客戶端,此時代理服務器對外就表現為一個反向代理服務器。
如下圖:
Nginx只做請求的轉發,后台有多個http服務器提供服務,nginx的功能就是把請求轉發給后面的服務器,決定把請求轉發給誰。
2、安裝tomcat2個,現在我們模擬的話服務器就采用tomcat來模擬。
安裝tomcat的過程就不介紹了,在 http://blog.csdn.net/u013144287/article/details/78499485過程中有介紹,
<Server port="8006" shutdown="SHUTDOWN">
-
<Connector port= "8081" protocol="HTTP/1.1"
-
connectionTimeout= "20000"
-
redirectPort= "8443" />
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
修改此3處端口號,分別在原來基礎上加1,然后wq保存,啟動兩台tomcat


8080.zcinfo.com 訪問運行8080端口的tomcat
8082.zcinfo.com 訪問運行8081端口的tomcat
如圖所示:hosts目錄是:C:\Windows\System32\drivers\etc
-
upstream tomcatserver1 {
-
server 192.168.3.43:8080;
-
}
-
upstream tomcatserver2 {
-
server 192.168.3.43:8082;
-
}
-
server {
-
listen 80;
-
server_name 8080.zcinfo.com;
-
-
#charset koi8-r;
-
-
#access_log logs/host.access.log main;
-
-
location / {
-
proxy_pass http://tomcatserver1;
-
index index.html index.htm;
-
}
-
}
-
server {
-
listen 80;
-
server_name 8082.zcinfo.com;
-
-
#charset koi8-r;
-
-
#access_log logs/host.access.log main;
-
-
location / {
-
proxy_pass http://tomcatserver2;
-
index index.html index.htm;
-
}
-
}
5、測試


負載均衡建立在現有網絡結構之上,它提供了一種廉價有效透明的方法擴展網絡設備和服務器的帶寬、增加吞吐量、加強網絡數據處理能力、提高網絡的靈活性和可用性。
負載均衡,英文名稱為Load Balance,其意思就是分攤到多個操作單元上進行執行,例如Web服務器、FTP服務器、企業關鍵應用服務器和其它關鍵任務服務器等,從而共同完成工作任務。
2、需求
nginx作為負載均衡服務器,用戶請求先到達nginx,再由nginx根據負載配置將請求轉發至tomcat服務器。
nginx負載均衡服務器:192.168.3.43
tomcat1服務器:192.168.3.43:8080
tomcat2服務器:192.168.3.43:8081
3、nginx的配置
-
upstream tomcatserver1 {
-
server 192.168.3.43:8080;
-
server 192.168.3.43:8082; #多加了此台服務器
-
}
-
upstream tomcatserver2 {
-
server 192.168.3.43:8082;
-
}
-
server {
-
listen 80;
-
server_name 8080.zcinfo.com;
-
-
#charset koi8-r;
-
-
#access_log logs/host.access.log main;
-
-
location / {
-
proxy_pass http://tomcatserver1;
-
index index.html index.htm;
-
}
-
}
-
server {
-
listen 80;
-
server_name 8082.zcinfo.com;
-
-
#charset koi8-r;
-
-
#access_log logs/host.access.log main;
-
-
location / {
-
proxy_pass http://tomcatserver2;
-
index index.html index.htm;
-
}
-
}
-
upstream tomcatserver1 {
-
server 192.168.3.43:8080 weight=2;
-
server 192.168.3.43:8082 weight=1;
-
}
-
upstream tomcatserver2 {
-
server 192.168.3.43:8082;
-
}
-
server {
-
listen 80;
-
server_name 8080.zcinfo.com;
-
-
#charset koi8-r;
-
-
#access_log logs/host.access.log main;
-
-
location / {
-
proxy_pass http://tomcatserver1;
-
index index.html index.htm;
-
}
-
}
-
server {
-
listen 80;
-
server_name 8082.zcinfo.com;
-
-
#charset koi8-r;
-
-
#access_log logs/host.access.log main;
-
-
location / {
-
proxy_pass http://tomcatserver2;
-
index index.html index.htm;
-
}
-
}
ps:關於nginx負載均衡的一些參數介紹例子
-
節點說明:
-
在http節點里添加:
-
-
#定義負載均衡設備的 Ip及設備狀態
-
upstream myServer {
-
-
server 127.0.0.1:9090 down;
-
server 127.0.0.1:8080 weight=2;
-
server 127.0.0.1:6060;
-
server 127.0.0.1:7070 backup;
-
}
-
-
在需要使用負載的Server節點下添加
-
-
proxy_pass http://myServer;
-
-
upstream 每個設備的狀態:
-
-
down 表示單前的server暫時不參與負載
-
weight 默認為1.weight越大,負載的權重就越大。
-
max_fails :允許請求失敗的次數默認為1.當超過最大次數時,返回proxy_next_upstream 模塊定義的錯誤
-
fail_timeout:max_fails 次失敗后,暫停的時間。
-
backup: 其它所有的非backup機器down或者忙的時候,請求backup機器。所以這台機器壓力會最輕。
4、效果