首页 > 解决方案 > nginx 没有从上游 gunicorn 返回 Cache-Control 标头

问题描述

我正在使用WhiteNoise从在 gunicorn 下运行的 Django 应用程序提供静态文件。由于某种原因,gunicorn 后端返回的Cache-ControlAccess-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为空)或做任何我认为不寻常的事情。

我错过了什么?

标签: djangonginxgunicornwhitenoise

解决方案


问题是你有

location / {
    try_files $uri @proxy_to_app;
}

nginx 配置中的指令,所以 nginx 只是自己提供文件,而 gunicorn 甚至不知道它,当然不能添加标题。


推荐阅读