Forum

How to set up Hiawatha to serve static content and also forwarding to/from a NodeJS server?

Mohawky
5 July 2015, 17:32
I'm sorry if my question is too noob but I'm not really sure how to understand the Hiawatha documentation!

I have this use case where I'm running a server where some of the contents are static HMTL, Javascript, and CSS stuff (actually I'm using AngularJS), and then I have some functionality where I connect to a NodeJS server (on the same machine for now).

So if my webpage is called mysite.com then I want to have this sort of forwarding:

mysite.com/ <-> /home/noob/mysite/index.html

mysite.com/somewhere.html/ <-> /home/noob/mysite/someplace/somewhere.html

$http.get('http://mysite.com:80/nodestuff?do=something').success(...) <-> 127.0.01:1337 (NodeJS server)

Also I would like to know how make load balancing with Hiawatha so that for example I can have 2 NodeJS instances at 127.0.0.1:1337 and 127.0.0.1:1338 sharing the same URL?

Thanks!
Hugo Leisink
6 July 2015, 10:00
I don't know much about Node.JS, but I guess it needs the reverse proxy functionality. The manual page explains how to use it.
Mohanwky
6 July 2015, 10:34
But I don't think that the manual page describe how to set up the reverse proxy so that a specific URL forward/proxy to/from a specific port (as described in my question) as the manual more describe how to forward a whole IP address or domain name to/from a specific port (unless I'm missing something), and also I can't see how to set up load balancing when having several ports sharing one URL (for the use case described in my question).

Maybe you would like to write an example?

Thanks.
Hugo Leisink
6 July 2015, 10:46
Hiawatha's reverse proxy doesn't support load balancing. That's why it is not described. And by the way, there is no point in load balancing when both end points are at the same machine.
Mohawky
6 July 2015, 13:03
Well actually when using NodeJS then there is a very good point in having load balancing even when the endpoints are on the same machine. And that's because when I have a NodeJS process attached to a port then that NodeJS process is running in only one thread. And even though there is an experimental cluster function in NodeJS, then the recommended way of using it is to have several NodeJS processes running independently where each is attached to a separate port (as described in my question), and then use something like Nginx to do the load balancing. Therefore I was hoping that Hiawatha could do the same as I like many of the security features that you have build into the system.
Hohawky
6 July 2015, 14:00
Anyway, is it possible to proxy/forward a full URL (as described in my question) to a specific port. In the manual I can only find examples where an IP address or domain name is proxy/forwarded to a port (but I need to do it based on the whole URL as can be done in for example Nginx or Apache)? if it is possible with Hiawatha, then I would really appreciate if you will write an example showing how to do it!

If load balancing between to endpoints on the same machine never will become a feature in Hiawatha, then maybe I can solve my problem with using JXcore (which is a NodeJS multithread fork) instead of NodeJS, even though I'm not really sure how well it function and how good it perform, and also I'm afraid what problems might come when using it as writing multithreading code is known to be extremely complicated!
Hugo Leisink
6 July 2015, 14:21
Have you checked the manual page? Because it already contains an example.
Mohawky
6 July 2015, 15:04
By Manual page, do you then mean the reference guide at: https://www.hiawatha-webserver.org/manpages/hiawatha ?

About reverse proxy then it says the following:

ReverseProxy [!]<pattern> [<skip directories>] http[s]://<hostname>[:<port>][/<path>] [<timeout>] [keep-alive]
Forward the request with URLs that match the regular expression <pattern> to another webserver, where <path> is placed before the original URL. When <hostname> is an IP address, the value of the Host HTTP header is unchanged. Otherwise, it is replaced with the value of <hostname>. The optional <skip directories> is a number that indicates how many directories in the original URL should be skipped when sending it to the final webserver. The connection is closed after <timeout> seconds, which is set to 5 seconds by default. By default, Hiawatha doesn't use keep-alive connections to the final webserver. You can enable this by adding 'keep-alive' to the configuration line. When specifying multiple reverse proxies for one (virtual) host, Hiawatha prefers reverse proxies with a scheme (HTTP/HTTPS) matching the one of the client connection. See also CAcertificates.
Example: ReverseProxy ^/icons/ 1 http://resources.lan/images

I don't understand the example as I can't see what you mean with an URL starting with '^/icons/' ? But maybe it is supposed to mean that the '1' simple skips the domain name, and therefore we are matching something like www.domain.com/icons/ ?

So related to my question, then when I get to my work computer maybe I could try something like:

ReverseProxy ^/nodestuff/ 1 127.0.0.1:1337 ?

Anyway, do you have any plans of writing a real book/manual about Hiawatha? I think that if you are interested in getting Hiawatha used by many people then a real user friendly book/manual might help getting people into it and understanding the project? Most other webserver projects actually have easy to read and understand books/manuals written about them (for example Apache, Nginx, and Varnish)! I think that the manual page is very much written in a 'you already know what I mean style', and as a software developer I can understand that it is more interesting just writing code than to document it, but also I think it can be hard to figure out what others projects are about and how to use them unless there are carefully explained! I mean can you understand Apache from just reading a reference guide about it and nothing else?

Anyway, about the reverse proxy then it would be important to know if it is completely asynchronous like for example Nginx (or is it blocking like Apache) ?
Mohawky
6 July 2015, 15:22
About my suggestion to make the reverse proxy, then after reading this forum post then I think that it's more like:
VirtualHost {
Hostname = www.mysite.com
....
ReverseProxy /nodestuff/ 127.0.0.1:1337/
}

But where is that documented?
Hugo Leisink
6 July 2015, 15:31
It is documented in the manual page. Your config option is not quite correct. The correct version is:
ReverseProxy ^/nodestuff/ http://127.0.0.1:1337/


The '1' in the example means that Hiawatha skips 1 directory. So, if the request is to http://www.domain.com/some/random/path, Hiawatha will skipp the first directory ('/some') and use /random/path when forwarding the request.

No, I have no plans for writing a book for the simple reason that Hiawatha has not many users.

Hiawatha's proxy is blocking. Asynchronous is pointless because not many browsers use request pipelining (they support it, but it is disabled by default).
Mohawky
6 July 2015, 16:35
As I understand the problem about whether or not the reverse proxy should be asynchronous, then if it is synchronous then it will use resources simple waiting for the replies (waste of CPU cycles and unnecessary use of memory which then limits the amount of possible client connections for a given hardware)!
Hugo Leisink
7 July 2015, 05:55
The CPU is not used much while waiting. It does use some memory, but since Hiawatha is a multithreading application, the amount of used memory won't be a bottleneck.
Mohawky
7 July 2015, 12:08
I think that I will try to build my own asynchronous NodeJS module to do all the firewall stuff, load balancing, DoS protection, and reverse proxies etc., and then maybe later do some benchmark tests to compare it with for example Hiawatha and other webservers.

But anyway, then thanks for sharing you info and good luck with the Hiawatha project.
This topic has been closed.