Nginx作為反向代理服務器,實現負載均衡。首先瀏覽器發起請求,到達Nginx,由Nginx將請求地址轉發給相應的tomcat服務器,再由tomcat服務器將結果返回給Nginx,Nginx將結果再轉發給瀏覽器。
在這過程中,對於瀏覽器來說,並不知道后端的存在, 相對於Tomact來說,當前的客戶端是Nginx服務器。這就完成了一個代理的過程。
首先准備三台Linux服務器;IP地址分別為 192.168.1.61 192.168.1.62 192.168.1.63
其中61安裝nginx服務器,將發給61的請求全部轉發給62安裝了tomcat的服務器
配置nginx-balance.conf文件
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# root html;
index index.html index.htm;
proxy_pass http://192.168.1.62:8080;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
保存退出,我們的反向代理就配置好了。
我們在准備多個tomcat服務器,IP為192.168.1.63 192.168.1.64 192.168.1.65
如果我們有多個服務器,並有nginx根據一定的策略將用戶的請求分別讓多個服務器,處理,這樣我們就實現了負載均衡。
配置負載均衡,修改配置文件為
worker_processes 2;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
# upstream 配置一組后端服務器,
# 請求轉發到upstream后,nginx按策略將請求指派出某一服務器
# 即配置用於負載均衡的服務器群信息
upstream backends {
#均衡策略
#none 輪詢(權重由weight決定)
#ip_hash
#fair
#url_hash
server 192.168.1.62:8080;
server 192.168.1.63;
# weight:權重,值越高負載越大;
# server 192.168.1.64 weight=5;
# backup:備份機,只有非備份機都掛掉了才啟用;
server 192.168.1.64 backup;
# down: 停機標志,不會被訪問
server 192.168.1.65 down;
# max_fails:達到指定次數認為服務器掛掉;
# fail_timeout:掛掉之后過多久再去測試是否已恢復
server 192.168.1.66 max_fails=2 fail_timeout=60s;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
# 反向代理設置,將所有/proxy_test/路徑下請求發給本機上的tomcat
location /proxy_test/ {
proxy_pass http://localhost:8080;
}
# 負載均衡設置,將所有jsp請求發送到upstream backends指定的服務器群上
location ~ \.jsp$ {
proxy_pass http://backends;
# 真實的客戶端IP
proxy_set_header X-Real-IP $remote_addr;
# 請求頭中Host信息
proxy_set_header Host $host;
# 代理路由信息,此處取IP有安全隱患
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 真實的用戶訪問協議
proxy_set_header X-Forwarded-Proto $scheme;
# 默認值default,
# 后端response 302時 tomcat header中location的host是http://192.168.1.62:8080
# 因為tomcat收到的請求是nginx發過去的, nginx發起的請求url host是http://192.168.1.62:8080
# 設置為default后,nginx自動把響應頭中location host部分替換成當前用戶請求的host部分
# 網上很多教程將此值設置成 off,禁用了替換,
# 這樣用戶瀏覽器收到302后跳到http://192.168.1.62:8080,直接將后端服務器暴露給瀏覽器
# 所以除非特殊需要,不要設置這種畫蛇添足的配置
proxy_redirect default;
}
# 一個url重寫的例子,瀏覽器請求 /page.go時,url被重寫成/test/page.jsp
location ~ \.go$ {
rewrite ^(.*)\.go$ /test/$1\.jsp last;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
配置均衡策略,Nginx會根據配置的策略將不同的請求轉發給服務器組的成員。
upstream backends {
#均衡策略
#none 輪詢(權重由weight決定)
#ip_hash
#fair
#url_hash
server 192.168.1.62:8080;
server 192.168.1.63;
# weight:權重,值越高負載越大;
# server 192.168.1.64 weight=5;
# backup:備份機,只有非備份機都掛掉了才啟用;
server 192.168.1.64 backup;
# down: 停機標志,不會被訪問
server 192.168.1.65 down;
# max_fails:達到指定次數認為服務器掛掉;
# fail_timeout:掛掉之后過多久再去測試是否已恢復
server 192.168.1.66 max_fails=2 fail_timeout=60s;
}