用户上传的文件该由谁来发?公开与私有的媒体怎么区分?怎么既做权限检查又不占 Python worker?这篇用真实的 nginx 把两种方式的响应逐项验证(上传校验见第 126 篇,大文件见第 169 篇)。
GET /media/a.png → 200 | Content-Type image/png | X-Content-Type-Options nosniff | CSP default-src 'none'; sandbox | Cache-Control max-age=604800 GET /media/v.svg → 200 | Content-Type image/svg+xml(SVG 可以内嵌脚本,所以加了 CSP sandbox 限制它) GET /media/nope.png → 404 GET /media/ → 403 GET /media/../private/secret.txt → 404
上一篇:静态文件生产部署;下一篇:Docker 部署 Flask。
nginx 是自己起的实例(非默认端口)。
未登录访问 /docs/1 → 401 | b'<!doctype html>\n<html lang=en>\n<title>40' | Content-Disposition: None ann 访问自己的 /docs/1 → 200 | b'%PDF-1.4 ann-report' | Content-Disposition: attachment; filename*=UTF-8''%E5%B9%B4%E5%BA%A6%20%E6%8A%A5%E5%91%8A.pdf ann 访问 bob 的 /docs/2 → 403 | b'<!doctype html>\n<html lang=en>\n<title>40' | Content-Disposition: None bob 访问自己的 /docs/2 → 200 | b'bob secret' | Content-Disposition: attachment; filename*=UTF-8''secret.txt 访问不存在的 /docs/9 → 404 | b'<!doctype html>\n<html lang=en>\n<title>40' | Content-Disposition: None 直接请求内部地址 /_protected/report.pdf → 404(internal location 只能由 nginx 内部重定向访问) 带 Range 的授权请求 → 206 | b'%PDF-' | Content-Range bytes 0-4/19(nginx 对 X-Accel-Redirect 的文件也支持 Range)