1

I'm developing a NodeJS application with AngularJS. For testing I want to host it on a webserver that runs an apache2 webserver. Googling and Try-And-Error brought me to the fact that I need to simply make Apache forward the requests with the proxy module:

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

But unfortunately, I am not able to see the page. Accessing someurl gives me a blank pages, where no javaScript is loaded.

Node debug log: GET / 200 1.680 ms - 1687

When I access the server via port 3000 I get the following log:

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 - -

Here is everything relevant from the 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" 

How do I get apache to correctly forward the request?

Heiko Becker
  • 556
  • 3
  • 16

3 Answers3

2

The comment from bennettp123 pointed me in the right direction.

The Apaches access.log file shows that there are 404 errors for the javaScript files and the stylesheets.

Adding them as an Alias before the Location directive fixes the problem:

Alias /javascripts "<path_to_node_project>/public/javascripts"
Alias /stylesheets "<path_to_node_project>/public/stylesheets"
Alias /partials    "<path_to_node_project>/public/partials"
Heiko Becker
  • 556
  • 3
  • 16
  • Nice solution. I hadn't thought of that. :) – bennettp123 Jan 13 '15 at 08:21
  • 2
    It's worth mentioning that this solution won't scale if you have multiple applications running behind the proxy. For example, if two apps both return URLs pointing to `/stylesheets`, then there's no way to tell the difference between them. – bennettp123 Jan 13 '15 at 08:41
2

Here is a better solution (found at How to use ProxyPass to serve static files through Express?):

In your case the Apache Config is correct. The only missing part is the change of the <head> element in the index.html like following:

<base href="/someurl/">
Community
  • 1
  • 1
edi
  • 917
  • 7
  • 17
0

According to the doco for 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.

That's what's happening in your case. The reverse proxy is forwarding the initial /someurl request. However, the NodeJS application returns URLs without /someurl in the path, and they aren't matched by the ProxyPass and ProxyPassReverse directives.

There are a few options:

  • Use something like mod_proxy_html, as mentioned by the Apache doco above
  • Alias all the paths (as above)
  • Make the NodeJS app return relative URLs (eg stylesheets/bootstrap.min.css)
  • Add a "reverse proxy" option to the NodeJS application, and prepend the reverse proxy path to all requests.
Community
  • 1
  • 1
bennettp123
  • 419
  • 3
  • 11