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报错.

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

阅读 1.9k
2 个回答

可以使用 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/;
  }
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题