如何使用nodejs设置代理服务器

How setup a proxy server using node js ?

本文关键字:代理服务器 设置 nodejs 何使用      更新时间:2024-03-25

i使用节点js设置代理我的脚本是这样的

var http = require('http'),
 httpProxy = require('http-proxy');
var options = {
  pathnameOnly: true,
  router: {
    '/wamp_application/': '127.0.0.1:80/',
    '/node_application': '127.0.0.1:1331',
    
  }
};

var proxyServer = httpProxy.createServer(options);
proxyServer.listen(8888);

现在是这样工作的当我访问127.0.0.1:8888/wamp_application时,我得到127.0.0.1:80但我想要像这个

127.0.0.1:8888/wamp_application/app1->127.0.0.1:80/app1127.0.0.1:8888/wamp_application/app2->127.0.0.1:80/app2127.0.0.1:8888/wamp_application/app3->127.0.0.1:80/app3等等

我试过这个

  router: {
        '/wamp_application/*': '127.0.0.1:80/*',
        '/node_application/*': '127.0.0.1:1331/*',
       };

它不起作用。实际上,我希望php和node应用程序都能从同一个端口访问。我该怎么做?

如果您只想转发到80以外的端口(反向代理),nginX是一个更好的选择。使用nginx.conf绑定应用程序。

请参阅:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass