将publicPath改为"./",否则会出现静态文件找不到,从而使index.html文件打开空白页的问题

vs vue 打包docker_html

2、创建config.js文件,并引入index.html中,

创建该文件主要使为了,在vue打包后,后端服务更改了地址,前端就不需要重新打包,只需要更改打包后的config.js文件的baseUrl(打包后dist文件夹中也会有config.js文件),就能更改请求地址

vs vue 打包docker_服务器_02


config.js文件的内容

var ipConfig = {
    baseUrl: 'http://192.168.1.xxx:xxx'   //后端请求地址
  }

将config.js引入到public 的 index.js中

vs vue 打包docker_vs vue 打包docker_03

3、在发起请求的地方使用config.js中的地址

比如我这里,将axios请求封装起来,每次请求都使用这个封装的axios

如果你没有封装请求,可以在每个请求地址前都加上window.ipConfig.baseUrl

如:window.ipConfig.baseUrl + “/user/login”

vs vue 打包docker_vue.js_04

4、打包

使用命令:

npm run build

结果出先如下情况,则证明打包成功

vs vue 打包docker_服务器_05

5、打包后的文件结构如下

vs vue 打包docker_vue.js_06

二、部署到服务器中(Linux)

1、将打包后的dist文件夹放到服务器的某个文件下

我这里是放到了 /opt/Workspace/test_web

所以dist文件夹的路径为/opt/Workspace/test_web/dist,(记住这个路径,等下配置ngnix需要)

如果你不知道你的dist文件夹的路径在哪,你可以使用pwd命令

vs vue 打包docker_vs vue 打包docker_07

2、配置ngnix(请先确保你的服务器已经下载了ngnix)

打开ngnix配置文件,写入

server {
        listen       8099;    //端口,别人通过什么端口来访问你的前端页面
        server_name  localhost;  //填localhost就行,服务的ip
        root   /opt/Workspace/test_web/dist/;  #vue文件dist的完整路径 
        sendfile        on;
	    tcp_nopush      on;
	    default_type text/html;
	    location / {
			index  /index.html;
			try_files $uri $uri/ /index.html;
         }
	
		error_page   500 502 503 504  /50x.html;
		location = /50x.html {
			root   html;
		}
    }