0Pricing
Django Academy · 课时

使用 Nginx 作为反向代理

置于 Gunicorn 前端并提供静态文件

使用 Nginx 作为反向代理 是 CoddyKit 上的免费 Django Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Django Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Django Academy 课程共包含 4 节课。

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

Why Add Nginx

Gunicorn runs your code, but it should not face the raw internet alone. A reverse proxy sits in front and shields it. 🛡️

What a Reverse Proxy Does

A reverse proxy receives every browser request, then forwards it to Gunicorn and relays the answer back. The client never talks to Gunicorn directly.

Nginx Is Fast at Files

Nginx excels at serving static files and handling thousands of connections, freeing Gunicorn to focus only on dynamic Python work.

The Two-Layer Stack

The flow becomes browser to Nginx to Gunicorn to Django. Each layer does what it is best at, giving you speed and safety together.

A Server Block

Nginx config lives in a server block. It listens on a port and names the domain it answers for.

server {
    listen 80;
    server_name example.com;
}

Proxy to Gunicorn

Inside a location, proxy_pass tells Nginx where to send dynamic requests, usually Gunicorn on a local port or socket.

location / {
    proxy_pass http://127.0.0.1:8000;
}

Pass Real Headers

Add proxy_set_header lines so Django sees the true host and client IP, not just Nginx's local address.

proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;

Serve Static Directly

Map a location for /static/ to your collected files. Nginx serves them straight from disk, skipping Gunicorn entirely for speed.

location /static/ {
    alias /app/staticfiles/;
}

Where to Connect

Nginx can reach Gunicorn over a TCP port or a faster Unix socket. Sockets avoid network overhead on the same machine.

Test Before Reload

Always check your config with nginx -t before reloading. A bad file caught here saves you from taking the site offline.

nginx -t
systemctl reload nginx

TLS Lives Here Too

Nginx is also the natural place to terminate HTTPS, decrypting traffic once before passing plain requests on to Gunicorn.

Quick Check

Pick the directive that forwards requests to Gunicorn.

Recap

You set up Nginx as a reverse proxy: it fronts Gunicorn with proxy_pass, serves static files fast, forwards headers, and handles HTTPS. 🎯

常见问题解答

「使用 Nginx 作为反向代理」课时是免费的吗?

是的 — 「使用 Nginx 作为反向代理」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Django Academy 课程的其余内容,请升级到 CoddyKit PRO。 Django Academy 课程共包含 4 节课。

「使用 Nginx 作为反向代理」这节课中我会学到什么?

置于 Gunicorn 前端并提供静态文件 你通过在浏览器中直接运行的动手代码来练习 Django Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Django Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Django Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「使用 Nginx 作为反向代理」课时需要多长时间?

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

我能在这节 Django Academy 课中编写并运行代码吗?

能。每节 Django Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 使用 Gunicorn 作为 WSGI 服务器
  2. 使用 Nginx 作为反向代理
  3. 使用 WhiteNoise 提供静态文件
  4. 环境变量与配置拆分
← 返回 Django Academy