子应用xhr请求跨域问题(ngxin)
描述bug 我有一个主应用(vue3+vite端口:878),和一个子应用(vue3+vite端口:810) 两个应用本地打包以后放在同一个nginx里,分两个server, 主应用:192.168.160.75:878 子应用:192.168.160.75:810 现在主应用访问子应用静态资源页面是正常的, 但是主应用里的子应用发起请求(192.168.160.75:810/api/xxxx)会跨域报错:
Access to XMLHttpRequest at 'http://192.168.160.75:810/api/uac/user/page?pageNum=1&_t=1675142238757' from origin 'http://192.168.160.75:878' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
本地nginx配置是这样的: server { client_max_body_size 100m; listen 878; server_name localhost;
location / {
add_header "Access-Control-Allow-Credentials" true;
add_header "Access-Control-Allow-Origin" "*";
add_header "Access-Control-Allow-Headers" "X-Requested-With,Content-Type";
add_header "Access-Control-Allow-Methods" "PUT,POST,GET,DELETE,OPTIONS";
root html/main;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /api/ {
proxy_pass http://192.168.162.110;
add_header "Access-Control-Allow-Credentials" true;
add_header "Access-Control-Allow-Origin" "*";
add_header "Access-Control-Allow-Headers" "X-Requested-With,Content-Type";
add_header "Access-Control-Allow-Methods" "PUT,POST,GET,DELETE,OPTIONS";
add_header "Content-Type" "application/json; charset=utf-8";
}
}
server {
client_max_body_size 100m;
listen 810;
server_name localhost;
location / {
add_header "Access-Control-Allow-Credentials" true;
add_header "Access-Control-Allow-Origin" "*";
add_header "Access-Control-Allow-Headers" "X-Requested-With,Content-Type";
add_header "Access-Control-Allow-Methods" "PUT,POST,GET,DELETE,OPTIONS";
root html/uac;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /api/ {
proxy_pass http://192.168.162.110;
add_header "Access-Control-Allow-Credentials" true;
add_header "Access-Control-Allow-Origin" "*";
add_header "Access-Control-Allow-Headers" "X-Requested-With,Content-Type";
add_header "Access-Control-Allow-Methods" "PUT,POST,GET,DELETE,OPTIONS";
add_header "Content-Type" "application/json; charset=utf-8";
}
}
- 单独访问子应用:192.168.160.75:810,接口不存在跨域错误,
- 在开发环境:(vite devServe)中,主应用内子应用访问接口也没有报错。 请问是我nginx 配置有问题吗?希望给与解答 十分感谢!!!
+1
+1 我在开发环境 主应用内子应用访问接口跨域
跨域问题可能需要自行排查,这个报错的意思是 预检请求(preflight request)没有配置响应头 Access-Control-Allow-Origin,当我在 google 里面搜索 nginx preflight Access-Control-Allow-Origin,出来的第一篇文章似乎有了答案,大家要善于用搜索引擎
已解决。
只如初见 @.***
------------------ 原始邮件 ------------------ 发件人: @.>; 发送时间: 2023年2月9日(星期四) 上午10:45 收件人: @.>; 抄送: @.>; @.>; 主题: Re: [Tencent/wujie] 子应用xhr请求跨域问题(ngxin) (Issue #366)
跨域问题可能需要自行排查,这个报错的意思是 预检请求(preflight request)没有配置响应头 Access-Control-Allow-Origin,当我在 google 里面搜索 nginx preflight Access-Control-Allow-Origin,出来的第一篇文章似乎有了答案,大家要善于用搜索引擎
— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>
已解决。 只如初见 @.*** … ------------------ 原始邮件 ------------------ 发件人: @.>; 发送时间: 2023年2月9日(星期四) 上午10:45 收件人: @.>; 抄送: @.>; @.>; 主题: Re: [Tencent/wujie] 子应用xhr请求跨域问题(ngxin) (Issue #366) 跨域问题可能需要自行排查,这个报错的意思是 预检请求(preflight request)没有配置响应头 Access-Control-Allow-Origin,当我在 google 里面搜索 nginx preflight Access-Control-Allow-Origin,出来的第一篇文章似乎有了答案,大家要善于用搜索引擎 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>
你好,可以请教下如何配置的吗
大哥你们都是咋解决的