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

使用安全标头强化 Nginx

在 Nginx 中添加 HTTP 安全标头,防御点击劫持、MIME 嗅探和内容注入攻击。

第 4 / 4 课13 个步骤

使用安全标头强化 Nginx 是 CoddyKit 上的免费 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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.

免费开始

用 AI 导师学习 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「使用安全标头强化 Nginx」课时是免费的吗?

是的 — 「使用安全标头强化 Nginx」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课程的其余内容,请升级到 CoddyKit PRO。 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课程共包含 4 节课。

「使用安全标头强化 Nginx」这节课中我会学到什么?

在 Nginx 中添加 HTTP 安全标头,防御点击劫持、MIME 嗅探和内容注入攻击。 你通过在浏览器中直接运行的动手代码来练习 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「使用安全标头强化 Nginx」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课中编写并运行代码吗?

能。每节 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 使用 SSL/TLS 保护 Nginx
  2. HTTP/2 与 Nginx 优化
  3. 基本身份验证与访问控制
  4. 使用安全标头强化 Nginx
← 返回 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)