TL;DR
Qué: Servidor web de alto rendimiento, reverse proxy y balanceador de carga.
Por qué: Sirve archivos estáticos rápidamente, maneja alta concurrencia, perfecto para proxy de APIs.
Quick Start
Instalación:
# macOS
brew install nginx
# Ubuntu/Debian
sudo apt install nginx
# CentOS/RHEL
sudo yum install nginx
Iniciar/Detener:
# Start
sudo nginx
# Stop
sudo nginx -s stop
# Reload config (no downtime)
sudo nginx -s reload
# Test config
sudo nginx -t
Ubicación de config:
- macOS:
/usr/local/etc/nginx/nginx.conf - Linux:
/etc/nginx/nginx.conf
Servir archivos estáticos:
# /etc/nginx/sites-available/mysite
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.html;
}
Cheatsheet
| Config | Descripción |
|---|---|
listen 80; | Escuchar en puerto 80 |
server_name domain.com; | Virtual host |
root /path; | Raíz del documento |
index index.html; | Archivo por defecto |
location /api { } | Coincidencia de URL |
proxy_pass http://localhost:3000; | Reverse proxy |
try_files $uri $uri/ =404; | Fallback SPA |
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
- Nginx Official Docs - Documentación oficial
- Nginx Config Generator - Generador de configuración
- Let’s Encrypt SSL - Certificados SSL
- Nginx Amplify Monitoring - Monitoreo