Docker+Nginx如何部署单页应用
发表于:2024-09-28 作者:千家信息网编辑
千家信息网最后更新 2024年09月28日,本篇内容介绍了"Docker+Nginx如何部署单页应用"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成
千家信息网最后更新 2024年09月28日Docker+Nginx如何部署单页应用
本篇内容介绍了"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
"Docker+Nginx如何部署单页应用"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
镜像
配置
资源
服务
应用
内容
前端
命令
文件
更多
服务器
目录
知识
静态
开发
支持
生成
实用
亲力亲为
学有所成
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
四十岁苏州软件开发找工作
端游泰拉瑞亚服务器端口
视图攻击如何连接本地数据库
陕西华表网络技术
我的世界进服务器就闪退怎么解决
连认证手机号的无线网络安全吗
汤森路透数据库邮箱
php项目数据库连接
网络安全学校宣传手册
异构服务器概念
网络安全网络产品和服务
数据库系软件多少钱
服务器数据库系统的应用基础
金蝶k3数据库表结构
天翼云服务器ip限制
新乡金蝶中国软件开发
黑苹果软件开发
南沙点金互联网科技有限公司
流媒体服务器 srs
奉贤区一站式软件开发出厂价格
服务器刷号
e740服务器
安装了服务器安全狗如何清除密码
工行软件开发中心试用期
大通区网络安全与保密
博涵网络技术有限公司
用友NC支持什么数据库
数据接口改为数据库表
为网络安全活动公关策划范文
聚力筑牢网络安全屏障和信息化