1. 修改router.js
设置mode 为 history
需要配置当未匹配到路由时,跳转到的404页面
const router = new VueRouter({
mode: 'history',
routes: [
{
path: '/',
name: 'home',
component: () => import('./views/Home'),
meta: { title: '自述文件' },
children: [
{
path: '404',
name: '404',
component: () => import('./views/404'),
meta: { title: '404' }
}
]
},
{
path: '*',
redirect: '/404'
}
]
})
2. 在项目根目录添加vue.config.js
module.exports = {
publicPath: './',
// assetsDir: 'static', // 静态资源打包后所在文件夹
productionSourceMap: false,
//webpack配置
configureWebpack: {
//关闭 webpack 的性能提示
performance: {
hints: false
}
},
devServer: {
proxy: {
'/api': {
target: process.env.VUE_APP_DOMAIN,//请求域名
//secure: false, // 如果是https接口,需要配置这个参数
changeOrigin: true,//如果是跨域访问,需要配置这个参数
pathRewrite: {
'^/api': ''
}
}
}
},
}
3. nginx.conf文件配置
设置mode 为 history
#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 3000; #配置nginx监听端口
server_name localhost; #服务器域名地址匹配
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root C:\JavaDEP\VSCodeProjects\vue-practice\dist; #这里需要配置为项目部署后所在路径
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
#api接口请求的转发地址
location /api {
proxy_pass http://127.0.0.1:8080/api;
}
#未知路径时跳转到404页面,需前端配置/404路由
error_page 404 /404;
# 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;
# }
#}
}