← All Writings
October 2, 2024 · 4 min read

NGINX | Reverse Proxy Server

Many tech giants use the reverse proxy server and load balancer to make the server secure and distribute the user requests evenly on each server. It is also used for caching, SSL handshakes and monitoring the incoming requests.

About NGINX

usage of NGINX

  • Load balancer
  • Data caching
  • SSL handshaking
  • Reverse proxy server

To download the nginx

for mac:

brew install nginx

for linux:

sudo apt install nginx

Default nginx working dir /etc/nginx

To start or stop NGINX

  • sudo systemctl start nginx or nginx (max) or sudo nginx (linux) - for starting nginx server (same goes for docker) or try running
  • sudo systemctl stop nginx - for stopping nginx server

After starting nxginx, visit http://localhost:80 it displays whether the nginx is running or not

Nginx terminologies

  • All the nginx configuration of nginx is defined inside nginx.conf file inside /etc/nginx dir
  • nginx.conf file contains two major things:
    • Directive (key value pair ‘name val’)
    • Context (block within curly braces ‘{}’)
    • e.g. http{ # context start sendfile on; # directive } # context ends

Using nginx to serve static html file

http {
    # server configuration
    server {
        listen 8080; # port number on which we want to listen the incoming request
        root /path/from/where/we/want/to/serve/content; # it looks for index.html and serve the file
    }
}
# required for nginx config
events{}

After changing configuration we need to reload the nginx server. To reload the nginx server use following command

nginx -s reload - reloads the nginx server

MIME types

it is used to define different of content types which is going to be served by the nginx

http {
    types {
        # type   extension
        text/css css;
        text/html html;
    }
    server {
        ...
    }
}

nginx provide us predefined MIME types in /etc/nginx dir inside the file names mime.types

NGINX Dir

To include all the mime types inside the mime.types file we can use the include directive

http {
    include mime.types;
    server {
        ...
    }
}

Location Block

It is used to define specific endpoints from which we can serve different files

  • /about → about/index.html
  • /contact → contact/index.html

create the about and contact directory in the root dir of project and create index.html file inside both the folder

server {
    root /path/to/files;
    location /about {
        root /path/to/files; # it appends /about at the end of the string implictly
    }
    location /contact {
        root /path/to/files;
    }
}

create the alias for different name but same file

location /me {
    alias /path/to/files/about;
}

Using placeholder files

location /another_loc {
    root /path/to/project;
    try_files /another_loc/myName.html /index.html =404;
    #directive file name apart from index.html fallback file status code
}

Redirection

location /false_loc {
    return 307 /true_loc;
    # redirect code path
}

Forwarding the existing server

nginx is distributing the requests on multiple server simultaneously

forwarding the existing localhost server using nginx where nginx works as the reverse proxy (middleman)

http {
    location / {
        proxy_pass http://localhost:3000 # typical react or next js app
    }
}

NGINX as load balancer

Start the multiple container of your favorite servers using single docker image which runs on different ports. Here we have used bunjs server which runs on port 3000,3001,3002 and 3003 respectively.

ConainerList

we have to create the backend context in nginx.conf file using upstream context and inside we can define server directive which contains all the server ports

upstream balancer {
    server 127.0.0.1:3000;
    server 127.0.0.1:3001;
    server 127.0.0.1:3002;
    server 127.0.0.1:3003;
}
server {
    ...
}

Redirect the reverse proxy to the servers using proxy_pass directive

server {
    location / {
        proxy_pass http://balancer/;
    }
}

by default nginx round robins on each server one after another leads to equal number of requests on each servers

After starting the nginx server try reloading page everytime and see the docker stats

ContainerList 1

Container List 2


That's all you need to know about NGINX it is great sorfware which is used by many tech companies out there.