Nginx条件不正确评估

我遇到了一个奇怪的问题与nginx和它如何评估条件。

这里是相关的configuration:

set $cors FALSE; if ($http_origin ~* (http://example.com|http://dev.example.com:8000|http://dev2.example.com)) { set $cors TRUE; } if ($request_method = 'OPTIONS') { set $cors $cors$request_method; } if ($cors = 'TRUE') { add_header 'Access-Test' "$cors"; add_header 'Access-Control-Allow-Origin' "$http_origin"; add_header 'Access-Control-Allow-Methods' 'POST, OPTIONS'; add_header 'Access-Control-Max-Age' '1728000'; } if ($cors = 'TRUEOPTIONS') { add_header 'Access-Test' "$cors"; add_header 'Access-Control-Allow-Origin' "$http_origin"; add_header 'Access-Control-Allow-Methods' 'POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'X-Requested-With, X-Prototype-Version'; add_header 'Access-Control-Max-Age' '1728000'; add_header 'Content-Type' 'text/plain'; } 

所以,条件块永远不会触发。

删除条件时,我发现“Access-Test”标题和“Access-Control-Allow-Origin”设置正确,但是,如上所述,启用条件将导致标题不能发送。

我正在运行testing:

 curl -Iv -i --request "OPTIONS" -H "Origin: http://example.com" http://staging.example.com/ 

我错过了什么明显的? 我已经尝试了“如果”和没有引号等

这是nginx 1.2.9。

好的,我显然需要在add_header行正确触发之前在条件块中添加显式的“return”语句。

所以,像这样的:

 if ($cors = 'TRUEOPTIONS') { add_header 'Access-Test' "$cors"; add_header 'Access-Control-Allow-Origin' "$http_origin"; add_header 'Access-Control-Allow-Methods' 'POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'X-Requested-With, X-Prototype-Version'; add_header 'Access-Control-Max-Age' '1728000'; add_header 'Content-Type' 'text/plain'; return 204; }