有可能是在Docker容器中缺少配置。可以将以下代码添加到Dockerfile中:
FROM nginx
# 将自定义Nginx配置文件复制到容器中
COPY nginx.conf /etc/nginx/nginx.conf
# 将自定义的CORS配置文件编写到容器中
COPY cors.conf /etc/nginx/conf.d/cors.conf
# 暴露端口以允许流量通过
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
同时,需要在Nginx服务器配置中设置CORS头,以允许具有凭据的请求。在Nginx的配置文件中添加以下配置:
location / {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
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-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' '*' always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
......
}
这样,在启动Docker容器后,应该会在响应头中看到“CORS Access-Control-Allow-Credentials, Access-Control-Allow-Origin”了。