Bridging apps with Nginx

I was assigned a proof of concept (POC) for a new client. One of the features of the proposal was a tech stack migration in parallel with the ongoing development. In other words, we had to show how to change from tech stack A to tech stack B while the front end is also developed.

For a front end, a REST API is just an interface, and it does not matter in which stack, framework or language it is. As long as the data and the interface is the same, we can switch among different stacks and the front end should work in the same way. Think of this as a contract between the front and the back end.

However, we needed to move some endpoints from stack A to stack B and not just move all the tech stack in a single move. We needed a new layer in front of both stacks to route each incoming request. Nginx was an excellent choice for this task.

The POC had to show how the account creation could be migrated to the new stack. Stack A, the original, was running in port 3000 (a typical Ruby on Rails project), and stack B, the new stack, was running in port 9000 (Play Framework in Scala), the configuration for port 4000 in Nginx was similar to:

server {
    listen 4000;
    server_name localhost;
    location / {
        proxy_pass http://localhost:3000;
    location /api/accounts.json {
        if ($request_method = POST) {
            proxy_pass http://localhost:9000;
        proxy_pass http://localhost:3000;

We had to proxy only the POST request, and any other verb was to be handled in the legacy stack. Fortunately, both stacks communicated with a common web service running in other port where the data reside, and in consequence they didn't need local persistence. Thus the data was always the same and the contract for the front end held.

Addendum (for Mac users)

Nginx can be installed using Homebrew running brew install nginx and the configuration files resides in /usr/local/etc/nginx. We could have even use the local DNS file in /etc/hosts, add a server name:      localdev.local

and update the server name to localdev.local and the listen port to 80 in the nginx configuration and access the local development project with the URL http://localdev.local.