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 :
- Helps clients access servers that are configured beyond a firewall. Only the IP address of the proxy is publicized the outer world.
- Helps balance loads among different backend servers. It helps choose which server should take the load of the incoming request from the client.
- If one of the servers fails to deliver the content of the client request, the request can be assigned to another server.
- 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.
This is a brief workflow and where the reverse proxy plugin fits in :
- Incoming requests from clients
- The request URI is processed to check if it satisfies the regex rules
- Checked if a match is found on the basis of the previous search
- A balancing algorithm and a list of slave servers are decided on this basis
- The list of servers is passed to a balancing function.
- A balancing function determines which server should be used.
- A new socket is created to connect to the slave server
- The socket is set to non-blocking mode
- If a connection cannot be established, another slave server is selected and tried to connect
- 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 :
- Naive – first alive server from server
- First alive – first alive server from 0
- Round Robin
- with locking – uses mutexes
- without locking – does not use mutexes
- 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.
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.
- Picture courtesy : http://www.excitingip.com/1783/what-is-a-reverse-proxy-server/