简体   繁体   English

NodeJS-具有路由更改的反向代理

[英]NodeJS - Reverse Proxy with Route Changing

I'm currently using NodeJS/Express as a simple domain router running on my VPS on port 80. My routes.coffee looks something like this: 我目前正在使用NodeJS / Express作为运行在端口80上的VPS上的简单域路由器。我的route.coffee看起来像这样:

request = require("request")

module.exports = (app) ->

    #404, 503, error
    app.get "/404", (req, res, next) ->
        res.send "404. Sowway. :("

    app.get "/error", (req, res, next) ->
        res.send "STOP SENDING ERRORS! It ain't cool, yo."

    #Domain Redirects
    app.all '/*', (req, res, next) ->
        hostname = req.headers.host.split(":")[0]

        #Website1.com
        if hostname == 'website1.com'
            res.status 301
            res.redirect 'http://facebook.com/website1'

        #Example2.com
        else if hostname == 'example2.com'
            pathToGo = (req.url).replace('www.','').replace('http://example2.com','')
            request('http://localhost:8020'+pathToGo).pipe(res)

        #Other
        else
            res.redirect '/404'

As you can see in Example2.com, I'm attempting to reverse proxy to another node instance on a different port. 如您在Example2.com中所看到的,我正在尝试将代理反向代理到另一个端口上的另一个节点实例。 Overall it works perfectly, except for one issue. 总体来说,它很完美,除了一个问题。 If the route on the other node instance changes (Redirects from example2.com/page1.html to example2.com/post5) , the URL in the address bar doesn't change. 如果其他节点实例上的路由发生更改(从example2.com/page1.html重定向到example2.com/post5) ,则地址栏中的URL不变。 Would anyone happen to have a nice workaround for this? 有人会碰巧有一个很好的解决方法吗? Or maybe a better way to reverse proxy? 还是反向代理的更好方法? Thanks! 谢谢!

In order to redirect the client, you should set the to 3xx and send a location header. 为了重定向客户端,您应该将3xx并发送位置标头。

I'm not familiar with request module but I believe it follows redirects by default. 我对请求模块不熟悉,但我相信默认情况下它遵循重定向。 On the other hand, you're piping the proxy-request's response to client's response object, discarding the headers and the status code. 另一方面,您正在将代理请求的响应传递到客户端的响应对象,而丢弃头和状态代码。 That's why the clients don't get redirected. 这就是为什么客户端不会被重定向。

Here is a simple reverse HTTP proxy using the built-in HTTP client. 这是一个使用内置HTTP客户端的简单反向HTTP代理。 It's written in but you can easily translate it to and use request module if you want. 它是用编写的,但是您可以轻松地将其翻译为并使用请求模块。

var http = require('http');
var url = require('url');

var server = http.createServer(function (req, res) {
  parsedUrl = url.parse(req.url);

  var headersCopy = {};
  // create a copy of request headers
  for (attr in req.headers) {
    if (!req.headers.hasOwnProperty(attr)) continue;
    headersCopy[attr] = req.headers[attr];
  }

  // set request host header
  if (headersCopy.host) headersCopy.host = 'localhost:8020';

  var options = {
    host: 'localhost:8020',
    method: req.method,
    path: parsedUrl.path,
    headers: headersCopy
  };

  var clientRequest = http.request(options);

  clientRequest.on('response', function (clientResponse) {
    res.statusCode = clientResponse.statusCode;
    for (header in clientResponse.headers) {
      if (!clientResponse.headers.hasOwnProperty(header)) continue;
      res.setHeader(header, clientResponse.headers[header]);
    }
    clientResponse.pipe(res);
  });

  req.pipe(clientRequest);
});

server.listen(80);

// drop root privileges
server.on('listening', function () {
  process.setgid && process.setgid('nobody');
  process.setuid && process.setuid('nobody');
});

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

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