nginx如果声明里面的位置返回404

下面的块

location / { if ($http_origin ~* (https?://[^/]*\.example\.com(:[0-9]+)?)) { add_header 'Access-Control-Allow-Origin' "$http_origin"; } try_files $uri $uri/ /index.php?$args; } 

…导致一个404,因为上面的代码永远不会到达try_files指令,所以:

  1. 这与nginx的IfIsEvil有关吗?

  2. 如果是,那么有没有其他的方式来testinghttp_origin不使用if语句?

我已经用nginx> 1.4(1.4.6,1.7,1.7.8)来试试这个。

我会用map

 map $http_origin $cors_header { default ""; "~^https?://[^/]+\.example\.com(:[0-9]+)?$" "$http_origin"; } server { ... location / { add_header Access-Control-Allow-Origin $cors_header; try_files $uri $uri/ /index.php; } ... }