【问题标题】:Include subdomain when proxying in node在节点中代理时包含子域
【发布时间】:2013-07-03 16:37:49
【问题描述】:

我已经设置了一个简单的 HTTPS 服务器来处理以下情况:

  1. 通过connect.static(__dirname) 处理在我的目录中有匹配文件的https://localhost:5000/ 请求。这对我的 index.html 和 CSS 文件等所有内容都非常有用,并且可以完全按照我的需要工作。

  2. https://localhost:5000/api 的请求应重定向到https://subdomain.mydomain.com:443/api

代理正在通过 HTTPS 正确传输所有内容,并且 SSL 握手部分似乎完全按照我的预期工作。问题是我的 API 使用子域来确定要连接到什么数据库以及要返回什么数据。所以,我的 API 看到了请求

https://localhost:5000/api/something

而不是

https://subdomain.mydomain.com/api/something

并抛出一个错误,告诉我必须提供子域。

做代理时如何告诉节点代理转发(或使用)域/子域?

这是我的代码:

var fs = require('fs');
var connect = require('connect'),
    https = require('https'),
    httpProxy = require('http-proxy'),
    options = {
      key: fs.readFileSync('key.pem'),
      cert: fs.readFileSync('cert.pem')
    },
    endpoint = {
        host: 'subdomain.mydomain.com',
        port: 443,
        prefix: '/api',
        target: { https: true }
    };

var proxy = new httpProxy.RoutingProxy();
var app = connect()
    .use(connect.logger('dev'))
    .use(function(req, res, next) {
        if (req.url.indexOf(endpoint.prefix) === 0) {
            proxy.proxyRequest(req, res, endpoint); 
        } else {
            next();
        }   
    })  
    .use(connect.static(__dirname));

https.createServer(options, app).listen(5000);
console.log('Listening on port 5000');

【问题讨论】:

    标签: node.js proxy subdomain


    【解决方案1】:

    万一有人碰到这个老问题,你应该使用 http-proxy 的 changeOrigin 选项。

    【讨论】:

      猜你喜欢
      • 2016-12-26
      • 2021-06-09
      • 1970-01-01
      • 1970-01-01
      • 2011-07-24
      • 2010-11-30
      • 1970-01-01
      • 2015-05-08
      • 2015-10-20
      相关资源
      最近更新 更多