使用nginx跨域出现Access-Control-Allow-Origin有两个相同的标头
时间: 2023-11-16 10:01:48 浏览: 109
使用nginx跨域出现Access-Control-Allow-Origin有两个相同的标头是因为在nginx配置中重复添加了Access-Control-Allow-Origin头信息。这通常是由于在nginx配置文件中多次添加了add_header指令导致的。解决方法是在nginx配置文件中只添加一次Access-Control-Allow-Origin头信息,或者将多次添加的头信息合并为一个。以下是一个示例nginx配置文件,其中只添加了一次Access-Control-Allow-Origin头信息:
```
server {
listen 80;
server_name example.com;
location / {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET,POST';
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET,POST';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
# rest of the configuration
}
}
```