2013-07-03 19 views
1

進行代理時包含子域我設置一個簡單的HTTPS服務器來處理以下到情況:在節點

  1. 請求https://localhost:5000/那些在我的目錄通過connect.static(__dirname)提供匹配的文件。這適用於像我的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'); 

回答

0

萬一有人碰到這個老問題,你應該使用HTTP代理的changeOrigin選項。