10.前端打包与nginx部署

文章目录

打包

首先,确保你的项目是可以运行的,以若依为例,运行npm run dev 是可以正常运行起来前端的。然后前端的打包命令是

复制代码
# 构建测试环境
npm run build:stage
# 构建生产环境
npm run build:prod

打包好之后,在项目代码路径下会出现一个dist文件夹,这里面的代码粘贴到nginx的html目录下即可。

部署

nginx

配置nginx,写好的配置文件如下:

复制代码
# 如果有前端上传文件的要求,需要加这个文件大小的限制,默认的大小不足以支撑项目需求。
client_max_body_size 200m;
server {
    # 使用docker将80映射成你想要的端口
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;
	# 对于前端的所有静态资源和界面,映射在nginx的html路径下
    location / {  
        root   /usr/share/nginx/html;
        try_files $uri $uri/ /index.html;
        index  index.html index.htm;
    }
    # 对于需要访问后端的字段,都是使用api开头的,走这里替换一下访问的路径
    location /prod-api/ {
			proxy_set_header Host $http_host;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header REMOTE-HOST $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_pass http://192.168.1.15:28000/;
		}
   
   # 对于其它的后端界面,通过这里配置对应的访问路径
    location /nvr-api/ {
			proxy_pass http://183.249.83.27:10800/;
		}

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

替换html

然后将打包好的dist目录丢在nginx的html下面。然后重启docker的nginx虚拟容器。

复制代码
sudo docker restart 容器id
相关推荐
无名修道院19 小时前
XSS 跨站脚本攻击:3 种类型(存储型 / 反射型 / DOM 型)原理以 DVWA 靶场举例
前端·网络安全·渗透测试·代码审计·xss
有谁看见我的剑了?19 小时前
Vmware vSphereClient为虚机安装vmtools
运维·云计算
代码猎人19 小时前
CSS可继承属性和不可继承属性有哪些
前端
用户447831536023219 小时前
基于 vue3 完成动态组件库建设
前端
数字冰雹19 小时前
从“被动响应”到“主动洞察”:数字孪生如何重塑数据中心运维
运维
网硕互联的小客服19 小时前
CC攻击对服务器正常运行会有什么影响?如何预防和解决CC攻击?
运维·服务器·网络·windows·安全
serve the people19 小时前
AI 模型识别 Nginx 流量中爬虫机器人的防御机制
人工智能·爬虫·nginx
xhxxx19 小时前
Vite + React 黄金组合:打造秒开、可维护、高性能的现代前端工程
前端·react.js·vite
Evan芙19 小时前
基于Nginx和Python的动态站点安装配置
数据库·python·nginx
中科米堆19 小时前
机器人企业采用自动化三维测量方案,完成关节部件快速检测-中科米堆CASAIM
运维·机器人·自动化·3d全尺寸检测