gpt4 book ai didi

django - 我如何强制 nginx 使用来自上游服务器的 header 将响应保存在缓存文件夹中?

转载 作者:行者123 更新时间:2023-12-04 17:53:41 25 4
gpt4 key购买 nike

我的 API 使用 Django REST Framework,nginx 作为反向代理,redis 用于缓存一些静态 api 数据。
我试图用 Cache-Control: max-ageLast-Modify header 实现缓存。
简而言之,它看起来像这样:

class SomeViewSet(viewsets.ModelViewSet):
....
def list(self, request, *args, **kwargs):
queryset = self.get_queryset()
cache_key = self._get_cache_key() # get a key for reddis
response = self._get_data_from_cache(cache_key) # get a data from reddis
if response:
# If data in redis return Response with a same Last-Modify
# and 'Cache-Control': 'max-age=120'
return response

# Setting up new value for this viewset in a reddis
serializer = self.get_serializer(queryset, many=True)
now = datetime.datetime.now()
cache.set(cache_key, [now, serializer.data])
return Response(serializer.data, headers={'Last-Modified': now, 'Cache-Control': 'max-age=120'})

它像我预期的那样工作,浏览器缓存数据 120 秒,当它过期时,客户端使用 If-Modified-Since header 检查内容。
但是我虽然在设置 max-age header 时 nginx 会将其保存在缓存文件夹中,并且会在不访问服务器的情况下为所有客户端提供服务。
这是我在本地机器上测试的 nginx 配置:

upstream django {
server 127.0.0.1:8002;
}

proxy_cache_path /home/ivan/projects/kors/test_prod/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;

server {
listen 8000;
server_name 127.0.0.1; #
charset utf-8;
client_max_body_size 75M;

location /media {
alias /path/to/media;
expires 1y;
log_not_found off;
access_log off;
}
location /static {
alias /path/to/static;
expires 1y;
log_not_found off;
access_log off;
}

location / {
uwsgi_pass django;
include /path/to/uwsgi_params;
uwsgi_param Host $host;
uwsgi_param X-Real-IP $remote_addr;
uwsgi_param X-Forwarded-For $proxy_add_x_forwarded_for;
uwsgi_param X-Forwarded-Proto $http_x_forwarded_proto;

proxy_cache my_cache;
}
}

考虑这篇文章 nginx-caching-guide .

How Does NGINX Determine Whether or Not to Cache Something?
By default, NGINX respects the Cache‑Control headers from origin servers. It does not cache responses with Cache‑Control set to Private, No‑Cache, or No‑Store or with Set‑Cookie in the response header. NGINX only caches GET and HEAD client requests. You can override these defaults as described in the answers below.

我认为 Cache-Contol: max-age header 会强制 nginx 将 json 保存在缓存文件夹中。
文件夹已创建,但此处没有数据,我来自不同浏览器的所有请求都在访问服务器。
我错过了什么?或者我完全误解了使用 nginx 进行缓存的概念?

最佳答案

首先,您在 header 中添加 Cache-Control: max-age 不会告诉 nginx 对其执行任何操作。客户端管理如何处理 header 以及如何处理后续请求。

你可能是一个 url 的 nginx 响应缓存。看不到您的 nginx 配置有问题。

uwsgi_cache_path /home/ivan/projects/kors/test_prod/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;

location / {

include /path/to/uwsgi_params;
uwsgi_param Host $host;
uwsgi_param X-Real-IP $remote_addr;
uwsgi_param X-Forwarded-For $proxy_add_x_forwarded_for;
uwsgi_param X-Forwarded-Proto $http_x_forwarded_proto;

uwsgi_cache my_cache;
uwsgi_pass django;
}

而且你需要在这里使用 uwsgi 缓存模块。

请参阅此处 http://nginx.org/en/docs/http/ngx_http_uwsgi_module.html

关于django - 我如何强制 nginx 使用来自上游服务器的 header 将响应保存在缓存文件夹中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42247476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com