Skip to content
seed

Using Nginx & Traeffic

Note

Reverse proxies are lightweight servers responsible for, SSL termination, encrypting responses, compression, and for returning standard server access error code, e.g. 503.

Nginx is a (reverse) HTTP proxy, also know as a router-- it forwards traffic from one standard port (typicall TCP 80/443) to an arbitrary number of listening ports running on the same or different hosts. Traeffic is a younger alternative to Nginx; although it runs just slightly slower, it is easier to setup, maintain, and has internal analytics.

Imagine you have a simple service architecture as shown below

Here

  • Redis stores all indices and calcuated values as key-value pairs
  • Postgres stores permament list of indicies that have been received
  • Worker watches redis for new indices, pulls each new index, calculates new values, and puts them back into redis

Purpose

The purpose of Nginx server (for a development environment) is to look at all the different requests and routes requests to the appropriate server, e.g., React or express.

Nginx will look at the request path, if it has /api in the request path, it will route it to the express app, if not, it routes it to the React server.

Nginx Routing

nginx/default.conf

upstream client {
  server client:3000;
}

upstream api {
  server api:5000;
}

server {}
  listen 80;
  server_name localhost;
  location / {
    proxy_pass http://client;
  }
  location /api {
    rewrite /api/(.*) /$1 break; # remove /api from the path
    proxy_pass http://api;
  }
}
upstream client {
  server client:3000;
}

upstream api {
  server api:5000;
}

server {}
  listen 80;
  server_name localhost;
  location / {
    proxy_pass http://client;
  }
  location /api {
    rewrite /api/(.*) /$1 break; # remove /api from the path
    proxy_pass http://api;
  }
}

Resources