vue 项目限制某页面路由只能由某服务器ip访问,其他的不能访问

1. 由于nginx 的内置变量只能 路径带有# 后面都获取不到,所以要想满足这个要求必须改成histroy模式

2.再nginx 中再配置就可以

具体做法:
一.vue 由hash 改成histroy

1.在router中

const createRouter = () => new Router({
  mode: 'history', // require service support
  base:'/demo/',
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

2.router 中的base需要和vue.config.js中的publicPath 保持一致

复制代码
module.exports = {
  /**
   * You will need to set publicPath if you plan to deploy your site under a sub path,
   * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
   * then publicPath should be set to "/bar/".
   * In most cases please use '/' !!!
   * Detail: https://cli.vuejs.org/config/#publicpath
   */
  publicPath: '/demo/',
  outputDir: 'dist',
  assetsDir: 'static',
.......
复制代码

3.修改nginx

复制代码
location ~* /test/abc {
                root /home/;
                allow 192.168.194.53;
                deny all;
                try_files $uri $uri/ /demo/index.html;
        }

        location / {
                root /home/;
                try_files $uri $uri/ /demo/index.html;
                index  index.php index.html index.htm;
        }
复制代码

目录:/home/demo   下面是打包的静态文件

 

这样页面就好了,/test/abc路由 只能允许192.168.194.53这个服务器访问,项目过程中遇到的问题,亲测有效,特此记录一下。

 

参考:https://juejin.cn/post/6992769181837950984

 

posted on   朝颜陌  阅读(975)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示