【发布时间】:2013-11-06 11:22:45
【问题描述】:
我在如下文件夹中有一个 Silex PHP 站点:
/home/user/project/build-19434/
然后是 nginx 的符号链接
/usr/share/nginx/my.site/site -> /home/user/project/build-19434
我的网络根是
/usr/share/nginx/my.site/site/web
现在我有一个 php 类,它从 /home/user/project/build-19434/web/images/gallery/ 获取所有图像并构建一个数组。后来我像这样转换这些路径,以便我可以将它们放在<img> 标签中:
/home/user/project/build-19434/web/images/gallery/img1.jpg
/images/gallery/img1.jpg
这似乎不适用于符号链接,因为我正在执行以下操作:
public function pathToUrl($path)
{
$root = $this->request->server->get('DOCUMENT_ROOT'); // essentially $_SERVER['DOCUMENT_ROOT']
return str_ireplace($root, '', str_replace("\\", '/', $path));
}
我的图片来自这个目录(这是从一个与 web 不同的目录设置的)
$path = __DIR__.'/../../web/images/gallery';
但是我的 $_SERVER 变量使用/usr/share/nginx 路径,而我获取图像的脚本是相对于__DIR__,它使用/home/user 路径。请参阅下面的部分服务器阵列(请求图库/照片页面时):
array(35) {
["USER"]=> string(5) "nginx"
["HOME"]=> string(14) "/var/lib/nginx"
["SCRIPT_NAME"]=> string(26) "/index.php//gallery/photos"
["REQUEST_URI"]=> string(15) "/gallery/photos"
["DOCUMENT_URI"]=> string(26) "/index.php//gallery/photos"
["DOCUMENT_ROOT"]=> string(39) "/usr/share/nginx/my.site/site/web"
["SCRIPT_FILENAME"]=> string(49) "/usr/share/nginx/my.site/site/web/index.php"
["ORIG_SCRIPT_FILENAME"]=> string(65) "/usr/share/nginx/my.site/site/web/index.php//gallery/photos"
["PATH_TRANSLATED"]=> string(39) "/usr/share/nginx/my.site/site/web"
["PHP_SELF"]=> string(26) "/index.php//gallery/photos"
}
如何将我的图像路径(可以按照所述符号链接)转换为 Web URL?我是否只需将__DIR__ 存储在index.php 中并将其用作我的根?
我更喜欢在我的 Windows 开发机器(没有符号链接,直接从 git 存储库中运行)和我的生产 Linux 机器(使用符号链接将项目更新到最新版本)上都适用的解决方案。
【问题讨论】:
-
我认为最好重写 nginx 配置以指向
/home目录而不是/usr,应该可以解决您的问题并消除对符号链接的需要 -
但是我想要一些固定的 nginx 配置(
/usr/share/nginx...)和多个应用程序版本(build-294a9、build-40ee 等),这样我就可以轻松回滚或更新应用程序而无需更改nginx 配置。