AEM Forward and Reverse Proxy

Many of us are confused over proxy and its variations as forward and reverse proxy. As part of this blog, we will discuss everything about proxy and its variations forward proxy and reverse proxy, with an example.

Proxy is all about having the authority to represent someone else. For example, in the class room, false attendance being awarded to a student by means of his /her friends or batchmates.

Before moving towards proxy and having a detailed discussion. Let’s first try to understand the communication mechanism between the client and server to fetch the data.

Client Server Architecture

Let’s first try to understand overall client and server architecture. As part of this architecture client will send a request to server and server will process the request to send data back to client.

Below is the over all flow in between client and server to fetch data:

  • The request will get originate from web browser and use HTTP or HTTPS protocol.
  • Request will reach out to server.
  • Server will process the request and send data back to client in predefined format as JSON, XML, binary etc.

Proxy

Now, lets try to understand what proxy is ?

Below is the best example of a proxy server that lies between the client and the actual server. In the case of a proxy server, both client and server will never know each other.

Let’s discuss forward and reverse proxy in detail:

Forward Proxy

For the server, the proxy server is the one who sent that request in place of the client to collect data. In this case, the server will never know who the actual client is who sent the request and is looking for a response. This mechanism is called forward proxy.

For example, if the client 1 is looking for a list of users, In this case, the proxy server will send the same request to the specific server responsible for the list of users. In this case, the server will never know which client requested a list of users because the request is coming from a proxy server.

Reverse Proxy

Client or browser, the proxy server is the one who is returning the response. In this case, the client will never know the actual server that returned the response. This mechanism is called reverse proxy.

Let’s try to understand everything in detail. As part of reverse proxy, clients will always use a single domain, such as abc.com, to get data related to users, products, customers, etc. In this case, the client will assume the proxy server is the one that is processing and returning all results. But, in actuality, we have a number of servers behind a proxy server, which is responsible for processing requests and returning the end result.

Here, the main role is played by the proxy server for sending requests to a specific server to fetch data.

For example, a client will hit the URL abc.com/products to fetch a list of products. In this case, requests will first reach the proxy server. The proxy server will internally call the actual abcproducts.com server with a URI of /products to fetch a list of products.

Setup Reverse Proxy as part of AEM Dispatcher

Reverse proxy implementation require below steps to be followed:

  1. Create Servlet to test reverse proxy (This step is optional to test reverse proxy in local).
  2. Create host entry.
  3. reverse proxy implementation.

1. Create Servlet

Follow below steps to create servlet:

  1. Create TestCacheServletByPath.java servlet having doGet() method returning hardcoded JSON response as {“number”:24} when we hit http://practice.abc/bin/practice/path.json URL.
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.json.JSONException;
import org.json.JSONObject;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.Servlet;
import java.io.IOException;
import java.util.Random;

@Component(
service=Servlet.class,
property={
Constants.SERVICE_DESCRIPTION + "=Test Cache Servlet",
"sling.servlet.methods=" + HttpConstants.METHOD_GET,
"sling.servlet.paths=" + "/bin/practice/path",
Constants.EXTENSION_DIRECTIVE + "=json"
}
)
public class TestCacheServletByPath extends SlingAllMethodsServlet {
private static final long serialVersionUID = 1L;
private final Logger logger = LoggerFactory.getLogger(getClass());

@Override
protected void doGet(final SlingHttpServletRequest req,
final SlingHttpServletResponse resp) throws IOException {

resp.setStatus(SlingHttpServletResponse.SC_OK);
resp.setContentType("application/json;charset=UTF-8");

JSONObject json = new JSONObject();
try {
json.put("number", 24);
} catch (JSONException e) {
logger.error("Exception >>>>> ", e);
}
resp.getWriter().print(json);
}
}

3. As normal servlet call having URL http://practice.abc/bin/practice/path.json will call servlet and return below output.

2. Add domain Name as part of host file

Follow below steps to add domain name as practice.abc in local windows host file:

  1. Traverse to below folder hierarchy: c:\Windows\System32\Drivers\etc\hosts

2. Open hosts file and make below highlighted entry in the file:

3. Reverse Proxy Implementation

  1. Make below host entry as part of
  2. In httpd.conf file, uncomment below lines of code to enable Enable the mod_proxy and mod_proxy_http modules for proxy:
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_module modules/mod_proxy.so

3. Add below lines of code to have reverse proxy within <VirtualHost> tag.

Configure Apache ProxyPass and ProxyPassReverse in httpd.conf file. ProxyPassReverse handle incoming request and then forwards the request.

ProxyRequests off
ProxyPreserveHost On
<Location /proxycall.json>
ProxyPass "http://practice.abc/bin/practice/path.json"
ProxyPassReverse "http://practice.abc/bin/practice/path.json"
Order allow,deny
Allow from all
</Location>

In case of reverse proxy setup, http://practice.abc/proxycall.json proxy URL will internally call actual /bin/practice/path.json servlet.

OUTPUT:

Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.

0