目录已存在时的符号链接

Symlink when the directory already exists

我们已经在我们的网络服务器上添加了一个 NFS 共享,并且需要设置一个符号链接 - 但是,当目录已经存在时,我们不确定如何执行此操作。

Webserver directory : /home/example/public_html/images    
NFS target : /nfsshare

我们想要一个来自 /home/example/public_html/images -> /nfsshare

的符号链接

NFS 共享包含 /home/example/public_html/images 中所有内容的最新副本,我们希望任何时候有人访问 https://www.example.com/images/123.jpg 或在 HTML 中引用 /images/123.jpg它显示文件 /nfsshare/123.jpg 例如

当我们创建符号链接时

ln -s /nfsshare /home/example/public_html/images/ 

它看起来工作正常但是当我们浏览到 /images 它仍然显示原始内容并且 nfsshare 文件夹然后在图像文件夹中

[root@host01 images]# ls
index.html  nfsshare  image.png

当您想要将文件夹实际指向其他位置而不是其当前位置时,是否有任何其他方法?

符号链接的作用是创建指向现有文件夹(或文件)的 "virtual" 文件夹(或文件)。但是当文件夹或文件已经存在时,您不能创建它——更准确地说,当物理目录已经存在时,您不能创建 "virtual" 文件夹(符号链接)。

... 如果我理解正确的话,您已经有一个现有的 /home/example/public_html/images 目录。你不能两者兼得;您需要删除(或重命名)现有的 Web 服务器目录。

顺便说一句,符号链接是在文件夹内创建的,因为您将现有文件夹名称指定为目标。

更新:
假设服务器上有一组合理的权限,您可能需要使用 root 或使用 sudo 来执行此操作。您可能还应该验证 NFS 文件夹的权限是否将您的 Web 应用程序限制为 read-only 访问。