节点HTTP代理403禁止发布请求

时间:2013-04-19 23:41:07

标签: proxy

我使用Node-http-proxy将我的请求从端口sample.com:80代理到端口sample.com:8080,其示例如下:

var httpProxy = require('http-proxy');

httpProxy.createServer(8080, 'localhost').listen(80);

http.createServer(function (req, res) {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
  res.end();
}).listen(9000);

当我这样做:使用POST sample.com:80卷曲时,我得到403 Forbidden response。但是当我用GET卷曲时301 permanent redirect。我的问题是:

是否可以使POST和GET的行为相同,总是返回200状态代码而不是403或301?

由于

1 个答案:

答案 0 :(得分:0)

var fmt = require('fmt');
var httpProxy = require('http-proxy');

fmt.sep();
console.log();
fmt.title("\033[1;36mPROXY SERVER STARTED\033[0m");
console.log();
fmt.field("\033[1;34mPROXY PORT\033[0m", 80);
fmt.field("\033[1;34mTARGET PORT\033[0m", 8080);
fmt.field("\033[1;34mTARGET ADDRESS\033[0m", '127.0.0.1');

var server = httpProxy.createServer(function(req, res, proxy)
{
    proxy.proxyRequest(req, res, { host: '127.0.0.1', port: 8080, buffer: httpProxy.buffer(req) });

    var reqBody = '';

    req.on('data', function(chunk)
    {
        reqBody += chunk;
    });

    req.on('end', function()
    {
        console.log();
        fmt.sep();
        console.log();
        fmt.title("\033[1;36mREQUEST FROM SHARED CLOUD\033[0m");
        console.log();
        fmt.dump(req.connection.remoteAddress, "\033[1;35mADDRESS\033[0m");
        console.log();
        fmt.msg("\033[1;35mHEADERS\033[0m :");
        fmt.dump(req.headers);
        console.log();
        fmt.msg("\033[1;35mBODY\033[0m :");
        fmt.dump(reqBody);
        console.log();
    });
});

server.proxy.on('proxyError', function(err, req, res)
{
    fmt.title("\033[1;36mPROXY STATUS\033[0m");
    console.log();
    fmt.dump('failed to proxy on port ' + 8080, "\033[1;31mERROR\033[0m");
    res.end();
});

server.proxy.on('end', function()
{
    fmt.title("\033[1;36mPROXY STATUS\033[0m");
    console.log();
    fmt.dump('request was proxied successfully', "\033[1;33mSTATUS\033[0m");
});

server.listen(80);