ALRIGHT!
What is Nginx?
Nginx is a high-performance web server that
delivers websites, handles requests, and
can also work as a reverse proxy, load
balancer, and caching tool to manage and
scale web traffic efficiently.
[Link]
server
[Link]
server
[Link]
.com
mple
.exa
www
server
[Link]
.com
mple
.exa
www
server
[Link]
.com
mple
.exa
www
[Link]
server
[Link]
.com
mple
.exa
www
server
Practical
Prerequisites
AWS Account
We will test this on EC2 instance
Installing Nginx
yum install nginx
apt install nginx
Start Nginx service
systemctl start/stop/reload nginx
Enable HTTP service in firewalld
sudo firewall-cmd --permanent --add-service=http
Access Default website from browser
Nginx config
/etc/nginx/[Link]
Default webpage
/usr/share/nginx/html
Create our own static website
Configure nginx config
user;
events {
}
Nginx Config
/etc/nginx/[Link] http {
server {
location { }
}
}
High-Level Structure:
[Link] Block:
Applies settings globally (e.g., user, processes, logging).
[Link] Block:
Manages connection-level configurations (e.g., worker connections).
[Link] Block:
Contains configurations for handling HTTP requests:
MIME types, logging, and general settings.
Includes all server blocks.
[Link] Block(s):
Defines settings for handling requests for specific domains or IPs.
Can contain multiple location blocks.
[Link] Block(s):
Handles specific request URIs or patterns (e.g., /api/, *.php).
HTTP Status
Nginx Logging
/var/log/nginx/
By default, worker_connections is often set to 1024.
worker_processes 4 (for a 4-core CPU)
worker_connections 10,000
Total max connections = 4 × 10,000 = 40,000 connections
Note: If worker_processes = 4 and worker_connections = 10,000,
the nginx user must have a file descriptor limit of at least 40,000.
Hosting Multiple Websites
website1
website2
server
http {
server {
location { }
}
server {
location { }
}
}
Custom Domain
Register a domain name.
In DNS settings, create a A record and
point to public IP of our server (EC2)
In [Link], server block, use
server_name [Link];
HTTPS Setup
HTTPS is a secure version of HTTP that encrypts
data between your browser and a website,
making it safe from hackers.
It uses SSL/TLS to protect sensitive information
like passwords and credit card details.
For Mac
brew install certbot
For Windows
choco install certbot -y
For Linux
Centos
sudo yum install epel-release -y
sudo yum install certbot python3-certbot-nginx -y
Ubuntu
sudo apt install certbot python3-certbot-nginx -y
First stop the nginx (if listening on port 80) as below command
will use local port 80.
To generate certificates
sudo certbot certonly --standalone -d [Link]
Files will be generated in
/etc/letsencrypt/live/[Link]/
listen 443 ssl http2;
ssl_certificate /etc/letsencrypt/live/[Link]/[Link];
ssl_certificate_key /etc/letsencrypt/live/[Link]/[Link];
ssl_protocols TLSv1.2 TLSv1.3;
Reverse Proxy
An Nginx reverse proxy acts as an intermediary
between clients and backend servers.
It forwards client requests to the appropriate
server, handles responses, and provides benefits
like load balancing, caching, and security.
server {
listen 80;
server_name [Link];
location / {
proxy_pass [Link]
}
}
Userdata script to install and run Apache Webserver
#!/bin/bash
sudo yum update -y
# Install Apache web server (httpd)
sudo yum install -y httpd
sudo systemctl start httpd
sudo systemctl enable httpd
# Create a simple HTML file to verify the web server is running
echo "<html><h1>This is Website 1</h1></html>" > /var/www/html/[Link]
Load Balancing
Nginx load balancing is a feature where Nginx
distributes incoming traffic across multiple
backend servers to ensure no single server
gets overloaded, improving performance,
reliability, and scalability of your application.
http {
upstream backend_servers {
server [Link]:3000;
server [Link]:3001;
server [Link]:3002;
}
server {
listen 80;
server_name [Link];
location / {
proxy_pass [Link]
}
}
}
Different Modes of LB
Round-Robin (Default): Evenly distributes requests across servers; no
configuration needed.
Least Connections: Sends requests to the server with the fewest active
connections (least_conn).
IP Hash: Routes requests based on the client’s IP to ensure session
stickiness (ip_hash).
Weighted Round-Robin: Assigns weights to servers to handle traffic
proportionally (server [Link] weight=3;).
High Availability
Backup Server will only server in case of
Primary Fail
upstream backend {
server [Link]:3000;
server [Link]:3000;
server [Link]:3000 backup;
}
Nginx Timeouts
Nginx Catching
Nginx caching is a process where Nginx stores
copies of responses (like HTML, images, or API
data) to serve them directly to users, reducing
backend load and improving response times.
http {
# Define the cache path
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m
max_size=1g;
server {
listen 80;
server_name [Link];
location / {
proxy_cache my_cache; # Enable caching using the defined cache
proxy_cache_valid 200 60m; # Cache 200 OK responses for 60 minutes
proxy_cache_key "$scheme$request_uri"; # Define the cache key
proxy_pass [Link] # Forward requests to the backend server
add_header X-Cache-Status $upstream_cache_status; # Add cache status header for debugging
}
Protocols
Interview Questions
Basic Questions
1. What is Nginx, and how does it differ from Apache?
2. Explain the difference between Nginx as a web server and as
a reverse proxy.
3. What is the default configuration file for Nginx?
4. How do you enable HTTP/2 in Nginx?
5. How do you troubleshoot Nginx issues?
Intermediate Questions
What is the role of proxy_pass in Nginx?
How do you configure Nginx as a load balancer?
How does Nginx handle caching?
How do you set up SSL/TLS with Nginx?
How do you configure a custom 404 page?
How do you block specific IP addresses in Nginx?
NGINX-Based Scalable Architecture on AWS
Set up an NGINX web server on AWS EC2 to serve static and dynamic
content.
Configured NGINX as a reverse proxy to forward requests to
backend services.
Implemented load balancing across multiple EC2 instances to
handle high traffic efficiently.
Secured the application with SSL/TLS certificates (Let's Encrypt,
Certbot).
Managed DNS settings for a custom domain and ensured high
availability.