Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.3k views
in Technique[技术] by (71.8m points)

Nginx location 中如何基于upstream应用中的请求头是否存在来做不同处理

背景

后端代码中对部分接口做了处理 支持跨域请求

@CrossOrigin(allowCredentials = "true")

但是后面又有新的接口 需要支持跨域 故为了方便起见 直接在Nginx层统一支持

add_header Access-Control-Allow-Origin $http_origin;

这样配置后 虽然新接口OK了 但是之前代码中已经显式支持跨域的接口就用不了了

The 'Access-Control-Allow-Origin' header contains multiple values 'https://foo.com, https://foo.com', but only one is allowed.

问:
Nginx location 中怎么动态添加Access-Control-Allow-Origin呢

location /api {
    proxy_pass xxx;
    if (Access-Control-Allow-Origin not exists ) {
        add_header Access-Control-Allow-Origin $http_origin;
    }
}

注:
Nginx 版本

nginx version: nginx/1.4.7

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

if ($http_Access_Control_Allow_Orign = '')


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...