如何使用 proxy_pass 和 if_modified_since 让 nginx 返回 304-Not Modified

Kev*_* G. 5 nginx

我看到有迹象表明这应该有效,所以我希望我错过了一些简单的事情。

我们有两个 nginx“服务器”,前面有一个 proxy_pass 缓存,后面有一个 SSI 服务器。如果我在请求中使用 If-Modified-Since 标头直接访问 SSI 服务器,它将返回 304-Not Modified 响应,这正是我们想要的。但我无法让 proxy_pass 缓存服务器返回 304-Not Modified 任何内容。

我应该能够让 proxy_pass 返回 304-Not Modified 吗?还有其他人可以使用您可以分享的配置吗?或者你能发现我的配置中的问题吗?

# this is the cache in front
server {
    listen 8096;    
    server_name _;
    proxy_buffering on;

    location /assets {
        proxy_pass http://localhost:8095;
        proxy_cache   my-cache;
        proxy_cache_valid  200s;
        if_modified_since before;
    }       
}

server {
    listen 8095;
    server_name _;
    root /var/www/;
    location / { deny all; }

    location /assets {}
        ssi on; # on or off doesn't make a difference to the front-end cache behavior
        if_modified_since before;   
    }
}

# here's the base config, fwiw:
proxy_buffering         on;
proxy_cache_valid       any 10m;
proxy_cache_path        /var/cache/nginx levels=1:2 keys_zone=my-cache:8m max_size=3000m inactive=24h;
proxy_temp_path         /var/cache/nginx/tmp;
proxy_buffer_size       4k;
proxy_buffers           100 8k;
Run Code Online (Sandbox Code Playgroud)

谢谢。

Kev*_* G. 1

是的,这是可能的,是的,这个配置工作正常。事实证明,nginx 缓存了一个根据我的测试设置为下周的 Expires 标头。 没关系:-/