div.example { background-color: rgba(229, 236, 243, 1); color: rgba(0, 0, 0, 1); padding: 0.5em; margin: 1em 2em 1em 1em }
div.warning { border: 1px solid rgba(255, 0, 0, 1) }

在httpd反向代理实践(一)中,仅仅是使用了httpd来访问静态的资源文件,现在我们搭建真正的动态资源(基于servlet),然后看看反向代理中涉及到的 Content-Location和Location首部,以及cookie的domain和path时的情况。

首先是被代理端配置:

basePath : http://www.example.com:8080/hello

1. 重定向(Location首部)

@WebServlet("/dog")
public class Dog extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String path = request.getContextPath();
String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/"; //如果在重定向中不使用完全路径,那么重定向的Location内容就是/hello/cat,此时反向代理就无法正确的替换了。
//response.sendRedirect(path + "/cat"); //使用完全路径:http://www.example.com:8080/hello/cat,在反向代理中将被替换为 http://www.example1.com/tomcat/cat
response.sendRedirect(basePath + "cat");
}
}

2.设置cookie

@WebServlet("/SetCookie")
public class SetCookieServlet extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie nameCookie = new Cookie("name", "zhangsan");
//如果我们没有主动设置domain和path的话,那么即便没有ProxyPassReverseCookieDomain和ProxyPassReverseCookiePath指令也不会有问题。
nameCookie.setDomain(request.getServerName());
nameCookie.setPath(request.getContextPath()); response.addCookie(nameCookie); try(PrintWriter out = response.getWriter();){
out.print("set name cookie");
}
}
}

3. 获取cookie

@WebServlet("/GetCookie")
public class GetCookieServlet extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try(PrintWriter out = response.getWriter();){
Cookie[] cs = request.getCookies();
if(cs != null){
for(Cookie c : cs){
out.println(c.getName() + " --> " + c.getValue());
}
}
}
}
}

代理服务器端的配置:

ProxyPass "/tomcat" "http://www.example.com:8080/hello"
ProxyPassReverse "/tomcat" "http://www.example.com:8080/hello"
ProxyPassReverseCookieDomain "www.example.com" "www.example1.com"
ProxyPassReverseCookiePath "/hello" "/tomcat"
 Note: ProxyPassReverseCookieDomain 和 ProxyPassReverseCookiePath是被代理资源在前面,代理资源在后面。

按照上面配置搭建被代理环境,如果我们没有设置ProxyPassReverse 、ProxyPassReverseCookieDomain和ProxyPassReverseCookiePath的话,那么将无法正常重定向和存取cookie。

httpd反向代理实践(二)的更多相关文章

  1. httpd反向代理实践(一)

    div.example { background-color: rgba(229, 236, 243, 1); color: rgba(0, 0, 0, 1); padding: 0.5em; mar ...

  2. Nginx 负载均衡和反向代理实践

    nginx 以哪个配置文件启动 Nginx 负载均衡和反向代理实践 环境介绍 192.168.1.50    在这台主机上配置Nginx 的反向代理,负载均衡,和web1,web1使用的81号端口 1 ...

  3. 详细分析apache httpd反向代理的用法

    html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...

  4. Apache Httpd 反向代理配置 (笔记)

    Apache Httpd 配置Http反向代理 打开配置文件 httpd.conf 先启动相关模块(去掉前面的注释#)LoadModule proxy_module modules/mod_proxy ...

  5. apache httpd反向代理配置

    apache httpd 2.4.6反向代理的配置,用户访问A server的8080端口,后台会自动请求Bserver的一个端口. 例如,用户访问ip-172-31-28-175的8080端口,后台 ...

  6. apache httpd反向代理的用法

    代理方式有三种:正向代理.透明代理和反向代理 正向代理 httpd通过ProxyRequests指令配置正向代理的功能.例如: ProxyRequests On ProxyVia On <Pro ...

  7. nginx 反向代理配置(二)

    上一篇文章主要是对 nginx 各个模块做了一个介绍,以及对什么是反向代理在文章开头做了一个简单介绍,这篇文章我们主要来看下如何进行 nginx 反向代理的配置 proxy 模块      nginx ...

  8. Httpd Nginx Haproxy反向代理

    Apache反向代理 部署httpd反向代理 准备工作: 三台虚拟机Ip地址分配: linux-node1:192.168.1.5 (源码编译httpd,并且配置proxy用于代理后端的httpd服务 ...

  9. 利用Nginx实现反向代理web服务器

    一.Nginx简介 Nginx是一个很强大的高性能Web服务器和反向代理服务器,它具有很多非常优越的特性: 可以高并发连接 内存消耗少 成本低廉 配置文件非常简单 支持Rewrite重写 内置的健康检 ...

随机推荐

  1. 2.Redis info命令详解

    命令 127.0.0.1:6379> info [server|clients|memory|stats|...] # Server redis_version:5.0.4 #redis版本 r ...

  2. 编程方式实现MySQL批量导入sql文件

    有时候需要在本地导入一些stage环境的数据到本地mysql,面对1000+的sql文件(包含表结构和数据,放在同一个文件夹下),使用navicat一个一个导入sql文件显然有点太慢了,于是考虑使用s ...

  3. 算法(Java实现)—— 贪心算法

    贪心算法 应用场景-集合覆盖问题 假设在下面需要付费的广播台,以及广播台新型号可以覆盖的地区,如何选择最少的广播台,让所有地区都可以接收到信号 广播台 覆盖地区 k1 北京.上海.天津 k2 广州.北 ...

  4. jmeter+jdk环境配置

    一:JDK 安装配置:https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html  ,下载JDK8 安装 1. ...

  5. JavaDailyReports10_08

    ------------恢复内容开始------------ AWT组件 1.1Frame组件与Panel组件 1.1.1显示框架窗口 1 package awt; 2 3 import java.a ...

  6. zookeeper选举算法

    一.ZAB协议三阶段 – 发现(Discovery),即选举Leader过程– 同步(Synchronization),选举出新的Leader后,Follwer或者Observer从Leader同步最 ...

  7. 一文教你轻松搞定ANR异常捕获与分析方法

    1. ANR 产生原理 关于 ANR 的触发原因,Android 官方开发者文档中 "What Triggers ANR?" 有介绍,如下: Generally, the syst ...

  8. JavaScript同步模式,异步模式及宏任务,微任务队列

    首先JavaScript是单线程的语言,也就是说JS执行环境中,负责执行代码的线程只有一个.一次只能执行一个任务,如果有多个任务的话, 就要排队,然后依次执行,优点就是更安全,更简单.缺点就是遇到耗时 ...

  9. Hbase性能调优(一)

    转自:https://blog.csdn.net/yueyedeai/article/details/14648111 1.修改Linux配置 Linux系统最大可打开文件数一般默认的参数值是1024 ...

  10. mac强制关闭应用

    原文链接http://zhhll.icu/2020/08/13/Mac/%E5%BC%BA%E5%88%B6%E7%BB%88%E6%AD%A2%E5%BA%94%E7%94%A8/ 有时候使用Mac ...