Laboratorio 8: Nginx - Reverse Proxy + TLS
Unidad: 10 - Nginx y SSL
Duración Estimada: 120-150 minutos
Dificultad: Intermedio
Objetivos
- Configurar un server block en Nginx
- Hacer reverse proxy hacia un servicio local
- Habilitar HTTPS con certificado self-signed (o Certbot si tienes dominio)
Requisitos Previos
- VM Ubuntu Server LTS (22.04 o 24.04) con acceso SSH
- Usuario con permisos sudo
Warning⚠️ ADVERTENCIA CRÍTICA
Si habilitas UFW sin permitir tu puerto SSH, puedes perder acceso.
Lo que podría salir mal: - Bloqueo de SSH remoto.
Cómo prevenirlo: 1. Verifica tu puerto SSH actual. 2. Agrega la regla de UFW para SSH antes de activar.
Pasos del Laboratorio
Paso 1: Levantar un backend local (15 min)
BASH
1$ python3 -m http.server 9000 --bind 127.0.0.1
Serving HTTP on 127.0.0.1 port 9000 (http://127.0.0.1:9000/) ...- 1
- python3 -m http.server simula un backend HTTP local (solo loopback).
Paso 2: Instalar Nginx y configurar reverse proxy (35 min)
BASH
1$ sudo apt update
2$ sudo apt install -y nginx
3$ sudo tee /etc/nginx/sites-available/rp.conf >/dev/null <<'EOF'
server {
listen 80;
server_name rp.local;
location / {
proxy_pass http://127.0.0.1:9000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
EOF
4$ sudo ln -sf /etc/nginx/sites-available/rp.conf /etc/nginx/sites-enabled/rp.conf
5$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
6$ sudo systemctl reload nginx
7$ curl -i -H 'Host: rp.local' http://127.0.0.1/ | head -n 10
HTTP/1.1 200 OK
Server: nginx
...- 1
- apt update actualiza el indice.
- 2
- apt install nginx instala Nginx.
- 3
- tee crea la configuracion del reverse proxy.
- 4
- ln -sf habilita el sitio.
- 5
- nginx -t valida sintaxis.
- 6
- systemctl reload aplica cambios.
- 7
- curl -H Host prueba el server block.
Paso 3: TLS self-signed (35 min)
BASH
1$ sudo mkdir -p /etc/nginx/tls
$ sudo openssl req -x509 -nodes -newkey rsa:2048 -days 365 \
-keyout /etc/nginx/tls/rp.local.key \
-out /etc/nginx/tls/rp.local.crt \
2 -subj "/CN=rp.local"
3$ sudo tee /etc/nginx/sites-available/rp-tls.conf >/dev/null <<'EOF'
server {
listen 443 ssl;
server_name rp.local;
ssl_certificate /etc/nginx/tls/rp.local.crt;
ssl_certificate_key /etc/nginx/tls/rp.local.key;
location / {
proxy_pass http://127.0.0.1:9000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
EOF
4$ sudo ln -sf /etc/nginx/sites-available/rp-tls.conf /etc/nginx/sites-enabled/rp-tls.conf
5$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
6$ sudo systemctl reload nginx
7$ curl -k -I -H 'Host: rp.local' https://127.0.0.1/
HTTP/2 200
server: nginx- 1
- mkdir -p crea directorio para llaves/certs.
- 2
- openssl req -x509 genera certificado self-signed para pruebas.
- 3
- tee crea server block TLS.
- 4
- ln -sf habilita el sitio TLS.
- 5
- nginx -t valida.
- 6
- reload aplica.
- 7
- curl -k prueba HTTPS ignorando confianza (self-signed).
Paso 4 (opcional): Certbot si tienes dominio (20-40 min)
BASH
- 1
- apt install certbot instala Certbot.
- 2
- certbot –nginx emite certificado y configura TLS real.
- 3
- certbot renew –dry-run valida renovacion.
Entregables (Evidencia)
nginx -tysystemctl status nginx --no-pagercurl -i -H 'Host: rp.local' http://127.0.0.1/(reverse proxy)curl -k -I -H 'Host: rp.local' https://127.0.0.1/(TLS self-signed)