尝试Docker+Nginx部署单页应用方法
发表于:2024-09-28 作者:千家信息网编辑
千家信息网最后更新 2024年09月28日,开发到部署,亲力亲为当我们开发一个单页面应用时,执行完构建后npm run build会生成一个 index.html 在 dist 目录,那怎么把这个 index.html 部署到服务器上呢?目录结
千家信息网最后更新 2024年09月28日尝试Docker+Nginx部署单页应用方法
开发到部署,亲力亲为
当我们开发一个单页面应用时,执行完构建后
npm run build
会生成一个 index.html 在 dist 目录,那怎么把这个 index.html 部署到服务器上呢?
目录结构
- dist/:前端构建完的静态文件
- docker/:镜像所需的配置文件
配置 Nginx
挑几点配置讲讲,先是 Gzip 压缩资源,以节省带宽和提高浏览器加载速度
虽然 Webpack 已经支持在构建时就生成 .gz 压缩包,但也可以通过 Nginx 来启用
gzip on;gzip_disable "msie6";# 0-9 等级,级别越高,压缩包越小,但对服务器性能要求也高gzip_comp_level 9;gzip_min_length 100;# Gzip 不支持压缩图片,我们只需要压缩前端资源gzip_types text/css application/javascript;
再就是服务端口的配置,将 API 反向代理到后端服务
server { listen 8080; server_name www.frontend.com; root /usr/share/nginx/html/; location / { index index.html index.htm; try_files $uri $uri/ /index.html; # 禁止缓存 HTML,以保证引用最新的 CSS 和 JS 资源 expires -1; } location /api/v1 { proxy_pass http://backend.com; }}
完整配置长这样
worker_processes 1;events { worker_connections 1024; }http { ## # Basic Settings ## sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; ## # Logging Settings ## access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; ## # Gzip Settings ## gzip on; gzip_disable "msie6"; gzip_comp_level 9; gzip_min_length 100; gzip_types text/css application/javascript; server { listen 8080; server_name www.frontend.com; root /usr/share/nginx/html/; location / { index index.html index.htm; try_files $uri $uri/ /index.html; expires -1; } location /api/v1 { proxy_pass http://backend.com; } }}
配置 Docker
这里简单一点,基于基础镜像,拷贝我们写好的 nginx.conf 和 index.html 到镜像内
FROM nginx:alpineCOPY nginx.conf /etc/nginx/nginx.confCOPY dist /usr/share/nginx/html
编写 Makefile
完成了上面的准备,就可以编写命令来执行镜像的打包了
先给镜像取个名称和端口号
APP_NAME = spa_nginx_dockerPORT = 8080
通过 build 来打包镜像
build: cp docker/Dockerfile . cp docker/nginx.conf . docker build -t $(APP_NAME) . rm Dockerfile rm nginx.conf
通过 deploy 来启动镜像
deploy: docker run -d -it -p=$(PORT):$(PORT) --name="$(APP_NAME)" $(APP_NAME)
最后还有个 stop 来停止和清理镜像
stop: docker stop $(APP_NAME) docker rm $(APP_NAME) docker rmi $(APP_NAME)
完整配置长这样
APP_NAME = spa_nginx_dockerPORT = 8080build: cp docker/Dockerfile . cp docker/nginx.conf . docker build -t $(APP_NAME) . rm Dockerfile rm nginx.confdeploy: docker run -d -it -p=$(PORT):$(PORT) --name="$(APP_NAME)" $(APP_NAME)stop: docker stop $(APP_NAME) docker rm $(APP_NAME) docker rmi $(APP_NAME)
完整命令长这样
# 静态资源构建npm run build# 镜像打包make build# 停止并删除旧镜像(首次可忽略)make stop# 镜像启动make deploy
总结
目前的部署方法相对简单,后续会加入基础镜像和镜像仓库的使用,先去前面探探路
镜像
配置
资源
服务
前端
命令
基础
文件
服务器
目录
静态
开发
支持
生成
方法
应用
亲力亲为
仓库
口号
可以通过
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
数据库找到软件
服务器 安全审核策略
无线传感器网络技术路线
ntis 数据库
软件开发毕业专业技能
主从数据库安装配置
三级数据库考试怎么考
网络技术适合的笔记本
网络安全网络产品和服务
昆明智能化软件开发价格走势
怎么删除淘宝相同数据库
彩票自动生成期号写入数据库
怎么用服务器做监控录像机
新修订的网络安全审查办法
用友erp数据库金额单位
服务器机柜一般多少钱
荣昌耀华网络技术有限公司
mongodb 数据库大小
聚焦互联网科技股
奉贤区一站式软件开发出厂价格
网络技术基础 胡远萍
英灵神殿队友连不上服务器
网络安全科技馆设计
八爪鱼采集数据到本地数据库
网络安全清查电脑简报
用友文件服务器配置
公路局推进网络安全活动
网络安全具有什么能力
数据库领域的技术专家
主题网络安全教育