Mastering Nginx: A Comprehensive Guide

Eric Okemwa
3 min readDec 27, 2023

--

Illustration by Laith Academy

Nginx, a powerful and versatile web server, has become a cornerstone of modern web infrastructure. In this guide, we will explore various use cases for Nginx, ranging from serving static files to load balancing across multiple servers.

1. Using Nginx as a Reverse Proxy

One of Nginx’s key strengths is its ability to act as a reverse proxy, forwarding client requests to backend servers. Below is a basic example of a reverse proxy configuration in Nginx:

server {
listen 80;
server_name example.com;

location / {
proxy_pass http://backend_server;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}

Here, Nginx listens on port 80 and forwards requests to a backend server.

2. Serving Static HTML and CSS Files

Nginx is renowned for its efficiency in serving static content. Below is a sample configuration for serving HTML and CSS files:

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

location / {
root /path/to/static/files;
index index.html;
}
}

This configuration serves static files from the specified directory.

3. Understanding MIME Types and Location Context

Nginx allows fine-grained control over MIME types and requests URIs through the location block. Here's an example:

types {
text/css css;
application/javascript js;
}

server {
listen 80;
server_name example.com;

location ~ \.css$ {
# Handle CSS files
root /path/to/css/files;
}

location ~ \.js$ {
# Handle JavaScript files
root /path/to/js/files;
}
}

In this example, MIME types are explicitly defined, and the location block directs requests based on file extensions.

4. Using Alias Instead of Root

The alias directive in Nginx allows you to specify an alternative location for requested resources. Here's a comparison between using root and alias:

# Using root
location /app/ {
root /path/to/app;
}

# Using alias
location /app/ {
alias /path/to/app/;
}

The alias directive provides more flexibility, especially when dealing with non-trivial directory structures.

5. The Power of the Location Block

Nginx’s location block is a versatile tool for directing traffic. Here's an example:

location ~* \.(jpg|jpeg|png|gif)$ {
root /path/to/images;
expires 30d;
add_header Cache-Control "public, max-age=2592000";
}

This configuration serves image files with caching headers, improving performance.

6. Configuring a Docker Container for a Node.js Server

Docker simplifies the deployment process. Below is a basic Dockerfile for a Node.js server:

FROM node:14

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD ["npm", "start"]

This Dockerfile sets up a Node.js environment and copies the application files.

7. Load Balancing Across Multiple Servers

Nginx excels at load balancing, enhancing scalability and reliability. Here’s a simple load balancing configuration:

http {
upstream backend {
server backend1.example.com;
server backend2.example.com;
server backend3.example.com;
}

server {
listen 80;
server_name example.com;

location / {
proxy_pass http://backend;
}
}
}

In this example, Nginx distributes incoming requests across multiple backend servers.

With these examples, you’ve gained practical insights into various aspects of Nginx. Experiment with these configurations in your projects to deepen your understanding and optimize your web infrastructure. Happy coding!

--

--

Eric Okemwa
Eric Okemwa

Written by Eric Okemwa

Let’s connect and make the complex simple!

No responses yet