Vue3 项目打包并部署到Nginx
一、安装Nginx
官网下载链接:
nginx: downloadhttps://nginx.org/en/download.htmlhttps://nginx.org/en/download.html
下载后解压并双击 nginx.exe 启动服务:
打开浏览器,访问 http://localhost/ ,若出现如下页面,表示安装成功:
若想停止服务,命令行运行:
nginx.exe -s stop
二、打包并部署vue项目
1、vite.config.js/vite.config.ts添加build属性
build: {
sourcemap: false, // 不生成 source map
terserOptions: {
compress: { // 打包时清除 console 和 debug 相关代码
drop_console: true,
drop_debugger: true,
},
},
},
2、终端执行 npm run build
build 完成后会生成一个 dist 文件夹。
3、部署到Nginx
将 dist 文件夹移动到 Nginx 目录下,可以重命名为自己想要的名字,此处重命名为 vue-test:
接下来需要修改一下 conf 目录下 nginx.conf 文件的配置。
自定义设置ip和端口:
listen 5173;
server_name localhost;
设置要启动的服务为 vue-test ,同时为了防止刷新页面时报错404,新增 try_files $uri $uri/ /index.html; 这条配置:
location / {
root vue-test;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
另外再加上下面这条配置,同样是为了防止刷新页面时出现404找不到页面:
location @router {
rewrite ^.*$ /index.html last;
}
修改完成后整个 nginx.conf 文件的内容如下:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 5173;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root vue-test;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location @router {
rewrite ^.*$ /index.html last;
}
#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 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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
完成上述所有配置后,启动Nginx,浏览器访问刚刚自定义的ip+端口,按道理就能够正常进入前端页面了。
三、刷新页面空白问题
如果按照上面过程打包部署到Nginx后,刷新页面后页面就变成空白,可尝试如下修改:
vite.config.js:
// 防止打包后点击刷新后页面空白
publicPath: process.env.NODE_ENV === 'production' ? './' : '/',
router.js:
history: createWebHashHistory(process.env.BASE_URL),
原文地址:https://blog.csdn.net/scongx/article/details/145305511
免责声明:本站文章内容转载自网络资源,如侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!