Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nginx部署,无法访问子应用,提示跨域拦截 #1477

Open
shawn2013 opened this issue Dec 14, 2024 · 1 comment
Open

Nginx部署,无法访问子应用,提示跨域拦截 #1477

shawn2013 opened this issue Dec 14, 2024 · 1 comment

Comments

@shawn2013
Copy link

子应用(Vite+vue3)部署在ip1上的nginx,基座应用(VUE3+Vite如果在本地开发环境运行,可以正常访问子应用,但是如果基座应用也发布到Nginx,会出现如下问题:
Access to XMLHttpRequest at 'http://ip1:6010/xxxx/yyy/menu?__rnd_axios=1734185969581' from origin 'http://ip1' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Nginx配置如下:
`
listen 6010;
server_name ip1;

location / {
if ( $request_uri ~* ^.+.(js|css|jpg|png|gif|tif|dpg|jpeg|eot|svg|ttf|woff|json|mp4|rmvb|rm|wmv|avi|3gp)$ ){
  add_header Cache-Control max-age=7776000;
  add_header Access-Control-Allow-Origin *;
}
    try_files $uri $uri/ /index.html; # 使用 history 模式进行路由

    root     /zzz/zzz; # 打包后的 dist 根目录
    autoindex on;       # 开启 nginx 目录浏览功能
    autoindex_exact_size off;   # 文件大小从 KB 开始显示
    charset utf-8;          # 显示中文
    add_header 'Access-Control-Allow-Origin' '*'; # 允许来自所有的访问地址
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'GET, PUT, POST, DELETE, OPTIONS'; # 支持请求方式
    add_header 'Access-Control-Allow-Headers' 'Content-Type,*';
    add_header 'Access-Control-Allow-Headers' 'x_hztz_token,*';
    add_header Cache-Control 'no-cache, no-store, must-revalidate';
    add_header Pragma 'no-cache';
}

`

求解

@shawn2013
Copy link
Author

已解决,归根结底还是跨域配置问题。后台java服务也需要配置跨域,
add_header 'Access-Control-Allow-Origin' '*'; # 允许来自所有的访问地址
add_header 'Access-Control-Allow-Credentials' 'true';
冲突了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant