Nginx块服务器路由 [英] Nginx block server routing

查看:78
本文介绍了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.com/admin/,它可以工作并且我的角度应用程序运行,但是如果刷新www.url.com/admin/login,它将生成404.

For example, if go to www.url.com/admin/ it works and my angular app runs but if I refresh on www.url.com/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.com/admin的备用子域并在另一个服务器块中对其进行管理?

is it best to setup an alternative subdomain that points to www.url.com/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块服务器路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆