What is a proxy?

In its simplest form, a proxy is a server that listens for requests and redirects them to other servers. This allows multiple servers to run on the same system but still receive their requests, such as the case with many large hosting servers today. Requests are redirected depending on how they were made to the machine such as the URL that was used.

Proxies can also provide other benefits such as distributing traffic among multiple instances of the same web server, formatting the requests such as adding additional headers, filtering requests, or adding an additional security layer.

What is NGINX

NGINX is a popular web server that is commonly used for proxying requests. It was designed for performance and stability. It can perform many tasks efficiently such as being a web server itself for static content, load balancing, content caching, SSL/TLS terminator, or proxying requests.

Configuring NGINX

NGINX configuration can get complex because of all of the features available. The following is a basic setup for getting started with. This configuration will listen for requests being made on port 80 with a specific URL and forward them on to a server listening on port 8080.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10

server {
listen 80;
server_name example.com www.example.com;

    location / {
       proxy_pass http://127.0.0.1:8080;
    }

}

Next we will want to get NGINX running on our local server. This is easily done using docker compose and an existing image. The image expects the configuration file created in the previous step to be placed in the templates folder in the same directory.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
version: "3.9"
services:
  nginx:
    container_name: nginx
    restart: unless-stopped
    image: "nginx:latest"
    environment:
      - NGINX_HOST=example.com
      - NGINX_PORT=80
    volumes:
      - ${PWD}/templates:/etc/nginx/templates
    network_mode: "host"

Checking configuration

You can check for proper configuration by running a curl command on your local machine and verifying that a proper response is received.

1
2
$ curl example.com
<!DOCTYPE html><html><head><meta http-equiv="Content-type" content="text/html; charset=utf-8"><title>Chronograf</title><link rel="icon shortcut" href="/favicon.fa749080.ico"><link rel="stylesheet" href="/src.14d28054.css"></head><body> <div id="react-root" data-basepath=""></div> <script src="/src.bb2cd140.js"></script> </body></html>