3

我在尝试使用 http-proxy 路由到 localhost 时遇到了麻烦。

我正在使用 IISNODE,但来自控制台应用程序也无法正常工作。

例如,如果“target”设置为 google,它也适用于在此代码段中创建的 local:9000,但不适用于在我的本地 IIS 中运行的站点

有任何想法吗?

更新:现在发布的代码片段对我有用,但仍然缺少很多工作。

var port = process.env.PORT;

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

// http Server 
var proxy = new httpProxy.createServer({});

var httpServer = http.createServer(function (req, res) {

    console.log('request received: ' + req.path);    

    var target = 'http://myapp';

    if (!req.url.toString().startsWith('/')) {
        target = target + '/';
    }

    target = target + req.url;

    console.log('routing request to ' + target);

    var urlObj = url.parse(req.url);

    req.headers['host'] = urlObj.host;
    req.headers['url'] = urlObj.href;

    proxy.web(req, res, {
        host: urlObj.host,
        target: target,
        enable: { xforward: true }
    });

});

httpServer.listen(port);

String.prototype.endsWith = function (s) {
    return this.length >= s.length && this.substr(this.length - s.length) == s;
}

String.prototype.startsWith = function (str) {
    return this.indexOf(str) == 0;
};
4

1 回答 1

0

I tested a simple node http-proxy outside iisnode and it works fine routing request to any sites, localhost iis or www.

But in iisnode, I can't make it work properly either. The request headers are not exactly the same and we're running on a named pipe instead of a tcp port, it's difficult to find what is causing the bad routing.

For instance, if we change req.url to '/', then it sends the request to the same domain the node app is running on, instead of the target domain.

A possible solution would be to use IIS reverse proxy (using ARR and URL Rewrite) to forward requests to your node proxy app running in standalone on a tcp port.

I have tested it and it works fine. I can give config example if needed.

Update: Here's how I made it work using IIS reverse proxy and a node proxy app. It doesn't use iisnode though:

A simple node proxyServer.js app:

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

var proxy = httpProxy.createProxyServer({})

var server = require('http').createServer(function(req, res) {
  console.log(req.url)  
  proxy.web(req, res, { target: 'http://nodejs.org' })
}).listen(3000)

Running it using node.exe proxyServer.js

In IIS, we need to activate the ReverseProxy in Application Request Routing:

IIS server -> ARR:

IIS Server ARR

Proxy settings...:

ARR Proxy settings...

Enable the proxy like this:

Activate reverse proxy like this

And then I create a dedicated website node.mydomain.com and a rewrite rule for this website in IIS:

<rule name="ReverseProxyInboundNodeProxy" stopProcessing="true">
    <match url="(.*)" />
    <conditions>
        <add input="{CACHE_URL}" pattern="^(https?)://" />
    </conditions>
    <action type="Rewrite" url="{C:1}://localhost:3000/{R:1}" />
</rule>

It's kind a "double proxy" solution but it works. I still have issue making a node proxy server runs through iisnode. Of course you need ARR and URL Rewrite installed in IIS.

于 2014-05-27T21:44:37.997 回答