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. react抽离配置文件、配置@符号、调整src文件夹---配置scss、编写项目的页面结构、创建各个页面 src/views、开始路由、入口文件处修改代码、修改App.js布局文件、添加底部的导航布局、构建个人中心。。。声明式跳转路由、使用React UI库请求渲染首页数据、

    1.回顾 2.react项目的配置 react默认创建的项目配置文件在 node_modules/react-scripts 文件夹内部 2.1 抽离配置文件 cnpm run eject cnpm ...

  2. 网络抓包 tcpdump 使用指南

    在网络问题的调试中,tcpdump应该说是一个必不可少的工具,和大部分linux下优秀工具一样,它的特点就是简单而强大.它是基于Unix系统的命令行式的数据包嗅探工具,可以抓取流动在网卡上的数据包. ...

  3. [Linux]CentOS7搭建/配置:YUM仓库/源[本地源/Web源(Apache HTTP(D))/自建源仓库]

    若想搞懂整个配置过程和原理,就按照章节(1 / 2)一步一步地来. 若想直接一步到位,不想花过多时间,尽快配好,就直接看附件章节. 什么是yum源? Yum(全称为 Yellow dog Update ...

  4. 使用ServiceSelf解决.NET应用程序做服务的难题

    1 ServiceSelf 为.NET 泛型主机的应用程序提供自安装为服务进程的能力,支持windows和linux平台. 功能 自我服务安装 自我服务卸载 自我服务日志监听 2 自我服务安装 虽然. ...

  5. itext 生成pdf ----hello world

      iText是Java中用于创建和操作PDF文件的开源库.它是由Bruno Lowagie.Paulo Soares等人编写的.Ohloh报告称2001年以来[2],26个不同的贡献者进行了1万多次 ...

  6. LDAP数据过滤问题

    集成ldap同步用户遇到的问题: 首先说明同步需求: 业务需要只同步 objectClass 类型为user的用户 连接ldap查询用户的时候 过滤器只加了 .where("objectCl ...

  7. [Pytorch框架] 1.7 数据并行

    数据并行(选读) Authors: Sung Kim and Jenny Kang 在这个教程里,我们将学习如何使用 DataParallel 来使用多GPU. PyTorch非常容易就可以使用多GP ...

  8. 玩一玩 Ubuntu 下的 VSCode 编程

    一:背景 1. 讲故事 今天是五一的最后一天,想着长期都在 Windows 平台上做开发,准备今天换到 Ubuntu 系统上体验下,主要是想学习下 AT&T 风格的汇编,这里 Visual S ...

  9. cryptohack wp day(3)

    第二节模运算----第一题( GCD ) 在做这道题前,了解下欧几里得算法: 欧几里得算法,也叫辗转相除法,用于求解两个非负整数a和b的最大公约数(Greatest Common Divisor, G ...

  10. Web进阶LNMP网站部署

    Web进阶LNMP网站部署 目录 Web进阶LNMP网站部署 LNMP架构工作流程 部署LNMP架构 1.安装nginx 2.安装php 3.安装数据库 将Nginx和PHP建立连接 1.修改ngin ...