Vue3 和 TP6 前后端分离线上部署遇到的问题(跨域及访问404)

一、环境:

① 服务器  Nginx1.21 ,宝塔部署; 

② 前端 Vue3,后端 ThinkPHP6.1

 

二、情况:

① Vue3 和 TP6 前后端分离 及 前后端不一样的域名时 实现跨域请求问题 ;

② Vue3前端使用路由约束后,线上部署访问 404问题

 

三、方案:

1、跨域解决:

 因为我的前端配置做了代理转发 ,下图:

所以  服务器中宝塔的配置,这样才能正常代理成功,下图:

 # 配置与后端服务器地址的映射
     location ^~ /webapi/ {
        proxy_pass  https://192.168.31.119:8089/webapi/;
     }  

 

后端中还得在请求接口的请求头设置 :

   header('Access-Control-Allow-Origin:*');
   //允许的请求头信息
   header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization");
   //允许的请求类型
   header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE,OPTIONS,PATCH');
   //允许携带证书式访问(携带cookie)
   header('Access-Control-Allow-Credentials:true');

 

2、访问404

因为vue前端使用了路由策略,重写路由访问地址,如下代码:

import { createRouter, createWebHistory } from "vue-router";
import { defineAsyncComponent } from 'vue'; //异步组挂载器

const routes = [
  {
    path: "/",
    name: "home",
    component:() => import('../pages/home/index.vue'),
  },

  {
    path: "/project/apply",
    name: "projectApply",
    component:() => import('../pages/project/apply.vue'),
  },
]

export const router = createRouter({
  history: createWebHistory(),
  routes: routes
})

如果直接部署在线上服务器就会报404错误。

所以 在服务器中配置了:

 location / {
       try_files $uri $uri/ /index.html;
    }

 上面是把VUE前端部署到服务器后遇到的问题,因为遇到(部署)了两次了!所以这次记录下,下次部署有参考!

希望也能帮到大家!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
跨域问题是指前端页面向不同的源请求数据时,由于浏览器同源策略的限制,导致请求失败的问题。 在 Spring Boot 中,可以通过使用 Spring Security 解决跨域问题,同时在 Vue 中也可以通过配置代理解决跨域问题。 以下是 Spring Boot + Spring Security + Vue 前后端分离跨域的解决方法: 1. 启用 Spring Security 在 Spring Boot 项目中,可以通过使用 Spring Security 来添加安全性。可以通过在 pom.xml 文件中添加以下依赖来启用 Spring Security: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 2. 添加跨域配置 在 Spring Security 中,可以通过添加跨域配置来允许跨域请求。可以在 WebSecurityConfigurerAdapter 类中添加以下配置: ``` @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.cors().and().csrf().disable(); } @Bean public CorsConfigurationSource corsConfigurationSource() { CorsConfiguration configuration = new CorsConfiguration(); configuration.setAllowedOrigins(Arrays.asList("*")); configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE")); configuration.setAllowedHeaders(Arrays.asList("content-type")); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", configuration); return source; } } ``` 这里我们设置了允许任何来源的请求,允许 GET、POST、PUT、DELETE 方法的请求,并且允许 content-type 头部的信息。 3. 配置代理 在 Vue 中,可以通过配置代理来解决跨域问题。可以在 vue.config.js 文件中添加以下配置: ``` module.exports = { devServer: { proxy: { '/api': { target: 'http://localhost:8080', changeOrigin: true, pathRewrite: { '^/api': '' } } } } } ``` 这里我们设置了代理地址为 http://localhost:8080,将所有以 /api 开头的请求转发到代理地址,并且开启了 changeOrigin 选项,解决跨域问题。 现在我们就成功地解决了 Spring Boot + Spring Security + Vue 前后端分离跨域问题

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值