我通过在应用程序的index.html 中使用HTML“Base”标签解决了我的环境中的这个问题,然后确保应用程序中的所有链接都是相对的。这将有助于确保浏览器请求适当的路径。
值得注意的是,由于我的应用程序是 SPA(单页应用程序),因此所有资产始终有效,因为实际导航始终位于根目录。
在你的情况下标签(在<head>
元素)可能看起来像:
<base href="/nodeapp/">
我对应的 Apache 配置很简单:
<Location /nodeapp>
ProxyPass http://127.0.0.1:9000
ProxyPassReverse http://127.0.0.1:9000
# other Apache directives here
</Location>
我的应用程序碰巧使用 AngularJS,我可以使用 # 字符到达应用程序的“根”。例如,如果我想去/settings
(这将是/nodeapp/settings
在生产中)我会指定#settings
as the href
对于任何给定的链接。您的里程可能会有所不同,但这确实值得一看https://docs.angularjs.org/guide/ https://docs.angularjs.org/guide/$location 如果你使用 AngularJS 并摆弄html5Mode
设置以查看最适合您的部署的设置。
我不必向 Express 讲述其挂载点,因为浏览器正在使用<base>
上面的标签,以及ProxyPass/ProxyPassReverse
Apache 指令适当地剥离/nodeapp
在将其交给 Express 之前。
虽然我不必接触 Express 的路由器(如上所述),但理解以下内容可能仍然有用。如果您使用 Express 4.x,“安装”Express 应用程序据称比以前版本的 Express 更容易。这里有一些很好的例子:https://github.com/visionmedia/express/wiki/New-features-in-4.x https://github.com/visionmedia/express/wiki/New-features-in-4.x
从该页面:
想象一下您的项目中的一个示例routes/people.js。
var people = express.Router();
people.use(function(req, res, next) {
});
people.get('/', function(req, res, next) {
});
module.exports.people = people;
您可以将其安装在 /people 路径下,以便任何对 /people/* 的请求都将路由到 people 路由器。
app.use('/people', require('./routes/people').people);
该页面还会引导您访问 Express 4.x Router API:http://expressjs.com/4x/api.html#router http://expressjs.com/4x/api.html#router
但是,如果您使用 Apache 的,则通常不需要此技术ProxyPass
重写入站请求。
如果您的应用程序不是 SPA,您可能会幸运地使用以下命令进行动态内容重写mod_proxy_html
。我在这方面取得的成功有限,但 AngularJS 最终不太喜欢它。但是,我相信它可能适合不同的应用程序。
如果您选择使用mod_proxy_html
您可以在此处查看 StackOverflow 问题和答案以获取更多信息:ProxyHTML 重写 URL https://stackoverflow.com/questions/14431090/proxyhtml-to-rewrite-url