nginx将虚拟目录重写为文件

Mic*_*ham 11 nginx virtual-directory

这应该很容易做到,但我正在撞墙.如果我收到www.mysite.com/mypath的请求,我想提供www.mysite.com/myotherpath/thisfile.html的内容.如何使用nginx配置执行此操作.

VBa*_*art 12

location = /mypath {
    try_files /myotherpath/thisfile.html =404;
}
Run Code Online (Sandbox Code Playgroud)


Ale*_*kiy 9

在适当的位置块中使用重写指令.例如,您有基本位置来处理所有请求

location / {
    /*your rules here*/
}
Run Code Online (Sandbox Code Playgroud)

您将需要添加另一个块,这将为您处理特定路径

location /mypath {
    rewrite ^/mypath$ /real/path/to/file/thisfile.html; 
}
Run Code Online (Sandbox Code Playgroud)

此外,对于您的服务器在thisfile.html默认的块中进行思考,您可以使用try thisfile.html指令

在官方页面官方Nginx RewriteModule页面上都有很好的解释