【发布时间】:2017-05-11 09:56:27
【问题描述】:
我想做的事:
代理一个在https://127.0.0.1:443/api/ 上运行的java api,以及在非SSL http://127.0.0.1:1337/ 上运行的我的UI,以解决一些CORS 问题。
我的尝试:
- 将 SSL 端口 443 处的 api 代理到我的非 SSL 开发端口 1338。
- 将我的 UI 代理到 1337
- 代理 1137 到
:8080/index.html和代理 1338 到:8080/api/ - 从 localhost:8080 访问我的应用
我的问题:
用户界面很好......但我无法在:8080/api/httpSession/init访问API
是的,我仍然可以通过 https://localhost/api/httpSession/init 访问 API
api.js - 在 :1337 处呈现 index.html
var app = express();
app.all('*', function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
var options = {
changeOrigin: true,
target: {
https: true
}
};
httpProxy.createServer(443, '127.0.0.1', options).listen(1338);
start.js - 代理 1337 和 1338 到 8080
// First I start my two servers
uiServer.start(); // renders index.html at 1337
apiServer.start(); //
// I attempt to patch them back into one single non-SSL port.
app
.use('/', proxy({target: 'http://localhost:1337/'}))
.all('/api/*', proxy({target: 'http://localhost:1338/'}))
.listen(8080, function () {
console.log('PROXY SERVER listening at http://localhost:%s', 8080);
});
【问题讨论】:
-
您是否有理由两次代理您的 api?首先到端口 1338,然后到端口 8080 在 /api/?
-
我知道这是题外话,但您也可以使用带有反向代理的 Web 服务器在同一端口上实现此目的。例如,您可以使用 IIS 或 Nginx 在一个虚拟服务器上分离反向代理端点。