美文网首页
Nginx跨域

Nginx跨域

作者: 六弦极品 | 来源:发表于2018-08-24 10:25 被阅读0次

Nginx解决跨域问题(CORS)

CORS(Cross-Origin Resource Sharing) 跨域资源共享,是一种允许当前域(domain)的资源(比如html/js/web service)被其他域(domain)的脚本请求访问的机制,通常由于同域安全策略(the same-origin security policy)浏览器会禁止这种跨域请求。
如:a.com 请求b.com的资源时,就涉及到了跨域。目前常见的跨域解决方案一般分为如下几类:

  • jsonp返回值解决get请求
  • iframe解决跨域访问
  • Nginx 解决CORS

Nginx配置

简单配置:

server {
        listen       80;
        server_name  b.com;
        location /{
            add_header 'Access-Control-Allow-Origin' 'http://a.com';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET';
        }
}
  • add_header:授权从a.com的请求

  • 第二条add_header:当该标志为真时,响应于该请求是否可以被暴露

  • 第三条add_header:指定请求的方法,可以是GET,POST,PUT,DELETE,HEAD
    同样支持通配符,如允许来自任何域的请求:

server {
        listen 80;
        server_name b.com;
        location /{
            Access-Control-Allow-Origin: *
        }
    }

0x1_2 高级配置

location / {
     if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        #
        # Custom headers and headers various browsers *should* be OK with but aren't
        #
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        #
        # Tell client that this pre-flight info is valid for 20 days
        #
        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' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
     }
     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' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
     }
}

nginx允许多个域名跨域

map $http_origin $other_domain{
    default '';
    "~http://aaa.bbb.com" http://aaa.bbb.com;
    "~http://ccc.ddd.com"  http://ccc.ddd.com;
}
server {
   .................
    location /uploads {
        add_header Access-Control-Allow-Origin $other_domain;
        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;
        }
    .................
}

相关文章

  • 浏览器跨域的那些事

    整理中 目标: 了解跨域 解决跨域 服务器配置跨域(java, nginx) 前端调试时配置解决跨域 一、什么是跨...

  • nginx反向代理

    前端调用接口的时候跨域了怎么办呢,如下代码跨域: 通过nginx反向代理下载nginx:http://nginx....

  • 关于设置env等环境变量的思考

    1、如何处理跨域后台处理跨域前端处理跨域浏览器处理跨域 前端本地处理跨域:代理线上跨域的处理方式:Nginx反向代...

  • Nginx跨域

    Nginx解决跨域问题(CORS) CORS(Cross-Origin Resource Sharing) 跨域资...

  • ajax跨域--nginx反向代理

    用nginx反向代理实现跨域,是最简单的跨域方式。只需要修改nginx的配置即可解决跨域问题,支持所有浏览器,支持...

  • [mark]九种跨域方式实现原理

    前端如何使用proxyTable和nginx解决跨域问题 前言 前后端数据交互经常会碰到请求跨域,什么是跨域,以及...

  • nginx解决前端跨越详细方法

    nginx解决前端跨域 1、安装好nginx后,进入/usr/local/etc/nginx/nginx.conf...

  • nginx配置跨域请求

    转载 Nginx配置跨域请求 Access-Control-Allow-Origin 当出现403跨域错误的时候 ...

  • Nginx跨域请求设置

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

  • 配置Nginx后上传文件出现跨域问题

    问题详情:服务端已开启跨域,在配置Nginx反向代理后,使用ELMENT UI 反而出现跨域问题 Nginx监听9...

网友评论

      本文标题:Nginx跨域

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