Refuerzo de Nginx con encabezados de seguridad
Añada encabezados de seguridad HTTP en Nginx para protegerse contra ataques de clickjacking, detección de tipos MIME e inyección de contenido.
Refuerzo de Nginx con encabezados de seguridad es una lección gratuita de API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) en CoddyKit. Esta es la lección 4 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway), y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
Headers as a Defense Layer
Beyond TLS and authentication, modern browsers honor security headers that instruct them how to behave. Nginx can inject these on every response with the add_header directive.
Preventing MIME Sniffing
X-Content-Type-Options: nosniff stops browsers from guessing a resource's type, blocking attacks that disguise a script as an image.
add_header X-Content-Type-Options "nosniff" always;Blocking Clickjacking
X-Frame-Options controls whether your site can be embedded in a frame. Use DENY or SAMEORIGIN to prevent clickjacking.
add_header X-Frame-Options "SAMEORIGIN" always;Strict Transport Security
HSTS forces browsers to use HTTPS for future visits. Set a long max-age once HTTPS is stable everywhere.
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;Content Security Policy
A Content-Security-Policy restricts where scripts, styles, and other resources may load from, mitigating cross-site scripting.
add_header Content-Security-Policy "default-src 'self'" always;Controlling the Referrer
Referrer-Policy limits how much referrer information leaks to other sites when users click outbound links.
add_header Referrer-Policy "strict-origin-when-cross-origin" always;Why the always Flag
Without always, Nginx adds the header only on successful responses (2xx, 3xx). The always flag ensures the header is present on error responses too.
add_header X-Frame-Options "DENY" always;Hiding the Nginx Version
By default Nginx reveals its version in the Server header and error pages. Turn this off to give attackers less information.
server_tokens off;The add_header Inheritance Trap
If a location block has its own add_header, it replaces all inherited headers from the parent. Re-declare needed headers in nested blocks.
# headers in http/server are dropped here
location /api {
add_header X-Content-Type-Options "nosniff" always;
}Grouping Security Headers
Keep all security headers in one include file and pull it into each server block for consistency.
# security_headers.conf
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
# server block:
include /etc/nginx/security_headers.conf;Verifying Headers
Use curl to inspect the response headers and confirm each one is present, even on error responses.
curl -I https://example.comQuick Check
Which header tells the browser to refuse loading your site inside a frame on another domain?
Recap
You hardened Nginx with browser security headers:
nosniffblocks MIME confusionX-Frame-Optionsstops clickjacking- HSTS enforces HTTPS, CSP restricts resources
- Use
alwaysand bewareadd_headerinheritance server_tokens offhides the version
These complement TLS and authentication for defense in depth.
Preguntas frecuentes
¿La lección «Refuerzo de Nginx con encabezados de seguridad» es gratis?
Sí — el texto completo de «Refuerzo de Nginx con encabezados de seguridad» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway), actualiza a CoddyKit PRO. El curso de API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) incluye 4 lecciones en total.
¿Qué aprenderé en «Refuerzo de Nginx con encabezados de seguridad»?
Añada encabezados de seguridad HTTP en Nginx para protegerse contra ataques de clickjacking, detección de tipos MIME e inyección de contenido. Practicas API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)?
No se requiere experiencia previa. API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 4 de 4.
¿Cuánto tiempo toma la lección «Refuerzo de Nginx con encabezados de seguridad»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)?
Sí. Cada lección de API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Protección de Nginx con SSL/TLS
- HTTP/2 y optimización de Nginx
- Autenticación básica y control de acceso
- Refuerzo de Nginx con encabezados de seguridad