关于vue3和thinkphp8部署在宝塔时访问thinkphp public静态文件存在跨域或者是请求数据存在跨域的解决方法

前端打包后上传到后端的public目录下,注意dist目录里面的所有文件都要上传,其中包含dist本身.

前端访问:http://域名/index.html

前后端统一域名网址设置伪静态-设置后重启nginx

# 如果是访问api的,则由thinkphp处理
location /api {
	if (!-e $request_filename){
		rewrite  ^(.*)$  /index.php?s=$1  last;   break;
	}
}
 
# 其他的由vue处理,并重新设置根目录为public下的dist,优先访问index
location / {
	root   /www/wwwroot/api.xtgmf.ltd/public/dist;
	index  index.html index.htm;
	try_files $uri $uri/ /index.html;
}

前后端分离配置伪静态

location ~ \.php(.*)$ {
    add_header Access-Control-Allow-Origin *;
    add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
    add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
    add_header Access-Control-Max-Age: 86400;
    if ($request_method = 'OPTIONS') {
        return 204;
    }
}

location /uploadFiles {
    add_header 'Access-Control-Allow-Origin' $http_origin;
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
    add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
    if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain; charset=utf-8';
        add_header 'Content-Length' 0;
        return 204;
    }
}       

安装系统环境

  • centos8及以上
  • nodejs18及以上
  • php8及以上
  • redis
  • mysql
  • php拓展:fileinfo、redis

最近更新

  1. TCP协议是安全的吗?

    2024-03-27 20:50:02       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-27 20:50:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-27 20:50:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-27 20:50:02       20 阅读

热门阅读

  1. 自动化测试理论基础(超详细)

    2024-03-27 20:50:02       19 阅读
  2. 态势感知平台简单介绍

    2024-03-27 20:50:02       21 阅读
  3. ChatGPT编程实践指南:让AI成为你的代码良师

    2024-03-27 20:50:02       17 阅读
  4. LeetCode刷题记录——day7

    2024-03-27 20:50:02       20 阅读
  5. Qt5.14.2 全面解锁Qt5事件处理的精髓

    2024-03-27 20:50:02       18 阅读
  6. Hutool工具包中的Validator类数据校验

    2024-03-27 20:50:02       18 阅读
  7. Mysql中的执行计划怎么分析?

    2024-03-27 20:50:02       19 阅读