Nginx 设置CSP开放所有权限包括允许跨域
要设置 Nginx 来允许跨域和开放所有权限,你需要在 Nginx 配置文件中添加以下内容:
location / {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
add_header 'Access-Control-Allow-Credentials' 'true';
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
}
将以上代码添加到你的 Nginx 配置文件中的 server 块中(通常位于 /etc/nginx/nginx.conf 或 /etc/nginx/conf.d/default.conf)。然后重新加载 Nginx 配置文件以使更改生效。
这将允许来自任何域的请求,并允许 GET、POST 和 OPTIONS 方法。它还允许特定的请求头,并启用了凭证(即允许携带身份验证信息)。
请注意,开放所有权限可能存在安全风险,因此请根据你的特定需求调整配置
原文地址: http://www.cveoy.top/t/topic/iJ7b 著作权归作者所有。请勿转载和采集!