EADST

在 Django + uWSGI 架构中,如何让 Nginx 单独托管一个 HTML 页面

在使用 Django + uWSGI + Nginx 的架构时,默认所有请求都会被转发给 Django 处理。但在实际场景中,经常会遇到这种需求:

我只想单独暴露一个 HTML 页面(如 verification.html), 不想经过 Django,也不想改 uWSGI。

本文记录一次真实排错过程,重点讲清楚正确配置方式以及最容易踩的坑。


一、问题背景

  • 主站架构:Nginx → uWSGI → Django
  • Django 负责绝大多数请求
  • 目标 URL:

http://eadst.com/verification.html * 目标文件路径:

/home/www/blog/templates/verification.html * 要求:

  • 仅由 Nginx 静态返回
  • 不修改 Django / uWSGI
  • 不影响现有站点

二、核心原则(非常重要)

1️⃣ 使用 精确匹配

必须使用:

location = /verification.html

否则请求会被:

location / { uwsgi_pass ... }

抢走,进入 Django。


2️⃣ 避免 alias + try_files 混用(坑点)

这是这次 404 的根因。

很多人会写成这样(❌ 不推荐):

location = /verification.html {
    alias /home/www/blog/templates/verification.html;
    try_files /home/www/blog/templates/verification.html =404;
}

在 精确匹配 + 单文件场景 下,这种写法非常容易导致 Nginx 判定文件不存在,最终触发 error_page 404。


三、最终正确 & 稳定的配置方式(推荐)

✅ 使用 root + try_files(最稳)

直接把下面这段加到 对应 server 块中,并且放在 location / 前面:

location = /verification.html {
    root /home/www/blog/templates;
    try_files /verification.html =404;
    default_type text/html;
    access_log off;
}

路径映射逻辑

请求: /verification.html 实际文件: /home/www/blog/templates/verification.html


四、完整示例 server 配置(节选)

server {
    listen 80;
    server_name eadst.com www.eadst.com 100.xxx.xxx.xxx;

    access_log  /home/www/blog/script/access.log;
    error_log   /home/www/blog/script/error.log;

    charset utf-8;

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;

    # 单独托管 verification.html(不进 Django)
    location = /verification.html {
        root /home/www/blog/templates;
        try_files /verification.html =404;
        default_type text/html;
        access_log off;
    }

    location /static/ {
        alias /home/www/blog/static/;
    }

    location / {
        include uwsgi_params;
        uwsgi_pass 10.0.8.8:8888;
    }
}

五、验证是否生效(非常关键)

1️⃣ 重载配置

sudo nginx -t
sudo /etc/init.d/nginx reload

2️⃣ 本机 curl 验证(推荐)

curl -i http://127.0.0.1/verification.html -H "Host: eadst.com"

正确结果应该是:

  • HTTP/1.1 200 OK
  • Server: nginx
  • 返回 verification.html 内容
  • 不是 Django 的 404 页面

六、排错思路总结(非常实用)

如果你遇到类似 404,可以按这个顺序排:

  1. 是不是精确匹配 location =
  2. 配置文件是否真的被加载

nginx -T | grep verification 3. 是否命中了 Django 的 404 页面

  • 是 → location 没生效
  • 是否混用了 alias + try_files
  • 路径权限是否可达

namei -l /path/to/file.html


七、工程级建议(长期)

不建议把对外暴露的 HTML 放在 templates 目录

更合理的做法是:

  • 放到 /static/ 或 /var/www/html/
  • 由 Nginx 统一管理静态资源
  • Django templates 只给后端渲染用

八、总结一句话

在 Django + uWSGI 架构中, 想单独托管一个 HTML 页面, 用 location = + root + try_files, 简单、稳定、不踩坑。

如果你后面还想扩展成单独托管一个目录 / 验证文件 / SEO 校验页 / CDN 回源页,这套方法都能直接复用。

相关标签
About Me
XD
Goals determine what you are going to be.
Category
标签云
Jupyter Proxy Tiktoken Dataset 阿里云 Baidu DeepSeek Permission Django Base64 Git Gemma GGML OCR Math Pillow git CV API 继承 Web Llama Safetensors News Docker printf LoRA Augmentation ResNet-50 强化学习 hf 公式 Jetson ModelScope Zip FP64 个人博客 Template PyCharm mmap 报税 CEIR ONNX Random PyTorch Search SQLite Rebuttal Mixtral RL LLM Password GoogLeNet Logo 云服务器 PDB 版权 Hungarian Crawler Heatmap Plate CLAP Vim Hilton 搞笑 BF16 Vmess Knowledge Anaconda icon 净利润 JSON Pickle Disk Algorithm FP16 FlashAttention XML Transformers 算法题 Diagram diffusers Paddle SVR EXCEL uwsgi Markdown 域名 SPIE Image2Text Use LaTeX AI Excel Card Statistics GIT 递归学习法 Bin TTS scipy HuggingFace VGG-16 IndexTTS2 关于博主 transformers Harness GPTQ SAM VPN NLP BTC 图标 COCO Review BeautifulSoup SQL Bitcoin Pandas Numpy 论文速读 RAR 长期记忆 Qwen TSV DeepStream Hotel logger Sklearn NameSilo Pytorch Translation uWSGI Distillation CAM Land FastAPI Qwen2.5 Paper Tensor 第一性原理 财报 Magnet Firewall v0.dev Linux Breakpoint Ptyhon Michelin Ubuntu CC LLAMA QWEN 论文 ChatGPT FP32 PDF OpenAI YOLO NLTK torchinfo Python llama.cpp CTC Plotly 自我改进 Input HaggingFace TensorFlow FP8 v2ray Windows Google 音频 Github PIP XGBoost TensorRT RGB 腾讯云 Clash Freesound Food Interview Video 多进程 Miniforge Shortcut tar OpenCV Nginx Agent Website Cloudreve WAN 证件照 Streamlit Color 图形思考法 VSCode Quantization 多线程 tqdm 飞书 Datetime Quantize 顶会 UNIX API网关 CSV Data Animate Tracking C++ 签证 Jev Attention Bert CUDA Qwen2 Domain Conda git-lfs WebCrawler InvalidArgumentError Claude LeetCode Bipartite GPT4 ms-swift MD5 UI
站点统计

本站现有博文340篇,共被浏览1019349次

本站已经建立2719天!

热门文章
文章归档
回到顶部