我已经为内部IP 192.168.1.119
添加了一个主机条目,作为/etc/hosts
中的multiserver.billinghub.net
。
当我访问 http:multiserver.billinghub.net/test/customer-portal时,它对http://multiserver.billinghub.net/jbilling/api/authentication/authenticate
进行内部调用,由于域相同,浏览器不会标记XHR调用。
但是当我使用IP地址而不是域名http://192.168.1.119/test/customer-portal
时,内部调用使用的是multiserver.billinghub.net
,这被视为CORS请求并被浏览器阻止。
我在后端使用Nginx来反向代理请求,并添加了必要的CORS头来解决上述问题。这是我的nginx配置,灵感来自这个要点评论。
upstream customer-portal {
random two least_conn;
server 192.168.1.119:8087;
server 192.168.1.113:8087;
keepalive 4;
}
upstream jbilling {
hash $binary_remote_addr consistent;
server 192.168.1.119:8080;
server 192.168.1.113:8080;
keepalive 4;
}
map $http_origin $cors_origin_header {
default "";
"~(^|^http:\/\/)(localhost$|localhost:[0-9]{1,4}$)" "$http_origin";
"http://multiserver.billinghub.net" "$http_origin";
"http://192.168.1.119" "$http_origin";
}
map $http_origin $cors_cred {
default "";
"~(^|^http:\/\/)(localhost$|localhost:[0-9]{1,4}$)" "true";
"http://multiserver.billinghub.net" "true";
"http://192.168.1.119" "true";
}
## Server block with port and redirect config
server {
listen 80 reuseport;
server_name multiserver.billinghub.net;
gzip on;
gzip_types application/xml;
gzip_min_length 1000;
add_header 'Access-Control-Allow-Origin' '$cors_origin_header' always;
add_header 'Access-Control-Allow-Credentials' '$cors_cred' always;
add_header 'Access-Control-Allow-Methods' "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH" always;
add_header 'Access-Control-Allow-Headers' "Accept, Accept-Language, Content-Language, Content-Type, authorization, Origin, Referer, User-Agent, Cache-Control, DNT, If-Modified-Since, Cache-Control, Range, Uuid" always;
add_header 'Access-Control-Expose-Headers' '*' always;
if ($request_method = 'OPTIONS' ) {
return 204 no-content;
}
location /test/customer-portal {
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://customer-portal;
# Required for new HTTP-based CLI
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_request_buffering off;
proxy_buffering off; # Required for HTTP-based CLI to work over SSL
# set client body size to 2M #
client_max_body_size 500M;
}
location /jbilling {
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://jbilling;
# Required for new HTTP-based CLI
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_request_buffering off;
proxy_buffering off; # Required for HTTP-based CLI to work over SSL
# set client body size to 2M #
client_max_body_size 500M;
}
}
字符串
添加上述配置后,我仍然得到一个CORS错误,由于某种原因,我得到了多个值在Access-Control-Allow-Header
像这样的
有人能告诉我知道我错过了什么吗?谢啦,谢啦
1条答案
按热度按时间zd287kbt1#
TL;DR
感谢我的好朋友Bharath,他建议将
add_header
指令从server
级别移动到location
级别,问题得到了解决。由于重复
add_header
指令并不美观,所以我创建了一个cors.conf
文件,并将其包含在location
块中。这是我的cors.conf文件:
字符串
更新nginx配置:(为简洁起见,不包括上游块)
型
PS:* 按照这个ServerFault answer使用
ngx_headers_more
模块构建Nginx也对我有用。*为什么我首先在Access-Control-Allow-Origin中获得多个值?
当从customer-portal调用jBilling时,Nginx正在添加所需的CORS origin header,但当调用从上游jBilling -> Nginx -> customer-portal返回时
Nginx再次添加了CORS头,因此该值被重复,因为如果头存在,
add_header
指令不会覆盖该值。当
add_header
指令转移到location
块时,根据Nginx,我们需要手动设置所有必要的头值。我仍然觉得使用
more_headers
模块构建Nginx是解决这个问题的更好方法,因为使用ngx_headers_more模块可以获得更多功能。更新:
我安装了headers-more-nginx-module并将其加载到我的nginx.conf中,这样我就不必维护一个单独的cors.conf(我在上面创建的那个),并且我可以直接在
server
级别设置必要的CORS头。下面是我更新的nginx.conf:
型
更新nginx配置:
型