nuxt做的前端,首页使用了SSR,其它页面使用了CSR,
前端服务器配置:
proxy_pass http://localhost:3000
后端服务器是laravel做的
在后端服务器打印请求ip, 有的请求ip 是 前端服务器的, 有的ip 是客户端的,
这种情况下如何设置跨域,只允许前端服务器 或 是自己网站发出的请求 才可以请求到后端服务器
引用gpt回答
对于Nuxt混合使用SSR和CSR的场景,解决跨域问题的方法可以是:
axios: {
proxy: true
},
proxy: {
'/api': {
target: 'http://localhost:3000',
changeOrigin: true
}
}
public function handle($request, Closure $next)
{
header("Access-Control-Allow-Origin: http://nuxtserver");
// Allow only GET, POST, PUT, DELETE
if ($request->getMethod() === "OPTIONS") {
return response()->json('OK', 200);
}
return $next($request);
}