nginx的try_files addheader不工作

我有简单的位置用于服务器生成的caching:

location /api/get-hloc { #add acccess-allow headers add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; #try cached files root /dev/shm/get-hloc/; try_files /$arg_hash.hloc /stocks/graphics/get-iis-graphic?$args; } 

它能做什么? 它只是检查是否有内存中的文件,可以为请求服务,如果没有文件,尝试实际生成文件的位置为未来的请求,并将其提供给客户端。

这一切都按预期工作,除了被忽略的add_header指令。

在try_files中尝试新位置之前是否可以添加连接头?或者我应该只在端点位置添加连接头?

尝试添加“always”到add_header指令:

如果指定了always参数(1.7.5),则无论响应代码如何,标题字段都将被添加。

请参阅文档了解更多详情。

对于你的例子,它看起来像这样:

 location /api/get-hloc { #add acccess-allow headers add_header 'Access-Control-Allow-Origin' '*' always; add_header 'Access-Control-Allow-Credentials' 'true' always; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always; add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type' always; #try cached files root /dev/shm/get-hloc/; try_files /$arg_hash.hloc /stocks/graphics/get-iis-graphic?$args; } 

请注意以下文档段落:

可能有几个add_header指令。 当且仅当在当前级别上没有定义add_header指令时,这些指令才从前一级inheritance。

这意味着如果你在前一级(例如服务器级别)有add_header指令,它们将被忽略,只有当前位置级别的add_header指令被使用。