一 概述
反向代理(Reverse Proxy)方式是指以代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络上的服务器;并将从服务器上得到的结果返回给Internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。
通常的代理服务器,只用于代理内部网络对Internet的连接请求,客户机必须指定代理服务器,并将本来要直接发送到Web服务器上的http请求发送到代理服务器中。当一个代理服务器能够代理外部网络上的主机,访问内部网络时,这种代理服务的方式称为反向代理服务。
反向代理配置:nginx.conf
upstream tomcat1 {
server 192.168.42.135:8081;
}
server {
listen 80;
server_name www.tomcat1.com; //可在本地主机host文件配置域名(switchHost)
location / {
proxy_pass http://tomcat1;
index index.html index.htm;
}
}
upstream tomcat2 {
server 192.168.42.135:8082;
}
server {
listen 80;
server_name www.tomcat2.com; //可在本地主机host文件配置域名(switchHost)
location / {
proxy_pass http://tomcat2;
index index.html index.htm;
}
}
nginx负载均衡配置:
upstream tomcat1 {
server 192.168.42.135:8081;
server 192.168.42.125:8083 weight=2; # 负载均衡服务 :weight表权重,权重默认为1,不配权重默认平均轮循(一人一次)
}
server {
listen 80;
server_name www.tomcat1.com; //可在本地主机host文件配置域名(switchHost)
location / {
proxy_pass http://tomcat1;
index index.html index.htm;
}
}