我想知道如何为每个文件提供不同的内容types。
例如,我有以下nginxconfiguration:
server { listen 80; server_name site.com; location ~ ^/(.*) { root /home/site/; try_files $uri $uri.png $uri.bin =404; } }
因此,例如任何$uri.png
都会有image/png
内容types头,而$uri.bin
会有application/octet-stream
。
问题:我不知道如何确定哪个try_files
select了file
。
我有lua模块build立和工作,以防万一需要。
有没有其他人试过这样的事情?
假设我已经明白了你的所作所为,这对我有用:
if ($request_uri ~* \.png$) { more_set_headers "Content-type: image/png"; } if ($request_uri ~* \.(jpg|jpeg)$) { more_set_headers "Content-type: image/jpeg"; } if ($request_uri ~* \.gif$) { more_set_headers "Content-type: image/gif"; }
需要一个非标准的模块,除非nginx最近已经改变了,允许标准的add_header函数覆盖Content-type: http ://wiki.nginx.org/HttpHeadersMoreModule
如果你想反向(即原始URI不包含扩展名和文件系统),可以尝试用$ urireplace$ request_uri,并且try_files之前/之后的顺序可能很重要