时间:2023-02-07 16:48:01 | 来源:建站知识
时间:2023-02-07 16:48:01 来源:建站知识
location /live{ ... add_header 'Access-Control-Allow-Origin' 'http://domain:port' always; add_header 'Access-Control-Allow-Credentials' 'true'; # 为预检请求加的header add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE'; #为预检请求加的header add_header 'Access-Control-Allow-Headers' '*';}
第一个响应头是允许跨域的域server { set $cors ''; if ($http_origin ~* "^http://deomain01:port$") { set $cors $http_origin; } if ($http_origin ~* "^http://deomain02:port$") { set $cors $http_origin; } if ($http_origin ~* "^http://deomain002:port$") { set $cors $http_origin; } location /live{ ... add_header 'Access-Control-Allow-Origin' '$cors'; add_header 'Access-Control-Allow-Credentials' 'true'; # 为预检请求加的header add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE'; #为预检请求加的header add_header 'Access-Control-Allow-Headers' '*'; }
$http_origin这个格式是nginx取请求中header的XXX的值的。 map $http_origin $cors_list{ default http://aaa.cn; "~ http://bbb.cn" http://bbb.cn; } server { listen 8089; server_name localhost; location /live{ ... add_header 'Access-Control-Allow-Origin' '$cors_list'; add_header 'Access-Control-Allow-Credentials' 'true'; # 为预检请求加的header add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE'; #为预检请求加的header add_header 'Access-Control-Allow-Headers' '*'; }
map指令是ngx_http_map_module模块提供的,默认情况下nginx有加载这个模块。关键词:配置,指定