API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) · Lezione

Rafforzare Nginx con gli header di sicurezza

Aggiunga header HTTP di sicurezza in Nginx per difendersi da clickjacking, MIME sniffing e attacchi di content injection.

Lezione 4 di 413 passaggi

Rafforzare Nginx con gli header di sicurezza è una lezione API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway), e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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.com

Quick 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:

  • nosniff blocks MIME confusion
  • X-Frame-Options stops clickjacking
  • HSTS enforces HTTPS, CSP restricts resources
  • Use always and beware add_header inheritance
  • server_tokens off hides the version

These complement TLS and authentication for defense in depth.

Gratis per iniziare

Impara API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) con un tutor IA — gratis

Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.

Corsi
12
Lezioni
48

Domande Frequenti

La lezione «Rafforzare Nginx con gli header di sicurezza» è gratuita?

Sì — il testo completo di «Rafforzare Nginx con gli header di sicurezza» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway), passa a CoddyKit PRO. Il corso API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) include 4 lezioni in totale.

Cosa imparerò in «Rafforzare Nginx con gli header di sicurezza»?

Aggiunga header HTTP di sicurezza in Nginx per difendersi da clickjacking, MIME sniffing e attacchi di content injection. Eserciti API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)?

Non è richiesta alcuna esperienza precedente. API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Rafforzare Nginx con gli header di sicurezza»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)?

Sì. Ogni lezione API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Protezione di Nginx con SSL/TLS
  2. HTTP/2 e ottimizzazione di Nginx
  3. Autenticazione di base e controllo degli accessi
  4. Rafforzare Nginx con gli header di sicurezza
← Torna a API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)