内网服务器A,服务映射到外网端口是8080,app接口请求外网8080端口的接口,出现跨域
下面有两种实现配置
server {
listen 6600;
server_name localhost;
root /opt/runner/target/yongxing-one-map-mobile/;
access_log /opt/runner/target/yongxing-mobile-access.log;
error_log /opt/runner/target/yongxing-mobile-error.log;
# 指定允许跨域的方法,*代表所有
# add_header Access-Control-Allow-Methods *;
# 表示请求头的字段 动态获取
# add_header Access-Control-Allow-Headers $http_access_control_request_headers;
gzip on;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary off;
gzip_comp_level 5;
gzip_buffers 32 16K;
gzip_min_length 1k;
#开始配置跨域
set $cors_origin "";
if ($http_origin ~* "^http://localhost(:\d+)?$"){
set $cors_origin $http_origin;
}
if ($http_origin ~* "^http://localhost$"){
set $cors_origin $http_origin;
}
# 本机ip
if ($http_origin ~* "^http://127.0.0.1:6600$"){
set $cors_origin $http_origin;
}
#外网ip ,是内网映射到外网,这里转了两手
if ($http_origin ~* "^http://114.114.114.114:9670$"){
set $cors_origin $http_origin;
}
add_header 'Access-Control-Allow-Origin' $http_origin;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
location /api {
#add_header Cache-Control "private,no-cache,max-age=0";
#add_header X-GW geowayonepiece;
# 此处后端已经增加了Access-Control-Allow-Origin header,故这里不进行重复设置,否则依然会出现跨域问题
add_header 'Access-Control-Allow-Origin' '';
#add_header Access-Control-Allow-Headers $http_access_control_request_headers;
#add_header Access-Control-Allow-Credentials true;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass_request_headers on;
# 第一:配置跨域,java 后台要配置跨域,具体实现百度,
# 第二:不配置跨域,可以隐藏请求头 proxy_hide_header Access-Control-Allow-Origin;
proxy_pass http://yongxing_backend;
access_log /opt/runner/target/yongxing-mobile-api.log;
}
location /bmfFiles {
add_header 'Access-Control-Allow-Origin' $http_origin;
proxy_pass http://127.0.0.1:7071;
}
}
location /atlas/ {
# 这里是代理第三方服的接口,隐藏请求,后面就不用处理----重点
proxy_hide_header Access-Control-Allow-Origin;
proxy_pass http://hnbmf_atlas;
}