nginx配置strict-origin-when-cross-origin
时间: 2023-11-28 10:45:33 浏览: 2239
利用Nginx处理Vue开发环境的跨域的方法
为了解决Nginx配置https访问时出现的strict-origin-when-cross-origin报错,可以按照以下步骤进行配置:
1.进入nginx的conf目录,打开nginx.conf文件。
2.在http段中添加以下内容:
```nginx
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
```
3.在server段中添加以下内容:
```nginx
if ($request_method = 'OPTIONS') {
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-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
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';
```
4.重新加载Nginx配置文件。
```shell
nginx -s reload
```
这样就可以解决strict-origin-when-cross-origin报错了。
阅读全文