Vue项目配置

使用Vite创建一个Vue项目,点我查看如何创建

配置打包路径

在Nginx中如果是二级目录,例如/web时,需要设置线上的打包路径

在项目跟路径下创建两个文件:.env.production.env.development,写入一下内容:

##生产环境
NODE_ENV = 'production'
VITE_BASE_PATH = /form-designer/
##开发环境
NODE_ENV = 'development'
VITE_BASE_PATH = '/'

vite.config.js中配置base属性,打开配置文件:

import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue' // https://vitejs.dev/config/
export default defineConfig(({ mode }) =>{
// 获取 .env 环境配置文件
const env = loadEnv(mode, process.cwd());
return {
base: env.VITE_BASE_PATH,
plugins: [vue()],
}
})

修改package.json,添加build命令:

"scripts": {
"dev": "vite",
"build": "vite build --mode production",
"preview": "vite preview"
},

配置路由

路由有两种模式:Hash和History

Hash模式

该模式下无需做过多配置

const router = createRouter({
history: createWebHashHistory(''),
routes,
....
});

history模式

该模式需要设置base路径

const router = createRouter({
history: createWebHistory(import.meta.env.VITE_BASE_PATH),
routes,
....
});

打包项目

使用下面命令打包

npm run build

压缩文件夹,方便上传到服务器进行打包部署

tar -zcvf dist.tar.gz dist/

Docker配置

工作系统中需要安装Docker环境,打包镜像用。这里以CentOS为例安装Docker

使用下面命令安装:

curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

出现命令提示,即为安装成功:

[root@swcode docker]# docker

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Common Commands:
run Create and run a new container from an image
exec Execute a command in a running container
ps List containers
build Build an image from a Dockerfile
pull Download an image from a registry
push Upload an image to a registry
images List images
login Log in to a registry
logout Log out from a registry
search Search Docker Hub for images
version Show the Docker version information
info Display system-wide information

配置Docker镜像源:

进入下面目录:

cd /etc/docker/

创建daemon.json,写入下面内容:

{
"registry-mirrors": ["http://hub-mirror.c.163.com","https://docker.mirrors.ustc.edu.cn","https://mirror.baidubce.com","https://registry.docker-cn.com"]
}

阿里云的镜像加速很好用的,点我获取加速地址

使配置生效

systemctl daemon-reload
systemctl restart docker

制作镜像

将文件上传到服务器上进行打包和发布,Dockerfile和要打包的文件需要在同一个目录下,确保服务器上已经安装Docker环境。

Dockerfile

# Docker image for vue application
# VERSION 1.0.0
# Author: swcode ### 基础镜像,使用nginx镜像
FROM nginx #作者
MAINTAINER swcode <2627311935@qq.com> #应用构建成功后的文件被复制到镜像内
COPY dist /usr/share/nginx/html/form-designer/ #拷贝.conf文件到镜像下,替换掉原有的nginx.conf
COPY nginx.conf /etc/nginx/nginx.conf #启动容器时的进程
ENTRYPOINT nginx -g "daemon off;"

Nginx配置

创建nginx.conf配置文件,基于location实现二级访问目录,修改配置信息:

worker_processes  1;

events {
worker_connections 1024;
} http {
include mime.types;
default_type application/json;
sendfile on;
keepalive_timeout 65; server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
} location ^~/form-designer {
alias /usr/share/nginx/html/form-designer;
index index.html index.htm;
# 路由为 history 模式需要设置该项
try_files $uri $uri/ /form-designer/index.html;
} # 代理服务
location /api {
default_type application/json;
#internal;
keepalive_timeout 30s;
keepalive_requests 1000;
#支持keep-alive
proxy_http_version 1.1;
# 路径重写 /api/user => /user
rewrite /api(/.*) $1 break;
proxy_pass_request_headers on;
proxy_next_upstream error timeout;
# Docker同一网络内部使用服务名访问
proxy_pass http://microService:8800;
} error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
} }
include servers/*;
}

注意!配置了root之后,其他的二级目录需要使用alias

如果是Docker部署,且后端项目和前端项目在同一自定义网络下(使用Docker Compose编排),代理地址使用服务名即可

上传文件

将dist.tar.gz上传到服务器,使用下面命令解压

tar -zxvf dist.tar.gz

确保下面三个文件在同一目录

Dockerfile
nginx.conf
dist/

build镜像

使用下面命令,build镜像

docker build -t form-designer .
  • form-designer:表示镜像名
  • ‘.’:表示当前目录

ngnix没有指定版本会自动拉取最新版,等待镜像build。。。

[root@swcode dockerfile]# docker build -t form-designer .
[+] Building 0.1s (8/8) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 469B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/nginx:latest 0.0s
=> [1/3] FROM docker.io/library/nginx 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 1.15kB 0.0s
=> CACHED [2/3] COPY dist /usr/share/nginx/html/form-designer/ 0.0s
=> [3/3] COPY nginx.conf /etc/nginx/nginx.conf 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:3212e45813e7d278aa33982cc7373e55418b9a3ed65c0249b8fd55c70bf6ee32 0.0s
=> => naming to docker.io/library/form-designer

使用docker命令查看镜像

[root@swcode dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
form-designer latest 372e067b2c6a 48 minutes ago 190MB

运行容器

使用下面命令运行容器:

docker run --name form-designer-web -p 81:80 -d form-designer
  • form-designer-web:是容器名
  • form-designer:是镜像名
  • -p 81:80:表示服务器端口(外部)81映射到容器内部端口80

使用docker命令查看容器

[root@swcode dockerfile]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
65f0979310a1 form-designer "/bin/sh -c 'nginx -…" 48 minutes ago Up 48 minutes 0.0.0.0:81->80/tcp form-designer-web

访问网站

访问网址:http://ip:81/form-designer

其他命令

删除镜像

docker rmi [镜像名 | 镜像ID]

删除容器

docker rm -f [容器名 | 容器ID]

使用Docker将Vite Vue项目部署到Nginx二级目录的更多相关文章

  1. 【Copy攻城狮日志】docker搭建jenkins拉取svn代码打包vue项目部署到nginx

    ↑开局一张图,故事全靠编↑ 前言 打开搜索引擎输入『Copy攻城狮』,发现最新的一条记录已经是去年的4月,意味着我又有一年时间没有再总结成长了.习惯了“温水煮青蛙”的日子,无论是经验水平还是薪资收入, ...

  2. 将Vue项目部署到Nginx中,出现的400,405,200响应空等问题处理

    最近用Vue3写了个项目,然后对接后台接口. 在本地vue配置文件中,配置了反向代理.成功请求了后端接口. 自测没有问题. 打包vue,发布到nginx中.运行nginx,成功显示了页面. 当点击页面 ...

  3. vue 项目部署到nginx

    第一步在控制台终端输入npm run build 打包完成之后项目中会生成一个dist文件夹,直接访问里面的index.html就ok了 第二步配置nginx 第三步重启nginx service n ...

  4. 如何将Vue项目部署到Nginx 服务器中

    https://blog.csdn.net/qq_35366269/article/details/91385689

  5. node vue 项目部署问题汇总

    场景:vue-router为history模式,不带项目名访问的部署,如果资源是用相对路径加载,则资源匹配路径不对 一.带项目名称访问,如部署到tomcat服务上 webpack:  build/ut ...

  6. Vue项目部署问题及解决方案

    Vue项目部署问题及解决方案 Vue-Router 有两种模式,默认是 hash 模式,另外一种是 history 模式. hash:也就是地址栏里的 # 符号.比如 http://www.examp ...

  7. vue 项目部署

    vue项目部署到PHP项目 入口目录 vue项目打包后, 是一个单文件html 我们只需要把打包后的文件夹放在php项目的public下面 访问 xxx.com/h5/index.html 就可以访问 ...

  8. 阿里云安装Nginx+vue项目部署

    阿里云安装Nginx+vue项目部署 nginx安装包下载 http://nginx.org/en/download.html nginx安装 首先先安装PCRE pcre-devel 和Zlib,因 ...

  9. django项目、vue项目部署云服务器

    目录 上线架构图 服务器购买与远程连接 安装git 安装mysql 安装redis(源码安装) 安装python3.8(源码安装) 安装uwsgi 安装虚拟环境 安装nginx(源码安装) vue项目 ...

  10. vue项目部署后页面加载首次很慢的优化方案

    参考: vue项目首次加载特别慢需要怎么配置? 1.看看你的依赖包是不是全局引入的,改为组件内按需引入,可大大降低加载时长.或者将组件引入方式改为cdn引入.需要注意的是,两种引入方式不能共存. 2. ...

随机推荐

  1. ARC149(A~E)

    Tasks - AtCoder Regular Contest 149 又是114514年前做的题,现在来写 屯了好多,清一下库存 A - Repdigit Number (atcoder.jp) 直 ...

  2. 面对AI的兴起,从人类发展到个人发展,普通人应当如何抉择?

    这一周被各种 AI 卷的不行,从 ChatGPT 4.0 上线到百度文心一言发布会,再到微软的 Microsoft 365 Copilot. 网上有很多人.公众号吐嘈百度,而晓衡接触到的圈子还有一些不 ...

  3. 更新pip遇到的问题及解决方法

    问题ython -m pip install --upgrade pip --user D:\APP\Anaconda3\python.exe: No module named pip解决方法 pyt ...

  4. 1.封装PageHelper实现分页

    前言 这几天想着动手将一些技术融合到项目中,昨天思考了会儿,想起了我与亲戚的对话:我说:"我想将若依项目完整的实现一遍",亲戚给我反馈到"你没必要完整复现若依项目,而且你 ...

  5. Thread面试题

    面试题目录https://www.cnblogs.com/Kaelthas/p/15005844.html 1.一个Thread对象代表一个线程,同一个线程能否多次启动? 不能,在Thread类中变量 ...

  6. VMware另一个程序锁定文件的一部分,进程无法访问

    问题描述:搭建RAC11g,在做共享磁盘的时候,节点2要共享节点1的磁盘,但是有一个问题,节点2关机之后,再打开,是有一个访问节点1的磁盘的过程,如果访问失败,就会开不了机器 rac1加的三个磁盘: ...

  7. Go语言实战: 即时通信系统(未完)

    使用Go语言构建一个即时通信系统,旨在锻炼Go语言编程能力 该通信系统至少能够允许用户能够在客户端进行公聊,即所发消息能被所有用户看到,也可发起私聊(即两个用户之间私密通信).同时,用户能够看到当前有 ...

  8. Shell在日常工作中的应用实践

    作者:京东物流 李光新 1 Shell可以帮我们做什么 作为一名测试开发工程师,在与linux服务器交互过程中,大都遇到过以下这些问题: •一次申请多台服务器,多台服务器需要安装相同软件,配置相同的环 ...

  9. Python用哈希算法查找相似图片(包括不同分辨率,不同大小,不同格式的图片)

    # -*- coding: utf-8 -*- ''' Python用哈希算法查找相似图片并放入[_df]的文件夹中 相似图片包括不同分辨率,不同大小,不同格式,只要图片相似就会算重复文件 安装cv2 ...

  10. git拉取代码总提示输入密码解决方法

    公司用的gitlab,在项目拉取(git clone)和更新(git pull)的时候,每次都提示输入用户名密码,不胜其烦,解决方法如下: # 首先 git config --global crede ...