【问题标题】:Configuring Nginx proxy microcache for a specific location within the server block为服务器块中的特定位置配置 Nginx 代理微缓存
【发布时间】:2017-02-06 06:59:40
【问题描述】:

我有一个 Django 网站,每隔几秒就会发布一次新内容。

我网站主页上的授权用户登陆/,而未经授权的用户登陆/unauth/unauth 显示与 / 类似的内容,但没有任何个人详细信息。我的网站使用以 gunicorn 作为上游的 nginx(反向代理)。

我正在尝试在location/unauth 上实现nginx microcaching,但到目前为止还没有成功(/var/cache/nginx 中没有出现任何内容)。在 location/unauth 中添加 add_header X-Cache-Status $upstream_cache_status; 不会在响应中产生任何内容。这几乎就像完全被忽略了(!)的位置块。

你能帮我解决这个问题吗?如果您想查看整个 nginx.conf,请告诉我。


我在我的 nginx 配置文件中添加了以下内容:

   #outside the server block
   proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=100m inactive=6m;

   #inside the server block
   location /unauth {
        add_header X-Cache-Status $upstream_cache_status;
        proxy_cache my_cache;
        proxy_cache_lock on;
        proxy_cache_valid 200 1s;
        proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;

        proxy_buffering on;
        proxy_buffers 24 4k;
        proxy_buffer_size 2k;
        proxy_busy_buffers_size 8k;

        try_files $uri @http_proxy_to_app;
    }

http://example.com/unauth/ 产生的 HTTP 响应头如下:

Status:             HTTP/1.1 200 OK
Server:             nginx   
Date:               Sun, 05 Feb 2017 00:10:03 GMT   
Content-Type:       text/html; charset=utf-8    
Transfer-Encoding:  chunked 
Connection:         close   
Vary:               Accept-Encoding 
Expires:            Sun, 05 Feb 2017 00:10:13 GMT   
Vary:               Cookie  
Last-Modified:      Sun, 05 Feb 2017 00:10:03 GMT   
Cache-Control:      max-age=10  
X-Frame-Options:    SAMEORIGIN  
Content-Encoding:   gzip

【问题讨论】:

  • 能否在 location 块中添加add_header X-Cache-Status $upstream_cache_status; 并重新运行请求?这将提供一些关于请求是否未命中缓存的信息。
  • 尝试将其更改为 www-data:www-data。也可能是因为您正在对@http_proxy_to_app 进行内部重定向。作为测试,您可以将缓存配置添加到该块吗?

标签: caching nginx reverse-proxy http-caching nginx-location


【解决方案1】:

缓存不起作用的原因是内部重定向到@http_proxy_to_app。 proxy_cache 需要在后重定向上下文中。

【讨论】:

  • 快速澄清。如果uri包含参数或参数(例如http://example.com/unauth/?page=2#section18),nginx.conf中的位置块location /unauth也会捕获这样的uri吗?
猜你喜欢
  • 1970-01-01
  • 2020-09-29
  • 2021-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-19
  • 2019-07-15
相关资源
最近更新 更多