1、安装nginx服务器:
(1)下载nginx安装包并解压:下载链接;
(2)进入nginx解压目录中编译安装:
./configure --prefix=/usr/local/nginx(必须先建好目录)
make
make install
(3)进入
usr/local/nginx/sbin
目录运行nginx:./nginx
,访问80端口即可看到nginx启动成功;
2、打包vue项目:
(1)修改config/index.js文件中的静态资源路径:
assetsPublicPath: './',
(2)打包:npm run build,在项目目录下即可看到打包后的dist目录,用浏览器打开该目录下的index.html文件即可看到效果;
3、Vue Router的两种方式(hash、history)。
4、配置nginx:
(1)修改nginx安装目录下的配置文件(usr/local/nginx/conf/nginx.conf
):
# 工作进程数
worker_processes 1;
# 工作模式与连接数设置
events {
# 参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];
use epoll;
# 单进程最大连接数
worker_connections 1024;
}
http {
# mime与文件类型映射
include mime.types;
# 默认文件类型
default_type text/html;
# 关闭日志
#access_log off
# 日志格式化
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 方式传输文件
sendfile on;
# 连接超时时间,o 表示无限制
keepalive_timeout 65;
##################### vue项目部署 ###################################
# vue项目打包后的dist目录位置
root /home/lyq/Desktop/vue-learn/dist;
server {
location / {
# hash模式只配置访问html
# index index.html;
# history模式下
try_files $uri $uri/ /index.html;
# try_files $uri $uri/ @router;
}
error_page 500 503 504;
location = /50x.html {
root html;
}
}
}
(2)刷新nginx配置并重启:./nginx -s reload;
(3)访问localhost:80
即可看到页面。