Nginx

High-performance web server and reverse proxy - load balancing, SSL termination, and static file serving

TL;DR

What: High-performance web server, reverse proxy, and load balancer.

Why: Serves static files fast, handles high concurrency, perfect for API proxying.

Quick Start

Install:

# macOS
brew install nginx

# Ubuntu/Debian
sudo apt install nginx

# CentOS/RHEL
sudo yum install nginx

Start/Stop:

# Start
sudo nginx

# Stop
sudo nginx -s stop

# Reload config (no downtime)
sudo nginx -s reload

# Test config
sudo nginx -t

Config location:

  • macOS: /usr/local/etc/nginx/nginx.conf
  • Linux: /etc/nginx/nginx.conf

Serve static files:

# /etc/nginx/sites-available/mysite
server {
    listen 80;
    server_name example.com;
    root /var/www/html;
    index index.html;
}

Cheatsheet

ConfigDescription
listen 80;Listen on port 80
server_name domain.com;Virtual host
root /path;Document root
index index.html;Default file
location /api { }URL matching
proxy_pass http://localhost:3000;Reverse proxy
try_files $uri $uri/ =404;SPA fallback

Reverse proxy:

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

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

Gotchas

403 Forbidden

# Check file permissions
ls -la /var/www/html

# Fix ownership
sudo chown -R www-data:www-data /var/www/html

# Check SELinux (CentOS)
sudo setenforce 0  # Temporary disable

Config syntax error

# Always test before reload
sudo nginx -t

# Check error log
tail -f /var/log/nginx/error.log

Port 80 already in use

# Find what's using port 80
sudo lsof -i :80

# Kill the process or use different port
listen 8080;

Enable site config

# Linux: Create symlink
sudo ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/
sudo nginx -s reload

Next Steps