当前位置: 首页 > news >正文

nginx部署vue项目访问路径问题

部署后,不管页面空白还是报错,实际上都是资源引用的路径不对造成的,根据路径来实际解决就可以了

history模式:

vue中使用history模式加上NGINX的使用会导致路径冲突,解决如下:

	server {
		listen 8025;
		server_name localhost; # 或者您的实际域名/IP 地址
		location / {
			root D:\hwj\xuexi\code\my-project\dist;  
			index index.html;
			try_files $uri $uri/ /index.html;  # 解决 Vue 路由刷新 404 问题
		}
	}

vue.config.js (关键:publicPath: '/', //设置为绝对路径)

const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
  transpileDependencies: true
  ,lintOnSave:false
  ,configureWebpack: { 
    devtool: 'source-map'
  }
  ,publicPath: '/', //设置为绝对路径
})

public目录下的index.html (关键点:<%= BASE_URL %>)

<!DOCTYPE html>
<html lang="">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <link rel="icon" href="<%= BASE_URL %>favicon.ico">
    <title><%= htmlWebpackPlugin.options.title %></title>
    <link rel="stylesheet" href="<%= BASE_URL %>layui-v2.9.20\layui\css\layui.css">
    <script src="<%= BASE_URL %>layui-v2.9.20\layui\layui.js"></script>
    <script src="<%= BASE_URL %>jquery-3.6.0.min.js"></script>
  </head>
  <body>
    <noscript>
      <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
    </noscript>
    <div id="app"></div>
    <!-- built files will be auto injected -->
  </body>
</html>

hash模式:

hash模式就没那么多事了,就是路径有点不好看

NGINX中配置如下:(好处:在同一个域名下的80端口,我可以配置test请求是访问前端,detail请求就是访问后端。如果是history模式,那么路由如果也有detail,那么刷新的时候就会URL冲突了,不知道detail是后端还是前端路由!)

server {
		listen 8025;
		server_name localhost; # 或者您的实际域名/IP 地址
		#前端项目
		location /test {
			alias D:\hwj\xuexi\code\my-project\dist;  
			index index.html;
            try_files $uri $uri/ /test/index.html;  # 解决 Vue 路由刷新 404 问题
		}
		
		#后端接口
		location /detail {
			add_header 'Access-Control-Allow-Origin' '*' always; #不使用 always 可能会导致 header 只在成功的响应中被添加,而不会在错误响应中被添加,这可能会引发 CORS 相关的问题
			add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
			add_header 'Access-Control-Allow-Headers' '*';
			add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
			if ($request_method = 'OPTIONS') {
                  return 204;
            }
			proxy_pass http://192.168.1.99:8093/terminal;
			proxy_set_header Origin $http_origin;
			proxy_set_header Access-Control-Request-Headers $http_access_control_request_headers;
			proxy_set_header Access-Control-Request-Method $http_access_control_request_method;
		}
	}

vue.config.js中也要改动下

const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
  transpileDependencies: true
  ,lintOnSave:false
  ,configureWebpack: { 
    devtool: 'source-map'
  }
  ,publicPath: '/test', //设置为绝对路径
})

相关文章:

  • MATLAB联动本地部署的DeepSeek模型
  • JAX-RS与JAXB:实现XML数据交互的完整指南
  • 基于MATLAB的城轨车辆跨接电缆长度计算
  • 青少年编程与数学 02-009 Django 5 Web 编程 20课题、测试
  • 68页PDF | 数据安全总体解决方案:从数据管理方法论到落地实践的全方位指南(附下载)
  • 编码格式大全:类型 特点及其在网络安全中的作用
  • STM32 PWM脉冲宽度调制介绍
  • python股票分析系统部署操作过程及代码实现
  • opensuse [Linux] 系统挂在新的机械硬盘
  • Ubuntu 20 掉显卡驱动的解决办法
  • 设计模式-工厂模式
  • go语言并发的最佳实践
  • 【LeetCode Hot100】最大子数组和|动态规划/贪心,Java实现!图解+代码,小白也能秒懂!
  • 【人工智能 | 大数据】基于人工智能的大数据分析方法
  • 【nvidia】NCCL禁用P2P后果权衡
  • 【C++指南】不允许你不了解C++命名空间
  • 视点坐标及鼠标交点坐标的信息显示(七)
  • PapaParse 用于解析 CSV 和 TSV 文件 JavaScript 库
  • 基于flask+vue的租房信息可视化系统
  • [数据结构]红黑树,详细图解插入
  • 在本轮印巴冲突的舆论场上也胜印度一筹,巴基斯坦靠什么?
  • 端午小长假前夜火车票今日开抢,多个技巧提高购票成功率
  • 上海市税务局:收到对刘某某存在涉税问题的举报,正依法依规办理
  • 前四个月社会融资规模增量累计为16.34万亿元,比上年同期多3.61万亿元
  • 加强战略矿产出口全链条管控将重点开展哪些工作?商务部答问
  • 第十届影像上海博览会落幕后,留给中国摄影收藏的三个问题