Introduction
Nginx is a high-performance web server and reverse proxy server widely used for serving dynamic web content, load balancing, and handling HTTP requests. This guide will walk you through the basic configuration of Nginx and highlight some of its key features.
Installation
Before configuring Nginx, you need to install it. Here’s how you can do it on various operating systems.
On Ubuntu/Debian:
sudo apt update
sudo apt install nginx
On CentOS/RHEL:
sudo yum install epel-release
sudo yum install nginx
On Fedora:
sudo dnf install nginx
Starting Nginx
Once installed, you can start Nginx using the following command:
sudo systemctl start nginx
To enable Nginx to start on boot:
sudo systemctl enable nginx
Basic Configuration
Nginx’s main configuration file is located at /etc/nginx/nginx.conf
. Additionally, you can create separate configuration files for different sites in the /etc/nginx/sites-available
directory and create symlinks to them in the /etc/nginx/sites-enabled
directory.
- Main Configuration File:
/etc/nginx/nginx.conf
This file contains the global settings for Nginx. Here’s a basic configuration:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 1024;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
- Server Block Configuration:
Server blocks are similar to virtual hosts in Apache. Create a new server block configuration file in /etc/nginx/sites-available
:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
location = /404.html {
internal;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
internal;
}
}
Enable the server block by creating a symbolic link to it in the sites-enabled
directory:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
Test the configuration for syntax errors:
sudo nginx -t
Reload Nginx to apply the changes:
sudo systemctl reload nginx
Key Features of Nginx
- Reverse Proxy: Nginx can act as a reverse proxy, distributing incoming traffic across multiple servers. This is useful for load balancing and improving website performance.
upstream backend {
server backend1.example.com;
server backend2.example.com;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Load Balancing: Nginx supports several load balancing methods, including round-robin, least connections, and IP hash.
upstream backend {
least_conn;
server backend1.example.com;
server backend2.example.com;
}
Caching: Nginx can cache responses from a reverse proxy to reduce load on upstream servers.
proxy_cache_path /data/nginx/cache
levels=1:2
keys_zone=my_cache:10m
max_size=1g
inactive=60m
use_temp_path=off;
server {
location / {
proxy_pass http://backend;
proxy_cache my_cache;
proxy_cache_bypass $http_cache_control;
proxy_cache_valid any 1m;
}
}
Security: Nginx provides various security features such as limiting the rate of requests, blocking certain IP addresses, and enforcing HTTPS.
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/ssl/certs/example.com.crt;
ssl_certificate_key /etc/ssl/private/example.com.key;
location / {
limit_req zone=one burst=5;
proxy_pass http://backend;
}
limit_req_zone $binary_remote_addr
zone=one:10m rate=1r/s;
- Static Content Serving: Nginx is highly efficient at serving static content like HTML, CSS, and JavaScript files.
server { listen 80;
server_name example.com;
location / {
root /var/www/example.com/html;
try_files $uri $uri/ =404;
}
}
Conclusion
Nginx is a powerful and flexible web server that can handle a variety of tasks from serving static content to acting as a reverse proxy and load balancer. By understanding and leveraging its features, you can ensure that your web applications are fast, secure, and scalable.