使用Docker将Vite Vue项目部署到Nginx二级目录

这篇具有很好参考价值的文章主要介绍了使用Docker将Vite Vue项目部署到Nginx二级目录。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

vite docker,docker,vue.js,nginx文章来源地址https://www.toymoban.com/news/detail-771280.html

其他命令

删除镜像

docker rmi [镜像名 | 镜像ID]

删除容器

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

到了这里,关于使用Docker将Vite Vue项目部署到Nginx二级目录的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Docker使用nginx部署VUE项目

    目录 一、安装Docker和一些基本Docker命令 二、打包VUE项目 三、写nginx配置文件 四、写Dockerfile文件 五、构建镜像 六、运行容器 七、挂载目录 八、使用docker-compose部署项目 总结 安装略,基本指令如下: vue项目根目录进入终端输入npm run build打包项目,在项目根目录会生成一个

    2024年02月07日
    浏览(35)
  • 解决Vue3+Vite3 打包部署到nginx后配置非根目录刷新页面报错空白

    报错内容 解决方法 router文件 vite.config.ts nginx.conf 配置中路径apps是我自建的存放前端页面的文件夹 起关键作用的是 try_files $uri $uri/ /demo/index.html ,当然上面项目文件夹demo也需保持一致 alias 后面的路径是Vue项目打包后dist静态文件服务器存放路径,一般在nginx下面建一个文件夹

    2024年02月11日
    浏览(32)
  • windows 搭建docker 以及部署前后端项目及程序,对所需目录挂载nginx进行配置显示

    1.我们在电脑上进行搜索 ,启用或者关闭windows功能 2. 如果没有 Hype-V的话,具体添加步骤如下: 在本地创建一个.bat文件,内容为:  保存为.bat文件,然后右键,以管理员身份运行。 等待安装好之后,最后一步点击Y。完成安装。 然后以管理员身份运行powershell.这个其实就是

    2024年02月08日
    浏览(28)
  • 【Vue3实践】(六)Vue3使用vite处理环境变量、打包部署、nginx配置

    由于在日常开发中会有一部分前端的开发任务,会涉及到Vue的项目的搭建、迭代、构建发布等操作,所以想系统的学习一下Vue相关的知识点,本专题会依照Vue的搭建、开发基础实践、进阶用法、打包部署的顺序进行记录。 历史文章链接如下: 《Vue3搭建、路由配置与基本语法

    2023年04月08日
    浏览(41)
  • Docker 方式 部署 vue 项目 (docker + vue + nginx)

    1.安装好 nginx 。 2. 把 vue 项目的源码克隆到确定目录下。用 git 管理,所以直接 git clone 到既定目录就行了。 如我的目录是:/root/jiangyu/projects/gentle_vue/gentle_vue_code 。 3. 项目打包: 复制 会自动生成 dist 文件夹 。 4. 在任意目录下新建文件 dockerfile 。内容如下: 复制 5. 构建镜

    2024年02月13日
    浏览(31)
  • 【Docker】docker镜像+nginx部署vue项目:

    一、文档: 【1】菜鸟教程:https://www.runoob.com/docker/docker-tutorial.html 【2】Docker部署Vue项目的项目实践:https://www.jb51.net/server/292938nb9.htm 【3】Docker部署vue项目:https://www.cnblogs.com/newcapecjmc/p/16443866.html 二、打包vue项目: 三、配置nginx: 四、配置Dockerfile: 五、构建镜像: 六、运

    2024年02月14日
    浏览(53)
  • 【Docker】docker部署springboot+vue+mysql+nginx前后端分离项目【部署实战篇】

    安装docker: https://blog.csdn.net/qq_39900031/article/details/121666892 springboot-vue前后端分离项目:https://gitee.com/ma-haojie/springboot-vue-demo.git https://jackwei.blog.csdn.net/article/details/110227719 或者 --restart=always 参数能够使我们 在重启docker时,自动启动相关容器 。 Docker容器的重启策略如下: no,默认

    2024年02月13日
    浏览(34)
  • 2-Docker-应用-多容器部署Django+Vue项目(nginx+uwsgi+mysql)

    基于Linux CentOS 7系统(虚拟机),使用Docker,多容器部署Django+Vue项目 整体部署用到了:Django+Vue+nginx+mysql+uwsgi 先每一个容器单独部署,最后用Docker compose 语法整合,统一部署 参考文章:https://blog.csdn.net/qq_45445505/article/details/135563784 章标题:Docker介绍 节标题:Docker安装 总结梳

    2024年03月10日
    浏览(84)
  • docker使用nginx部署vue刷新页面404

    从docker内部复制出来的配置文件是这样的,但是刷新页面之后就显示404,关键是我两个前端项目都是用的这一个配置文件,但是只有一个项目出现刷新浏览器显示404的问题,这给我搞懵了!!! 在网上查找问题原因和解决办法,但是大多数都说是资源找不到,让修改配置文件

    2024年01月18日
    浏览(35)
  • 【Docker】使用Docker安装Nginx及部署前后端分离项目应用

            Nginx是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。它是由 伊戈尔·赛索耶夫 为俄罗斯访问量第二的Rambler.ru站点开发的,公开版本1.19.6发布于2020年12月15日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、简单的配置

    2024年01月22日
    浏览(37)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包