【发布时间】:2021-04-20 23:40:00
【问题描述】:
处理CORS请求的common solution如下:
location ... {
add_header Access-Control-Allow-Origin ...;
add_header Access-Control-Allow-Credentials ...;
add_header Access-Control-Allow-Headers ...;
add_header Access-Control-Allow-Methods ...;
if ($request_method = 'OPTIONS') {
add_header Access-Control-Allow-Origin ...;
add_header Access-Control-Allow-Credentials ...;
add_header Access-Control-Allow-Headers ...;
add_header Access-Control-Allow-Methods ...;
return 204;
}
}
UPD顺便说一句,从I can see 来看,不需要重复的add_header。
【问题讨论】:
-
没关系。来自引用的nginx.com/resources/wiki/start/topics/depth/ifisevil 页面:“在某些情况下,您根本无法避免使用
if,例如,如果您需要测试一个没有等效指令的变量” 并且有一个 when -it-can't-be-avoided 示例在功能上与问题中的情况相同:if ($request_method = POST ) { return 405; }所以答案是:对于这种情况,您无法避免它。就缺乏nginx.com/resources/wiki/start/topics/depth/ifisevil 所描述的风险而言,这并不能使它“安全”——但只是没有真正的替代方案