Unity webgl跨域问题 unity使用nginx设置跨域 ,修改请求头

跨域

什么是跨域

跨域是指浏览器因安全策略限制,阻止一个域下的网页访问另一个域下的资源。

一些常见的跨域情况:

  1. 协议不同
    http://example.com 请求 https://example.com
  2. 域名不同
    http://example.com 请求 http://anotherdomain.com
  3. 端口不同
    http://example.com:80 请求 http://example.com:8080
  4. 子域不同
    http://sub.example.com 请求 http://another.example.com
  5. 顶级域名不同
    http://example.com 请求 http://example.org
    这些情况都会触发浏览器的同源策略,导致跨域问题。

为什么会有跨域

浏览器的安全机制,防止不同来源的恶意脚本窃取数据。

如何解决跨域

  1. CORS(跨域资源共享)
    通过服务器设置响应头,允许特定域的访问。
  2. JSONP
    通过

nginx配置

复制代码
    server {
        listen       7963;
        server_name  localhost;

 
         location / {
          root   html;
          index  index.html index.htm;

          # 添加 CORS 头部
          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') {
              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';
              add_header Content-Length 0;
              add_header Content-Type text/plain;
              return 204;
          }
}

查看服务器是否支持跨域

如果服务不支持跨域,再怎么设置nginx的请求头都是不行的!
如果服务不支持跨域,再怎么设置nginx的请求头都是不行的!
如果服务不支持跨域,再怎么设置nginx的请求头都是不行的!

如何查看服务器是否支持跨域

https://cn.bing.com/?mkt=zh-CN 为例

打开之后按下F12 ,切换到网络,刷新网页

滑倒最上面,看响应头中是否有:Access-Control-Allow-Origin *

或者使用post man,找一下有没有:Access-Control-Allow-Origin *;, 有就是允许,没有就是不允许

很明显bing不允许。

允许的情况会:

相关推荐
泡沫冰@4 小时前
上章节中文件的讲解
前端·网络·nginx
三言老师5 小时前
CentOS7.9:Redis‑Cluster集群部署结构化实战教程
linux·运维·服务器·数据库
attitude.x6 小时前
Cursor+GitOps:自动化运维新姿势
运维·自动化
数智化管理手记7 小时前
主数据重复、错漏频发?一站式主数据管理平台如何落地?
大数据·运维·数据库·人工智能·数据挖掘
用户83134859306987 小时前
Vue+Three.js实现PCB电路板3D交互:元器件点击高亮、部件显隐、模型自动旋转
vue.js·webgl·three.js
Shell运维手记7 小时前
ARP 协议超详细讲解(适合网工考试 / 运维理解)
运维·网络·网络协议·智能路由器
西安景驰电子8 小时前
PCIe 授时卡原理及应用
运维·服务器·windows·单片机·嵌入式硬件·fpga开发
huainingning9 小时前
华三瘦AP切换为胖AP并配置无线功能
运维·网络·网络协议·tcp/ip
外滩运维专家9 小时前
十几台服务器要改配置,别再一台台 SSH 了
运维
冷莫溪10 小时前
Zabbix——认识及部署Zabbix(基于Rocky9.7)
linux·运维·php·zabbix