
反向代理介绍
- 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。
- 反向代理的作用:
(1)保证内网的安全,可以使用反向代理提供WAF功能,阻止web攻击大型网站,通常将反向代理作为公网访问地址,Web服务器是内网。
(2)负载均衡,通过反向代理服务器来优化网站的负载。
反向代理Demo
系统:centos 7
此处使用tomcat作为被代理的服务器
- 准备工作
123
- centos 7安装jdk- 上传tomcat到服务器(解压出两份,注意修改解压出来的目录名称)- 安装nginx

修改tomcat配置,防止端口冲突(文件位置:
tomcat/conf/server.xml
),只需要修改其中一个tomcat配置即可修改ngxin配置(文件位置:
nginx/conf/nginx.conf
)
第一种配置方式
1234567891011121314151617181920212223242526272829303132333435
upstream tomcat-test1 { ## 设置被代理的ip server 192.168.200.132:8080; } server { listen 80; server_name www.tomcat1.com; #charset koi8-r; #access_log logs/host.access.log main; location / { ## 被代理路径 proxy_pass http://192.168.200.132:8080; index index.html index.htm; } }upstream tomcat-test2 { server 192.168.200.132:8081; } server { listen 80; server_name www.tomcat2.com; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://192.168.200.132:8081; index index.html index.htm; } }
第二种配置方式
1234567891011121314151617181920212223242526272829303132333435大专栏 配置Nginx反向代理an>
upstream tomcat-test1 { ## 设置被代理的ip server 192.168.200.132:8080; } server { listen 80; server_name www.tomcat1.com; #charset koi8-r; #access_log logs/host.access.log main; location / { ## 被代理路径 proxy_pass http://tomcat-test1; index index.html index.htm; } }upstream tomcat-test2 { server 192.168.200.132:8081; } server { listen 80; server_name www.tomcat2.com; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://tomcat-test2; index index.html index.htm; } }
运行tomcat,运行nginx
123456
tomcat-test1/bin/startup.shtomcat-test2/bin/startup.sh# nginxnginx/sbin/nginx
测试
配置均衡负载
- 配置
nginx.conf
1234567891011
## 轮询,每个节点一次upstream tomcat-test2 { server 192.168.200.132:8081; server 192.168.200.132:8082; } ## 设置权重 upstream tomcat-test2 { server 192.168.200.132:8081; server 192.168.200.132:8082 weight=2; //权重越大,分配到的请求越多 }
反向代理实例
配置
nginx.conf
文件123456789101112131415161718192021222324
# erp项目 upstream erp {server 192.168.183.130:8080; } server { listen 80; server_name 192.168.183.130:8080; ## 代理相关静态资源 location / { proxy_pass http://192.168.183.130:8080; proxy_read_timeout 600s; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host:$server_port; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
测试
使用域名能访问到是因为我配置了hosts IP映射,如果没有配置,请使用IP访问