Redirect Nginx-如何将目录重定向到其路径包含该目录的文件?

Redirect Nginx-如何将目录重定向到其路径包含该目录的文件?,redirect,nginx,url-rewriting,uwsgi,virtual-directory,Redirect,Nginx,Url Rewriting,Uwsgi,Virtual Directory,我是Nginx新手,我正在尝试将一个目录重定向到一个文件,下面是我尝试做的基本操作: When entering this link: http://localhost:8889/dir go to this link instead: http://localhost:8889/dir/path/to/the/file/index.html 正如您所看到的,目录是我试图重定向到的文件路径的一部分。此外,配置文件中已经有一个位置块,用于将该目录重定向到正确的位置: location /di

我是Nginx新手,我正在尝试将一个目录重定向到一个文件,下面是我尝试做的基本操作:

When entering this link:
http://localhost:8889/dir

go to this link instead:

http://localhost:8889/dir/path/to/the/file/index.html
正如您所看到的,目录是我试图重定向到的文件路径的一部分。此外,配置文件中已经有一个位置块,用于将该目录重定向到正确的位置:

location /dir {
    root      /opt/dir;
}
我的第一次尝试是使用重写指令,正如我在本博客()中看到的:

但是页面说它有一个重定向循环,我相信这是与旧位置块的冲突

然后,我尝试添加另一个位置块,正如我从这里看到的()

但在重新加载配置文件后,Nginx告诉我已经存在一个具有相同目录的位置块

所以我的问题是我有没有办法做到这一点?或者甚至不可能

谢谢

找到了解决办法

我使用“return”返回完整的硬编码url,而不是重写当前url:

location ~* /dir$ {
    return http://localhost:8889/dir/path/to/the/file/index.html; 
}
而且这种重写解决方案也很有效:

location ~* /dir$ {
    rewrite ^/dir$ /dir/path/to/the/file/index.html;
}
我遇到的问题实际上是由缓存引起的。我实际上是从缓存中重新加载页面,所以在更改nginx配置文件后无法看到结果,我通过在Chrome的developer tools中选中“Disable cache”选项解决了这个问题

location ~* /dir$ {
    return http://localhost:8889/dir/path/to/the/file/index.html; 
}
location ~* /dir$ {
    rewrite ^/dir$ /dir/path/to/the/file/index.html;
}