avatar

sunday

Sunday's Blog

  • 首页
Home 浏览器出现的一些跨域的问题,如何正确设置cors
文章

浏览器出现的一些跨域的问题,如何正确设置cors

Posted 2024-01-21 Updated 2024-01- 21
By sunday
10~13 min read

浏览器出现的一些跨域的问题

1.预检请求问题,就是会以options的方法发送请求

比如你有如下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-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';

    if ($request_method = 'OPTIONS') {
        return 204;
    }
} 

浏览器会报如下错误:

Access to fetch at 'https://example.com/api/send' from origin 'https://example2.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

这时候变更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-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
   # 去掉以下配置就可以了 
   # if ($request_method = 'OPTIONS') {
   #     return 204;
   # }
} 

2.返回了多个Access-Control-Allow-Origin响应头问题

比如你有如下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-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
} 

浏览器会报如下错误:

Access to fetch at 'https://example.com/api/send' from origin 'https://example2.com' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed. Have the server send the header with a valid value, or, if an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

这时候变更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-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
} 

疑难杂症, nginx
nginx 疑难杂症
License:  CC BY 4.0
Share

Further Reading

Mar 27, 2025

解决nextjs15使用useLocalStorage报错的问题

已经在组件中使用"use client"声明了,还是报错,错误如下: Unhandled Runtime Error Error: useLocalStorage is a client-only hook 1.解决方案1-使用 dynamic 导入 import dynamic from 'nex

Dec 21, 2024

让debian12笔记本盒盖不休眠

1.打开终端,编辑 /etc/systemd/logind.conf 文件,查找下面的选项并改成下面那样 HandleLidSwitch=ignore HandleLidSwitchExternalPower=ignore LidSwitchIgnoreInhibited=no

Dec 3, 2024

如何查找字体的family名称

有时一些软件引用字体总是需要填写字体的family名称,但是实际情况是字体文件的名称不总是等于family里的名称 1.mac上查找字体family名称 请确保安装了fontconfig 没有则执行 brew install fontconfig 然后执行查找命令 fc-scan -b /Users

OLDER

next.js中使用zustand引入第三方库报错self is not defined

NEWER

Window11上用vscode一键打包上传部署react/vue项目,一键脚本

Recently Updated

  • nextjs15使用better-sqlite3的连接报错问题
  • nextjs + clerk + supabase + realtime 实时监听数据库更改
  • 解决nextjs15使用useLocalStorage报错的问题
  • mac上使用nodejs appium控制chrome浏览器
  • 2024年终总结

Trending Tags

nginx acme 强制跳转HTTPS nodejs 代理 mac 神器 vue3 工具 docker

Contents

©2025 sunday. Some rights reserved.

Using the Halo theme Chirpy