美文网首页
Nginx跨域设置 Access-Control-Allow-O

Nginx跨域设置 Access-Control-Allow-O

作者: 平凡的运维之路 | 来源:发表于2020-03-17 21:51 被阅读0次

现象

  • 当出现403跨域错误的时候 No 'Access-Control-Allow-Origin' header is present on the requested resource,需要给Nginx服务器配置响应的header参数

  • 网上大多数的解决方案是:

  • 只需要在Nginx的配置文件中配置以下参数:

  • 事实上这个参数在我的 Nginx 1.12 上并不能用。

location / {  
  add_header Access-Control-Allow-Origin *;
  add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
  add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
} 

解决方法

  • 在nginx1.12版本之上使用以下的方式来解决跨域方式
location /agentProxy/{
         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' '*';

            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
            return 204;
        }
        if ($request_method = 'POST') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' '*';
        }
        if ($request_method = 'GET') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' '*';
        }
        
        proxy_pass      http://agentProxy;
        proxy_set_header Host $host;
 }

相关文章

  • Nginx跨域设置 Access-Control-Allow-O

    现象 当出现403跨域错误的时候 No 'Access-Control-Allow-Origin' header ...

  • Nginx跨域请求设置

    Nginx跨域请求设置 开发环境中,前后端分离开发时,经常会有跨域请求的问题出现,Nginx可以设置如下: 说明:...

  • nginx设置跨域

    当出现403跨域错误的时候 No 'Access-Control-Allow-Origin' header is ...

  • Nginx 跨域设置

    nginx配置文件中,在需要跨域的location内,增加如下配置,重启nginx即可。

  • Nginx 常用配置

    Nginx 相关配置, 包括开启 gzip,设置缓存时间,包括前后端开发设置代理来避免跨域等 友情链接 Nginx...

  • nginx配置允许跨域

    nginx设置允许跨域 这一段可以放在http、server、location里面分别代表不同程度的跨域,其中Ac...

  • vue 打包之后 接口报错404

    注:已经设置过ngnix反向代理 问题:vue项目proxyTable设置的代理;nginx后台配置了跨域如图 当...

  • vue前端配置代理实现跨域请求

    跨域的解决方法:设置让服务器允许跨域,使用nginx反向代理服务器实现 在config.js中配置config配置...

  • C# Web直接上传视频或者文件到OSS

    代码 解决本地调试跨域问题 设置跨域规则:找到OSS存储——Bucket列表——基础设置——跨域访问——设置 暴露...

  • Nginx 跨域设置(CORS)

    浏览器同源策略 同源策略是Web应用程序安全模型中的一个重要概念。根据该策略,Web浏览器允许第一个Web页面中包...

网友评论

      本文标题:Nginx跨域设置 Access-Control-Allow-O

      本文链接:https://www.haomeiwen.com/subject/unnxyhtx.html