nginx 模块 location 顺序加载的问题

最近nginx 处理图片跨域的问题,前端用到canvas ,后台用的php,当前php 代码里面也可以处理跨域的问题,更好的方式,肯定是在nginx 端处理。

在location / 中加入

if ( $request_method = 'OPTIONS') {
add_header Access-Control-Allow-Origin $http_origin;
add_header Access-Control-Allow-Methods 'OPTIONS';
add_header Access-Control-Allow-Headers 'X-ACCESS_TOKEN,Access-Control-Allow-Origin,Authorization,Origin,x-requested-with,Content-Type,Content-Range,Content-Disposition,Content-Description,token';
add_header 'Access-Control-Max-Age' 60;
add_header 'Content-Length' 0;
return 204;
}

在 匹配php以及图片的 location 中也要加入 

if ( $http_origin ~ (.*).aaa.cn) {
add_header Access-Control-Allow-Origin $http_origin;
add_header Access-Control-Allow-Methods 'GET, POST';
add_header Access-Control-Allow-Headers 'X-ACCESS_TOKEN,Access-Control-Allow-Origin,Authorization,Origin,x-requested-with,Content-Type,Content-Range,Content-Disposition,Content-Description,token';
}

弄了很久,才发现有些没有生效,原因在于, location 加载会有相关的规则。具体参考:

https://www.cnblogs.com/zhaof/p/5945576.html?utm_source=itdadao&utm_medium=referral

原文地址:https://www.cnblogs.com/wjq310/p/10383503.html