Dom*_*nic 4 javascript proxy node.js node-http-proxy
我试图让一个非常简单的代理使用node-http-proxy,我希望只返回谷歌的内容:
const http = require('http');
const httpProxy = require('http-proxy');
const targetUrl = 'http://www.google.co.uk';
const proxy = httpProxy.createProxyServer({
target: targetUrl
});
http.createServer(function (req, res) {
proxy.web(req, res);
}).listen(6622);
Run Code Online (Sandbox Code Playgroud)
例如,我希望http:// localhost:6622/images/nav_logo242.png代理到http://www.google.co.uk/images/nav_logo242.png而不是返回404找不到.
谢谢.
将http-proxy选项设置changeOrigin为true,它将host自动在请求中设置标头。
虚假站点依靠此host头正常工作。
const proxy = httpProxy.createProxyServer({
target: targetUrl,
changeOrigin: true
});
Run Code Online (Sandbox Code Playgroud)
作为express-http-proxy的替代方法,您可以尝试使用http-proxy-middleware。它支持https和websockets。
const proxy = require('http-proxy-middleware');
app.use('*', proxy({
target: 'http://www.google.co.uk',
changeOrigin: true,
ws: true
}));
Run Code Online (Sandbox Code Playgroud)
您需要设置Host请求的标头
const http = require('http');
const httpProxy = require('http-proxy');
const targetHost = 'www.google.co.uk';
const proxy = httpProxy.createProxyServer({
target: 'http://' + targetHost
});
http.createServer(function (req, res) {
proxy.web(req, res);
}).listen(6622);
proxy.on('proxyReq', function(proxyReq, req, res, options) {
proxyReq.setHeader('Host', targetHost);
});
Run Code Online (Sandbox Code Playgroud)
在快速应用程序内部,在代理某些请求时使用express-http-proxy可能更容易.
const proxy = require('express-http-proxy');
app.use('*', proxy('www.google.co.uk', {
forwardPath: function(req, res) {
return url.parse(req.originalUrl).path;
}
}));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2166 次 |
| 最近记录: |