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. SpringBoot进阶教程(六十八)Sentinel实现限流降级

    前面两篇文章nginx限流配置和SpringBoot进阶教程(六十七)RateLimiter限流,我们介绍了如何使用nginx和RateLimiter限流,这篇文章介绍另外一种限流方式---Senti ...

  2. 配置简单的拦截器java中

    springMVC.xml文件中==== <!-- 拦截器 --> <mvc:interceptors> <mvc:interceptor> <mvc:map ...

  3. JavaScript中对象是否需要加引号?

    对象的属性名是包括空字符串在内的所有字符串. 那么问题来了,我们平时定义的对象如下,是没有引号""or''的,这样不加引号有没有错呢? 答案是,加不加分情况!但加了肯定没问题... ...

  4. Linux 内核参数 优化

    Linux 内核参数 优化 目录 Linux 内核参数 优化 1.编辑内核配置文件 2.参数及简单说明 3.客户端的典型状态转移参数 4.TCP重传参数 5.实现Nginx高并发的内核参数优化 生效配 ...

  5. dp的冗余(选数类)

    我们先来看一个例题: 在一个长度为n的序列中选出任意个数的数,要求每m个数中至少一个被选,要求选的数之和最小化. 我们很容易想出用f[i][j]来表示前i个数选的最后一个数是j,也就有 for(int ...

  6. IDEA关联mysql失败Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezon'

    时区错误,MySQL默认的时区是UTC时区 要修改mysql的时长 在mysql的命令模式下,输入: set global time_zone='+8:00'; 再次连接成功

  7. 进制及其字符串之间互转——C#

    本文介绍进制数转进制数,及每个进制对应的字符串 一.首先进制数转进制数(int-->int) 1.二进制数与十进制数互转: (1)二进制数转十进制数:还没找到 (2)十进制数转二进制数:目前还没 ...

  8. Modbus仿真器 Modbus Poll 和Modbus Slave详细图文教程

    Modbus Poll 是Witte Software公司开发的的Modbus主机仿真器,用于测试和调试Modbus从设备.软件支持ModbusRTU.ASCII.TCP/IP协议.支持多设备监控,可 ...

  9. Label_img&a

    绝对路径 相对路径 从根目录开始写 从引用的文件所在目录开始写 也可以作为链接提示 target = _blank 新窗口打开 = _self 默认值 本窗口打开 = _new 新窗口打开 = _pa ...

  10. 在kotlin用jni调用c++的dll中踩的坑

    在kotlin用jni调用c++的dll中踩的坑 can't find dependents libraries 不是个有效的32位程序(或者是?????32??????) 常规检查 java 指针 ...