小知识:尝试Docker+Nginx部署单页应用方法

开发到部署,亲力亲为

当我们开发一个单页面应用时,执行完构建后

?
1
npm run build

会生成一个 index.html 在 dist 目录,那怎么把这个 index.html 部署到服务器上呢?

目录结构

dist/:前端构建完的静态文件 docker/:镜像所需的配置文件

%小知识:尝试Docker+Nginx部署单页应用方法-猿站网-插图

配置 nginx

挑几点配置讲讲,先是 gzip 压缩资源,以节省带宽和提高浏览器加载速度

虽然 webpack 已经支持在构建时就生成 .gz 压缩包,但也可以通过 nginx 来启用
?
1
2
3
4
5
6
7
gzip on;
gzip_disable “msie6”;
# 0-9 等级,级别越高,压缩包越小,但对服务器性能要求也高
gzip_comp_level 9;
gzip_min_length 100;
# gzip 不支持压缩图片,我们只需要压缩前端资源
gzip_types text/css application/javascript;

再就是服务端口的配置,将 api 反向代理到后端服务

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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;
}
}

完整配置长这样

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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 到镜像内

?
1
2
3
4
from nginx:alpine
copy nginx.conf /etc/nginx/nginx.conf
copy dist /usr/share/nginx/html

编写 makefile

完成了上面的准备,就可以编写命令来执行镜像的打包了

先给镜像取个名称和端口号

?
1
2
app_name = spa_nginx_docker
port = 8080

通过 build 来打包镜像

?
1
2
3
4
5
6
build:
cp docker/dockerfile .
cp docker/nginx.conf .
docker build -t $(app_name) .
rm dockerfile
rm nginx.conf

通过 deploy 来启动镜像

?
1
2
deploy:
docker run -d -it -p=$(port):$(port) –name=”$(app_name)” $(app_name)

最后还有个 stop 来停止和清理镜像

?
1
2
3
4
stop:
docker stop $(app_name)
docker rm $(app_name)
docker rmi $(app_name)

完整配置长这样

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
app_name = spa_nginx_docker
port = 8080
build:
cp docker/dockerfile .
cp docker/nginx.conf .
docker build -t $(app_name) .
rm dockerfile
rm nginx.conf
deploy:
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)

完整命令长这样

?
1
2
3
4
5
6
7
8
9
10
11
# 静态资源构建
npm run build
# 镜像打包
make build
# 停止并删除旧镜像(首次可忽略)
make stop
# 镜像启动
make deploy

总结

目前的部署方法相对简单,后续会加入基础镜像和镜像仓库的使用,先去前面探探路

原文链接:https://segmentfault.com/a/1190000018268971

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:Docker安装官方Redis镜像并启用密码认证

2023-4-3 18:40:39

建站知识

小知识:Docker数据存储总结

2023-4-3 18:55:13

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索