nginx解决strict-origin-when-cross-origin
时间: 2023-08-30 13:09:39 浏览: 964
"strict-origin-when-cross-origin" 是一个安全策略,用于防止跨站点数据泄露。当使用此策略时,浏览器将发送 Referer 标头,但只包含源站点,而不包含路径和查询参数。
如果你想在 nginx 中解决 "strict-origin-when-cross-origin" 问题,可以使用以下方法:
1. 在 nginx 配置文件中添加以下代码:
```
add_header Referrer-Policy strict-origin-when-cross-origin;
```
这将在响应头中添加 "Referrer-Policy" 标头,并将其设置为 "strict-origin-when-cross-origin"。
2. 如果你使用的是 HTTPS,可以将以下代码添加到 nginx 配置文件中:
```
proxy_set_header Referer "";
```
这将在转发请求时删除 Referer 标头,从而避免跨站点数据泄露的问题。
以上两种方法都可以解决 "strict-origin-when-cross-origin" 问题,具体选择哪种方法取决于你的具体情况。
相关问题
nginx 解决 strict-origin-when-cross-origin 跨域
为了解决`strict-origin-when-cross-origin`跨域问题,可以在nginx配置文件中添加以下内容:
```
location / {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
#
# Custom headers and headers various browsers *should* be OK with but aren't
#
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
#
# Tell client that this pre-flight info is valid for 20 days
#
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}
}
```
这段代码会在nginx的配置文件中添加跨域处理的相关配置,包括允许的请求方法、请求头、响应头等信息。其中`Access-Control-Allow-Origin`设置为`*`表示允许所有来源的请求访问该资源。
nginx 处理 strict-origin-when-cross-origin
Nginx 本身并不直接处理 strict-origin-when-cross-origin,这是一个由浏览器执行的安全特性。strict-origin-when-cross-origin 是默认的跨域请求策略,它在不同源的情况下会发送简单请求(GET、POST等)的 origin 头,并且发送包含身份验证凭据的请求时不会发送 origin 头。
Nginx 的作用是作为一个 Web 服务器或反向代理服务器,可以通过配置来控制请求的转发和响应的处理。如果你需要在 Nginx 上处理跨域请求,你可以通过配置相关的头文件来实现。
在 Nginx 中,你可以使用 `add_header` 指令来添加头文件。例如,你可以在 Nginx 的配置文件中添加以下指令:
```
location / {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
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;
}
}
```
上述配置将在响应中添加跨域请求所需的头文件,允许所有来源(`Access-Control-Allow-Origin: *`),并允许 GET、POST、OPTIONS 方法,以及一些常见的请求头。
请注意,这只是一个简单的示例配置,你可以根据你的具体需求进行调整。另外,由于浏览器执行跨域请求策略,仍然需要在客户端的 JavaScript 代码中进行相应的配置,以确保请求能够成功发送。
阅读全文
相关推荐














