大网数据广告活动
服务器运维问答

How to map a location to a path in NGINX

· 52CE 我爱测

I have an nginx setup where I need to map specific url paths to server paths. For example I need example.test/myapp/ to go to the following directory in the server: /apps/myapp/web I tried adding the following location block: loc…

问题原文

原帖链接 · Mattia · CC BY-SA 4.0

I have an nginx setup where I need to map specific url paths to server paths. For example I need example.test/myapp/ to go to the following directory in the server: /apps/myapp/web

I tried adding the following location block:

location /myapp/
{
    root /apps/myapp/web;
}

but if I go to example.test/myapp/ or example.test/myapp I get a 404.

As far as I can tell according to the docs this location should capture anything with a path that starts with /myapp/ and it's the only location block in the server so there's no chance of conflict. Also I've tried changing the location to point to the root (i.e. location /) and it works fine so it's not a problem of the wrong server path or missing files. What am I missing?

已采纳解答

原帖链接 · Tero Kilkanen · CC BY-SA 4.0

nginx resolves file paths by using following strategy:

  1. Use path from root directive
  2. Add URI from request after the directive.

Now, in your case:

  1. root is /apps/myapp/web.
  2. URI from example.test/myapp/ is /myapp/.

Therefore nginx looks up files in /apps/myapp/web/myapp/ directory for that request.

If you want that /myapp/ URI will get files from /apps/myapp/web/ directory, you need to use the alias directive:

location /myapp/ {
    alias /apps/myapp/web/;
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2022-01-07)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。