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. 记录第一次使用Vivado——以全加器为例子

    从altera转战xilinx,经典的FPGA到ZYNQ系列,第一站就是先熟悉编译软件Vivado.我就直接跳过软件安装部分了,如有疑问,可以在评论区提出来,我看到了就帮你解答. 首先是是打开界面 然 ...

  2. 工作中用的sql

    //字段是空字符串或者null select * from blade_process_should_pay_invoice where is_deleted = 0 and process_inst ...

  3. sql中筛选条件为空值

    <select id="getEmployeeBasicInformationList" resultType="org.springblade.entity.Al ...

  4. Python & Matplotlib: Monte Carlos Method

    Hey! 这里是Lindy:) Hope you guys are doing well! 今天想记录的概念叫做 蒙特·卡罗 方法,是今年在cs课上老师做的扩展延伸.其实我在初次接触这个概念时觉得很新 ...

  5. Unity Package Manager

    (注:Unity 2018.1及以后的版本才可以使用Package Manager.) 一个package是一个容器,里面放的是Assets, Shaders, Textures, plug-ins, ...

  6. Docker 的 2020,实 "鼠" 不易!

    元旦你们出去嗨,栈长在家撸文章,惨惨惨- 没错, Docker 的 2020 年也过的不是很顺利,可以说是流年不利.命运多舛,一年发生两件大事,太折腾! 相信大家也已经看到很多相关的报道了,但同时也有 ...

  7. java线程调度

    JAVA线程调度分抢占式和协调式 协调式的线程切换由线程本身自己控制,好处是实现简单,当前线程只有当事情做完才会通知系统进行切换并没有同步开销,坏处是容易引发事故,假如阻塞的线程由于代码BUG没有通知 ...

  8. [学习笔记]Golang--基础数据类型

    1,不同类型的变量不能互相赋值或者操作,如var a int8 = 16var b int = 23c := a + b 会报错,且int虽然默认32位,但和int32是不同的类型 iota只在声明枚 ...

  9. int和Integer的区别?包装类?装箱?拆箱?

    int和Integer的区别: 1) int是基本数据类型,直接存储的数值,默认是0; 2) Integer 是int的包装类,是个对象,存放的是对象的引用,必须实例化之后才能使用,默认是null; ...

  10. php 二位数组 转一维数组

    $result = []; array_map(function ($value) use (&$result) { $result = array_merge($result, array_ ...