将图像存储在 public_html 文件夹中的最佳实践

问题描述 投票:0回答:1

寻求建议。我们希望将所有图像存储在 public_html 文件夹下的一个文件夹中。目的是为我们的每个子域应用程序提供一个可用的图像库。然而,我们看到定位这些图像的唯一方法似乎是使用图像子域的绝对路径。它有效,但我关心性能。还有其他建议吗?

Folder Structure
public_html
    image folder  (image.mydomain.com)
        test.jpg
    subdomain folder (app1.mydomain.com)
    subdomain folder (app2.mydomain.com)
       index.html

我们现在正在为图像做什么。

还有其他选择吗? -public_html文件夹的相对路径?
- 有任何 php 来指定 public_html 文件夹并附加图像吗? - https://localhost/ 有效吗?

php html linux public-html
1个回答
0
投票

你现在拥有的:

public_html
    image folder  (image.mydomain.com)
        test.jpg
    subdomain folder (app1.mydomain.com)
    subdomain folder (app2.mydomain.com)
       index.html

这允许我跨域访问文件。例如,我可以请求

http://app1.mydomain.com/../app2.mydomain.com/index.html
并获取从 app1 的域提供的 app2 的文件。这是有问题的。不要将子域的目录放在另一个域的目录下。 相反,每个域的文件夹结构应该完全独立,并且不重叠。每个域都应该在 Web 服务器配置中由自己单独的虚拟主机定义,并且每个域都应该有自己单独的 html 目录。例如,在 nginx 中,您可以执行以下操作:

server { server_name image.mydomain.com; root /path/to/wherever/image.mydomain.com/public_html; } server { server_name app1.mydomain.com; root /path/to/wherever/app1.mydomain.com/public_html; } server { server_name app2.mydomain.com; root /path/to/wherever/app2.mydomain.com/public_html; }

您的文件将如下所示:

/path/to/wherever/ image.mydomain.com/ public_html/ test.jpg app1.mydomain.com public_html/ index.html app2.mydomain.com public_html/ index.html

这将为您的图像创建一个独立的站点,基本上是一个本地 CDN。在 HTML 中,您只需使用完整域的绝对引用:

<img src="http://image.mydomain.com/test.jpg">

© www.soinside.com 2019 - 2024. All rights reserved.