django - nginx 没有从上游 gunicorn 返回 Cache-Control 标头
问题描述
我正在使用WhiteNoise从在 gunicorn 下运行的 Django 应用程序提供静态文件。由于某种原因,gunicorn 后端返回的Cache-Control
和Access-Control-Allow-Origin
标头没有通过 nginx 代理传递回客户端。
以下是对 gunicorn 后端的示例请求的响应:
% curl -I -H "host: www.myhost.com" -H "X-Forwarded-Proto: https" http://localhost:8000/static/img/sample-image.1bca02e3206a.jpg
HTTP/1.1 200 OK
Server: gunicorn/19.8.1
Date: Mon, 02 Jul 2018 14:20:42 GMT
Connection: close
Content-Length: 76640
Last-Modified: Mon, 18 Jun 2018 09:04:15 GMT
Access-Control-Allow-Origin: *
Cache-Control: max-age=315360000, public, immutable
Content-Type: image/jpeg
当我通过 nginx 服务器向同一个文件发出请求时,缺少两个标头。
% curl -I -H "Host: www.myhost.com" -k https://my.server.com/static/img/sample-image.1bca02e3206a.jpg
HTTP/1.1 200 OK
Server: nginx/1.10.3 (Ubuntu)
Date: Mon, 02 Jul 2018 14:09:25 GMT
Content-Type: image/jpeg
Content-Length: 76640
Last-Modified: Mon, 18 Jun 2018 09:04:15 GMT
Connection: keep-alive
ETag: "5b27758f-12b60"
Accept-Ranges: bytes
我的 nginx 配置几乎是gunicorn 部署文档中记录的内容,即我没有启用 nginx 缓存(nginx -T | grep -i cache
为空)或做任何我认为不寻常的事情。
我错过了什么?
解决方案
问题是你有
location / {
try_files $uri @proxy_to_app;
}
nginx 配置中的指令,所以 nginx 只是自己提供文件,而 gunicorn 甚至不知道它,当然不能添加标题。
推荐阅读
- javascript - 通过 C# 添加 attirubtes 时 HTML5 ondragstart 不会触发
- gspread - 如何将大量表中的数据添加到一张表中
- python - 慢速迭代的替代方案
- sql - TypeError:无法读取反应原生 sqlite 中未定义的属性“打开”
- php - 如何在PHP的while循环中分离输出的复选框变量结果?
- haskell - 序列号的建设性处理
- android - 如果它的适配器使用对由单独类管理的 MutableListView 的引用,我真的需要 DiffUtil.ItemCallback 来更新我的 RecyclerView 吗?
- java - 我们可以在不运行单独的 ignite 集群的情况下将 Apache ignite 与 Spring 应用程序一起使用吗
- ios - PKPaymentAuthorizationViewController 不调用委托
- python - 用于安装 pandas 的 Sagemaker 生命周期配置不起作用