如何使 Apache 正确代理到 NodeJS?

How to make Apache proxy to NodeJS correctly?

我正在使用 AngularJS 开发 NodeJS 应用程序。 为了进行测试,我想将它托管在运行 apache2 网络服务器的网络服务器上。 谷歌搜索和尝试错误让我意识到我需要简单地让 Apache 使用代理模块转发请求:

<Location /someurl>
  ProxyPass http://mydomain:3000/
  ProxyPassReverse http://mydomain:3000/
</Location>

但不幸的是,我无法看到该页面。 访问 someurl 给我一个空白页面,其中没有加载 javaScript。

节点调试日志: GET / 200 1.680 ms - 1687

当我通过端口 3000 访问服务器时,我得到以下日志:

GET / 304 53.814 ms - -
GET /javascripts/directives.js 200 14.369 ms - 6521
GET /javascripts/services.js 200 8.532 ms - 2054
GET /stylesheets/bootstrap.min.css 304 4.173 ms - -
GET /stylesheets/lmm.css 304 4.131 ms - -
GET /javascripts/jquery.min.js 304 4.203 ms - -
GET /javascripts/bootstrap.min.js 304 2.060 ms - -
GET /javascripts/angular.min.js 304 2.486 ms - -
GET /javascripts/angular-route.min.js 304 2.562 ms - -
GET /javascripts/angular-translate.min.js 304 2.643 ms - -
GET /javascripts/app.js 304 2.526 ms - -
GET /javascripts/controller.js 304 2.768 ms - -
GET /javascripts/translations.js 304 1.368 ms - -
GET /partials/footer.html 304 2.391 ms - -
GET /partials/home.html 304 2.202 ms - -
GET /fonts/glyphicons-halflings-regular.woff 304 1.602 ms - -

以下是 acces.log 的所有相关内容:

myIP - - [13/Jan/2015:10:01:19 +0100] "GET /someurl HTTP/1.1" 200 852 "-" 
myIP - - [13/Jan/2015:10:01:19 +0100] "GET /stylesheets/bootstrap.min.css HTTP/1.1" 404 513 "http://mydomain/someurl" 
myIP - - [13/Jan/2015:10:01:19 +0100] "GET /stylesheets/lmm.css HTTP/1.1" 404 504 "http://mydomain/someurl" 
myIP - - [13/Jan/2015:10:01:19 +0100] "GET /javascripts/jquery.min.js HTTP/1.1" 404 509 "http://mydomain/someurl" 
myIP - - [13/Jan/2015:10:01:19 +0100] "GET /javascripts/bootstrap.min.js HTTP/1.1" 404 513 "http://mydomain/someurl" 
myIP - - [13/Jan/2015:10:01:19 +0100] "GET /javascripts/angular.min.js HTTP/1.1" 404 510 "http://mydomain/someurl" 
myIP - - [13/Jan/2015:10:01:19 +0100] "GET /javascripts/angular-route.min.js HTTP/1.1" 404 517 "http://mydomain/someurl" 
myIP - - [13/Jan/2015:10:01:19 +0100] "GET /javascripts/angular-translate.min.js HTTP/1.1" 404 520 "http://mydomain/someurl" 
myIP - - [13/Jan/2015:10:01:19 +0100] "GET /javascripts/controller.js HTTP/1.1" 404 509 "http://mydomain/someurl" 
myIP - - [13/Jan/2015:10:01:19 +0100] "GET /javascripts/app.js HTTP/1.1" 404 503 "http://mydomain/someurl" 
myIP - - [13/Jan/2015:10:01:19 +0100] "GET /javascripts/directives.js HTTP/1.1" 404 510 "http://mydomain/someurl" 
myIP - - [13/Jan/2015:10:01:19 +0100] "GET /javascripts/services.js HTTP/1.1" 404 507 "http://mydomain/someurl" 
myIP - - [13/Jan/2015:10:01:19 +0100] "GET /javascripts/translations.js HTTP/1.1" 404 511 "http://mydomain/someurl" 
myIP - - [13/Jan/2015:10:01:19 +0100] "GET /javascripts/bootstrap.min.js HTTP/1.1" 404 512 "http://mydomain/someurl" 
myIP - - [13/Jan/2015:10:01:19 +0100] "GET /javascripts/angular.min.js HTTP/1.1" 404 510 "http://mydomain/someurl" 
myIP - - [13/Jan/2015:10:01:19 +0100] "GET /javascripts/angular-route.min.js HTTP/1.1" 404 516 "http://mydomain/someurl" 
myIP - - [13/Jan/2015:10:01:19 +0100] "GET /javascripts/angular-translate.min.js HTTP/1.1" 404 520 "http://mydomain/someurl" 
myIP - - [13/Jan/2015:10:01:19 +0100] "GET /javascripts/app.js HTTP/1.1" 404 502 "http://mydomain/someurl" 
myIP - - [13/Jan/2015:10:01:19 +0100] "GET /javascripts/controller.js HTTP/1.1" 404 509 "http://mydomain/someurl" 
myIP - - [13/Jan/2015:10:01:19 +0100] "GET /javascripts/directives.js HTTP/1.1" 404 509 "http://mydomain/someurl" 
myIP - - [13/Jan/2015:10:01:19 +0100] "GET /javascripts/services.js HTTP/1.1" 404 507 "http://mydomain/someurl" 
myIP - - [13/Jan/2015:10:01:19 +0100] "GET /javascripts/translations.js HTTP/1.1" 404 511 "http://mydomain/someurl" 

如何让 apache 正确转发请求?

bennettp123 的评论为我指明了正确的方向。

Apaches access.log 文件显示 javaScript 文件和 样式表。

在 Location 指令修复问题之前将它们添加为别名:

Alias /javascripts "<path_to_node_project>/public/javascripts"
Alias /stylesheets "<path_to_node_project>/public/stylesheets"
Alias /partials    "<path_to_node_project>/public/partials"

根据 ProxyPassReverse 的文档:

Only the HTTP response headers specifically mentioned above will be rewritten. Apache will not rewrite other response headers, nor will it rewrite URL references inside HTML pages. This means that if the proxied content contains absolute URL references, they will by-pass the proxy. A third-party module that will look inside the HTML and rewrite URL references is Nick Kew's mod_proxy_html.

这就是你的情况。反向代理正在转发初始 /someurl 请求。但是,路径中没有 /someurl 的 NodeJS 应用程序 returns URL,并且它们与 ProxyPass 和 ProxyPassReverse 指令不匹配。

有几个选项:

  • 使用类似 mod_proxy_html 的内容,如上面的 Apache doco 所述
  • Alias所有路径(如
  • 制作 NodeJS 应用 return 相对 URL(例如 stylesheets/bootstrap.min.css
  • 向 NodeJS 应用程序添加 "reverse proxy" 选项,并为所有请求添加反向代理路径。

这是一个更好的解决方案(在 How to use ProxyPass to serve static files through Express? 找到):

在您的情况下,Apache 配置是正确的。唯一缺少的部分是 index.html 中 <head> 元素的更改,如下所示:

<base href="/someurl/">