关于nginx使用路径重定向至服务的问题?

现在有域名www.services.com, 对应的服务器上部署了三个服务ServiceA, ServiceB, ServiceC,
每个服务的访问方法为访问对应路径的index.html文件即可, 如ServiceA服务的前端路径为Path/to/A, ServiceB服务的前端路径为pATh/tO/B

然后我想要实现通过http://www.services.com/ServiceA 来访问Path/to/A/index.html, 于是我尝试了这样子的配置:

http {  listen 80;  server_name www.services.com;  # nginx默认配置
  location /  {    root html;    index index.html index.htm
  }  # ServiceA配置
  locaton ^~ /ServiceA {    root Path/to/A
    index.index.html index.htm
  }
}

而最后实际上会请求访问到/Path/to/A/ServiceA/或是/usr/share/nginx/html/ServiceA/, 于是返回404报错.

请问为什么会出现这样的路由规则? 如何实现期望效果?


慕田峪4524236
浏览 1291回答 2
2回答

HUH函数

可以使用 try files 解决# ServiceA配置 locaton ^~ /ServiceA {      root Path/to/A ;    #index.index.html index.htm      try_files $url  index.html; }

慕森王

index index.html index.htm;  locaton /ServiceA/ {    alias Path/to/A/;   }  locaton /ServiceB/ {    alias Path/to/B/;   }  locaton /ServiceC/ {    alias Path/to/C/;   }
打开App,查看更多内容
随时随地看视频慕课网APP