Reverse Proxy and its support by Monkey HTTP server

Reverse proxy is the process of routing client requests to backend servers. It sits in between the client and backend servers. It takes a request from the client and assigns it to one of the servers. It fetches the requested resource from the server and returns it to the client. Some of the advantages of using a reverse proxy are :

  1. Helps clients access servers that are configured beyond a firewall. Only the IP address of the proxy is publicized the outer world.
  2. Helps balance loads among different backend servers. It helps choose which server should take the load of the incoming request from the client.
  3. If one of the servers fails to deliver the content of the client request, the request can be assigned to another server.
  4. It is also possible to cache information that is requested frequently by clients. This reduces the load of having to fetch the resource from the server each time.

The following diagram gives a picture of the entire system.


Monkey plugin for reverse proxy :

The monkey server provides support for reverse proxy through the plugin proxy_reverse. You can read more about it here.

Workflow :

This is a brief workflow and where the reverse proxy plugin fits in :

  1. Incoming requests from clients
  2. The request URI is processed to check if it satisfies the regex rules
  3. Checked if a match is found on the basis of the previous search
  4. A balancing algorithm and a list of slave servers are decided on this basis
  5. The list of servers is passed to a balancing function.
  6. A balancing function determines which server should be used.
  7. A new socket is created to connect to the slave server
  8. The socket is set to non-blocking mode
  9. If a connection cannot be established, another slave server is selected and tried to connect
  10. If all servers return an error and cannot be connected to, then an error is returned.
Algorithms for balancing :

Some of balancing algorithms used for this purpose are as follows :

  1. Naive – first alive server from server
  2. First alive – first alive server from 0
  3. Round Robin
    1. with locking – uses mutexes
    2. without locking – does not use mutexes
  4. Least Connections
Some developments in future : Providing cache for reverse proxy plugin

Thanks to Sonny Karlsson, a developer of Monkey httpd server for the idea. He proposed the idea of developing a cache for reverse proxy plugin in the monkey server.

Exception case:
When Monkey server becomes one of the slave servers for reverse proxy plugin there is the possibility of a recursion happening. The following diagrams would show a normal case and the exceptional case. For this reason, it has to be identified if a request is coming from a client or a reverse proxy plugin. If it is found that the resource is not present in the cache, and the slave server to which the request is going to be redirected is Monkey server itself, this would be like an ordinary incoming request to the server, which would be processed and looked up the cache, to see if the resource is present. This will happen indefinitely. Thus, if the resource requested by a request from the proxy reverse plugin is not present in the cache and the slave server to which the request is going to be directed to is found to be Monkey, immediately the next slave server in the list is chosen and the request is sent to this server. The following diagram will show you a workflow of entire system.

Caching the reverse proxy plugin requests
Caching the reverse proxy plugin requests
References :
  1. Picture courtesy :

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s