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不允许。

允许的情况会:

相关推荐
Shadow(⊙o⊙)28 分钟前
Linux进阶知识1.0
linux·运维·服务器
szephyr1 小时前
HTTPS 证书申请与自动续期:acme.sh + Let‘s Encrypt 完整记录
nginx·https·acme.sh·证书续期
闲云自留地1 小时前
告别命令恐惧症,openEuler 基础操作 + 文本 + 网络管理实战
linux·运维·云计算
其实防守也摸鱼2 小时前
常见安全架构中 Shiro 的认证确认机制解析
运维·服务器·数据库·windows·github
^Rocky3 小时前
IT 疑难杂症诊疗室:复杂技术问题解决实录
运维·bug
挖掘狂人4 小时前
当生产环境"变慢",我用这套 perf + strace 30 分钟定位瓶颈
linux·运维·性能优化
阿明66 小时前
Linux进程【Linux】
linux·运维·服务器
Android系统攻城狮6 小时前
Linux Gstreamer深度解析之gst_audio_resampler_update调用流程与实战(三十)
linux·运维·服务器·gstreamer音视频·音视频进阶
微信ipad协议开发6 小时前
微信视频号自动化:内容发布与互动的 API 实现
运维·微信
Lsetea9 小时前
部署证书后提示“证书名称与输入不匹配”:Safari 报错的三层排查
nginx·https·safari·ssl证书·openssl