简体   繁体   English

使用node-http-proxy,通过proxyRequest更改URL

[英]Using node-http-proxy, changing url with proxyRequest

When modifying a node-http-proxy example ... 修改node-http-proxy示例时 ...

The Issue: When manually changing the req.headers.host to a remote host ('example.com'), the url in the the browser is not changing. 问题:当手动将req.headers.host更改为远程主机('example.com')时,浏览器中的URL并未更改。

Note: I thought this was working, however when using google.com as my example, it turned out google was detecting the proxy and changing the host. 注意:我以为这可行,但是当以google.com为例时,事实证明google正在检测代理并更改主机。

Question: Is there a way to change the url in the browser to a remote host (ie. example.com), everytime? 问题:有没有办法每次都将浏览器中的URL更改为远程主机(例如example.com)? Thanks! 谢谢!

var util = require('util'),
    colors = require('colors'),
    http = require('http'),
    httpProxy = require('../../lib/node-http-proxy');

//
// Http Server with proxyRequest Handler and Latency
//
var proxy = new httpProxy.RoutingProxy();
http.createServer(function (req, res) {
  req.headers.host = 'example.com'; // manually setting example.com
  var buffer = httpProxy.buffer(req);
  setTimeout(function () {
    proxy.proxyRequest(req, res, {
      port: 80,
      host: 'example.com',
      buffer: buffer
    });
  }, 200);
}).listen(8004);



util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '8004 '.yellow + 'with proxyRequest handler'.cyan.underline + ' and latency'.magenta);
util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9000 '.yellow);

A few solutions that will do this are: 可以做到这一点的一些解决方案是:

  • Issue a 301 redirect. 发出301重定向。
  • Use IPTables to DNAT and change the desination address. 使用IPTables进行DNAT并更改目标地址。
  • Use TPROXY to transparently proxy the requests without modifying the packets. 使用TPROXY透明地代理请求,而无需修改数据包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM