系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > server > anz > 详细页面

nginx部署vue项目,给访问路径加前缀的实现

时间:2022-12-10来源:www.pcxitongcheng.com作者:电脑系统城

Nginx安装与启动

去官网下载nginx压缩包,解压到电脑合适位置,我这放在D盘,目录是D:\nginx-1.21.6,


在这个路径,直接输入cmd,打开命令行,启动命令:

nginx.exe

或者

start nginx

关闭命令

taskkill /f /t /im nginx.exe

改了配置文件,不需要先关闭再启动,直接重启,重启命令

nginx -s reload

Vue增加访问路径

有时候会根据需要,区分不用的vue项目,这样要加一个前缀,不加前缀,访问是http://localhost:8080/,加一个前缀,cancer,访问路径就是http://localhost:8080/cancer
这个路径,在router/index.js修改配置,增加一个base

1
2
3
4
5
6
7
8
const router = new VueRouter({
  routes: routes.concat(asyncRouterMap),
  base: window.publicPath ? window.publicPath + "/" : "",
  mode:
    process.env.NODE_ENV === "production" || process.env.NODE_ENV === "test"
      ? "history"
      : "hash",
});

window.publicPath就是需要的前缀,window.publicPath = “/cancer”;

然后npm run build打包,把打包后的文件,在nignx路径下html文件夹下,新建一个文件夹,cancer,把包里的内容放进去

nginx配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
server {
     #前端启动端口监听
    listen       8780;
    #本机ip,也可以是域名
    server_name  192.168.2.87;
    location / { 
        root   html;
        index  index.html index.htm;
    }
    location /cancer {
        alias  html/cancer;
       index  index.html index.htm;
       try_files  $uri  $uri/   /cancer/index.html;
    }

分享到:

相关信息

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载