ingress跨域处理

avatar 2021年11月9日18:52:50 评论 1,635 次浏览

跨域主要是因为浏览器的同源策略限制,是服务对浏览器的一种约定,如果缺少,则对浏览器会产生影响,同源即是在同一个域中两个页面具有相同的协议,主要是protocol,主机(host),端口号(port)等

什么是跨域

当一个请求url的协议,域名,端口三者之间任意一个与当前页面url不同即为跨域

当前url 被请求页面url 是否跨域 原因
http://www.wulaoer.org/ http://www.wulaoer.org/index.html 同源(协议、域名、端口号相同)
http://www.wulaoer.org/ https://www.wulaoer.org/index.html 协议不同(http/https)
http://www.wulaoer.org/ http://www.baidu.com/ 主域名不同(wulaoer/baidu)
http://www.wulaoer.org/ http://wiki.wulaoer.org/ 子域名不同(www/wiki
http://www.wulaoer.org:8080/ http://www.wulaoer.org:9090/ 端口号不同(8080/9090)

下面看一下在nginx里配置,这里是在nginx里的service中配置的

location / {
 
    if ($http_origin ~* (*)) { //也可以指定域名
        set $cors "true";
    }
 
    if ($request_method = 'OPTIONS') {
        set $cors "${cors}options";  
    }
    if ($request_method = 'GET') {
        set $cors "${cors}get";  
    }
    if ($request_method = 'POST') {
        set $cors "${cors}post";
    }
 
    if ($cors = "true") {
        # Catch all incase there's a request method we're not dealing with properly
        add_header 'Access-Control-Allow-Origin' "$http_origin";
    }
 
    if ($cors = "trueget") {
        add_header 'Access-Control-Allow-Origin' "$http_origin";
        add_header 'Access-Control-Allow-Credentials' 'true';
        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';
    }
}

我这里没有指域名,就是所有,下面看一下ingress中配置的域名

kubectl edit ingress -n 命名空间  ingress名称
.........................
    nginx.ingress.kubernetes.io/cors-allow-credentials: "true"
    nginx.ingress.kubernetes.io/cors-allow-headers: DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,app_key,storeToken,Referer
    nginx.ingress.kubernetes.io/cors-allow-methods: PUT,GET,POST,OPTIONS
    nginx.ingress.kubernetes.io/cors-allow-origin: '*'(指域名)
    nginx.ingress.kubernetes.io/enable-cors: "true"
  creationTimestamp: "2021-10-27T14:38:01Z"
.........................

判断是否是跨域可以参考上面的,有写是跨域有些不能称之为跨域。没有了,就这么多吧。。。。

avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: