vue 專案限制某頁面路由只能由某伺服器ip訪問,其他的不能訪問
阿新 • • 發佈:2022-05-27
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