我有一个设置,其中图像以nodejs中的即时方式呈现。之后,图像被缓存在一个目录中,nginx将在后续请求中直接获取它们。
我如何在nodejs / javascript中生成相同的etag,因为nginx将在第二次请求时生成?
答案 0 :(得分:0)
我有一个想法。您可以设置nginx
以使用try_files
来首先检查文件是否存在于静态目录中,如果不存在,则代理节点以进行创建。这不是您正在寻找的,但它将允许浏览器缓存一致的URL,然后etag不那么重要。
location / {
root /path/to/root/of/static/files;
try_files $uri $uri/ @nodeserver;
expires max;
access_log off;
}
location @nodserver {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:3000;
}
答案 1 :(得分:0)
以下是答案,概述了Nginx如何计算etag:https://serverfault.com/questions/690341/algorithm-behind-nginx-etag-generation
基本上
etag = sprintf("%xT-%xO", file.last_modified_time, content_length)
您可以使用sprintf-js在js中重复此操作,但是在nginx缓存文件之前,您需要知道文件的last_modified_time,这是IMO不可能的。