proxyee
proxyee copied to clipboard
请问https如何转发到ip呢
比如对于例子中的代码:
public void init(HttpProxyInterceptPipeline pipeline) {
pipeline.addLast(new HttpProxyIntercept() {
@Override
public void beforeRequest(Channel clientChannel, HttpRequest httpRequest,
HttpProxyInterceptPipeline pipeline) throws Exception {
//匹配到百度的请求转发到淘宝
if (HttpUtil.checkUrl(httpRequest, "^www.baidu.com$")) {
pipeline.getRequestProto().setHost("www.taobao.com");
pipeline.getRequestProto().setPort(443);
pipeline.getRequestProto().setSsl(true);
}
// if (HttpUtil.checkUrl(httpRequest, "^www.baidu.com$")) {
// httpRequest.headers().set(HttpHeaderNames.HOST, "www.taobao.com");
// pipeline.getRequestProto().setHost("113.141.190.106");
// pipeline.getRequestProto().setPort(443);
// pipeline.getRequestProto().setSsl(true);
// }
pipeline.beforeRequest(clientChannel, httpRequest);
}
});
我改成这样(将taobao域名改成ip)
public void beforeRequest(Channel clientChannel, HttpRequest httpRequest,
HttpProxyInterceptPipeline pipeline) throws Exception {
//匹配到百度的请求转发到淘宝
// if (HttpUtil.checkUrl(httpRequest, "^www.baidu.com$")) {
// pipeline.getRequestProto().setHost("www.taobao.com");
// pipeline.getRequestProto().setPort(443);
// pipeline.getRequestProto().setSsl(true);
// }
if (HttpUtil.checkUrl(httpRequest, "^www.baidu.com$")) {
httpRequest.headers().set(HttpHeaderNames.HOST, "www.taobao.com");
pipeline.getRequestProto().setHost("113.141.190.106"); //这个ip是我ping www.taobao.com得到的ip
pipeline.getRequestProto().setPort(443);
pipeline.getRequestProto().setSsl(true);
}
pipeline.beforeRequest(clientChannel, httpRequest);
}
});
当改成第二种方式(通过ip)的方式,就不能访问成功,返回的是:
tester@tester tester-tester % curl -k -x 127.0.0.1:9999 https://www.baidu.com
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head><title>403 Forbidden</title></head>
<body>
<h1>403 Forbidden</h1>
<p>You don't have permission to access the URL on this server.<hr/>Powered by Tengine</body>
</html>
tester@tester tester-tester %
我是有这样的需求,需要从将域名转发到不同的nginx对应的ip(都是https的协议),所以请教一下,非常感谢~
现在的实现还支持不了这个场景,等我明天改下
不过我看了下你的需求,如果你的后端服务如果本来就是可以用ip访问的应该就没问题
感谢回复。我的服务在浏览器中后端服务(通过nginx反向代理转发的)直接通过ip访问,nginx会强制跳转到域名。命令行curl返回的错误码是405 Not Allowed。 我尝试过在beforeConnect修改远程ip地址,但是没有成功(我对这个逻辑确实也不太熟,最近刚刚开始使用才了解的)。
@nanshuge 已经修复啦,等1.7.0发布好再试试看