Nginx块服务器路由

编程入门 行业动态 更新时间:2024-10-21 09:42:39
本文介绍了Nginx块服务器路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试将url路由到服务器的另一部分-使用nginx服务器块通过角度创建的管理区域.

I am trying to route a url to another part of my server - the admin area which is made by angular using nginx server blocks.

我可以使用它来工作

location ~ /admin { root /home/sysadmin/html; }

这将路由到/home/sysadmin/html/admin.index.html .我不知道如何将所有更深的URL路由到此文件位置,即/admin/dashboard/也将转到/home/sysadmin/html/index.html.

This routes to /home/sysadmin/html/admin.index.html. What I can't figure out is how to route all deeper URLS to this file location i.e. /admin/dashboard/ would also go to /home/sysadmin/html/index.html.

例如,如果转到www.url/admin/,它可以工作并且我的角度应用程序运行,但是如果刷新www.url/admin/login,它将生成404.

For example, if go to www.url/admin/ it works and my angular app runs but if I refresh on www.url/admin/login it will produce a 404.

我需要/admin的所有子URL才能进入系统的/home/sysadmin/html/admin/index.html文件.

I need all child urls of /admin to go to my systems /home/sysadmin/html/admin/index.html file.

请注意,这是一个nginx问题,而不是一个有角度的问题,因为我可以正常浏览和运行项目.

Please note, this is a nginx issue not a angular one as I can navigate and run my project fine.

是否最好设置一个指向www.url/admin的备用子域并在另一个服务器块中对其进行管理?

is it best to setup an alternative subdomain that points to www.url/admin and manage it another server block?

希望这很清楚.

推荐答案

您正在寻找的是 Nginx try_files指令.

location ~ /admin { root /home/sysadmin/html/admin; try_files $uri $uri/ /index.html =404; }

location ~ /admin { root /home/sysadmin/html/admin; try_files $uri $uri/ /index.html =404; }

这表示它将首先测试实际文件或文件夹(相对于您指定的根文件夹)的存在,然后如果找不到,则将在该根目录中返回index.html.

This says that it will test for the existence of the actual files or folders (relative to the root folder you specified) first, and then if none are found it will return the index.html in that root instead.

这在Angular中设置HTML5模式时很常见,听起来好像您正在尝试这样做.

This is common when setting up HTML5 mode in Angular, for which it sounds like you are trying to do.

更多推荐

Nginx块服务器路由

本文发布于:2023-10-12 17:02:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485327.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   服务器   Nginx

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!