Nginx Explained for Developers

Nginx Explained for Developers

 

Nginx Explained for Developers

Modern web applications such as Laravel, Node.js, React, and APIs need a fast and reliable web server. One of the most popular tools developers use today is Nginx.

In this guide, you will learn what Nginx is, how it works, and how developers use it in real projects.

1. What is Nginx?

Nginx is a high-performance web server, reverse proxy, and load balancer.

It was created to solve the C10K problem (handling 10,000+ simultaneous connections).

Developers use Nginx to:

  • Serve websites

  • Handle static files

  • Act as a reverse proxy

  • Balance traffic between servers

  • Improve application performance

Many large companies use Nginx including:

  • Netflix

  • Airbnb

  • WordPress

  • GitHub

2. Why Developers Use Nginx

Nginx is popular because it is:

1️⃣ Very Fast

It uses an event-driven architecture, meaning it can handle thousands of requests efficiently.

2️⃣ Low Memory Usage

Unlike traditional web servers, Nginx uses fewer system resources.

3️⃣ Great for Modern Applications

Nginx works perfectly with:

  • Laravel

  • Node.js

  • Docker

  • Kubernetes

3. How Nginx Works

When a user visits a website:

Browser → Nginx → Application Server → Database

Example flow:

  1. User requests a website

  2. Nginx receives the request

  3. Nginx decides what to do:

    • Serve static files

    • Forward request to backend

  4. Backend returns response

  5. Nginx sends response to user

Example:

User → Nginx → Laravel App → MySQL → Response

Nginx acts as a traffic controller for your application.

4. Nginx Architecture

Nginx uses a master-worker architecture.

Master Process

Responsible for:

  • Reading configuration files

  • Managing worker processes

  • Restarting workers if needed

Worker Processes

Workers handle:

  • Incoming requests

  • Client connections

  • Request processing

Example architecture:

Master Process
|
-----------------------
| | |
Worker1 Worker2 Worker3

Each worker can handle thousands of connections simultaneously.

5. Installing Nginx

Ubuntu / Linux

sudo apt update
sudo apt install nginx

Start Nginx

sudo systemctl start nginx

Enable auto start

sudo systemctl enable nginx

Check status

sudo systemctl status nginx

6. Basic Nginx Configuration

The main configuration file:

/etc/nginx/nginx.conf

Websites are usually configured in:

/etc/nginx/sites-available/

Example basic configuration:

server {
listen 80;
server_name example.com;

root /var/www/html;
index index.html index.php;

location / {
try_files $uri $uri/ =404;
}
}

Explanation:

DirectivePurpose
listenPort to listen on
server_nameDomain name
rootWebsite directory
indexDefault file

7. Serving Static Files

Nginx is extremely fast at serving static files such as:

  • CSS

  • JavaScript

  • Images

  • Videos

Example:

location /assets/ {
root /var/www/project;
}

Request:

example.com/assets/style.css

Nginx serves the file directly without hitting the application.

This improves performance dramatically.

8. Nginx as a Reverse Proxy

Nginx often sits in front of your application server.

Example with Node.js:

server {
listen 80;

location / {
proxy_pass http://localhost:3000;
}
}

Request flow:

User → Nginx → Node.js App

Nginx forwards the request to the backend server.

9. Using Nginx with Laravel

Developers commonly run Laravel with PHP-FPM.

Example configuration:

server {
listen 80;
server_name laravel-app.com;

root /var/www/laravel/public;
index index.php;

location / {
try_files $uri $uri/ /index.php?$query_string;
}

location ~ \.php$ {
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}

Components involved:

ComponentRole
NginxWeb server
PHP-FPMExecutes PHP
LaravelApplication logic
MySQLDatabase

10. Load Balancing with Nginx

Nginx can distribute traffic across multiple servers.

Example:

upstream backend {
server 192.168.1.10;
server 192.168.1.11;
}

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

Request distribution:

User Request
|
Nginx
/ \
Server1 Server2

Benefits:

  • High availability

  • Better performance

  • Scalability

11. Nginx Caching

Nginx can cache responses to speed up websites.

Example:

proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=my_cache:10m;

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

Benefits:

  • Faster responses

  • Reduced server load

  • Improved user experience

12. Security Features

Nginx helps secure your application.

Examples:

Rate Limiting

limit_req_zone $binary_remote_addr zone=api_limit:10m rate=10r/s;

Prevents API abuse and DDoS attacks.

Hide Server Information

server_tokens off;

Prevents attackers from knowing server details.

13. Nginx vs Apache

Developers often compare Nginx with:

Apache HTTP Server

FeatureNginxApache
ArchitectureEvent-drivenProcess-based
PerformanceVery highGood
Static filesExcellentGood
Dynamic appsNeeds PHP-FPMBuilt-in modules

Many production systems use:

Nginx + PHP-FPM

instead of Apache.

14. Real-World Architecture Example

Typical production stack:

User
|
Internet
|
Nginx
|
Application Server
|
Database

Example for Laravel:

Browser
|
Nginx
|
PHP-FPM
|
Laravel
|
MySQL

15. When Developers Use Nginx

Nginx is used when you need:

  • High-performance web server

  • Reverse proxy

  • Load balancing

  • Static file delivery

  • API gateway

  • SSL termination

Conclusion

Nginx is one of the most important tools in modern backend development.

It provides:

  • High performance

  • Scalability

  • Security

  • Efficient traffic handling

That is why many production systems use:

Nginx + Application Server + Database

to build fast and scalable web applications.

Souy Soeng

Souy Soeng

Hi there 👋, I’m Soeng Souy (StarCode Kh)
-------------------------------------------
🌱 I’m currently creating a sample Laravel and React Vue Livewire
👯 I’m looking to collaborate on open-source PHP & JavaScript projects
💬 Ask me about Laravel, MySQL, or Flutter
⚡ Fun fact: I love turning ☕️ into code!

Post a Comment

CAN FEEDBACK
close