phphtmllinuxpublic-html

Best Practice to store images in public_html folder


Looking for advice. We would like to store all images in a folder directly under the public_html folder. The intention is have an image library available to each of our subdomain applicatons. However, it appears the only way we can see to target these images is to use an absolute path to the image subdomain. it works, but I'm concerned with performance. Any other advice?

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

What we are doing for images now.

Any other options? -Relative path up to the public_html folder?
-Any php to specify public_html folder and append image? -Does https://localhost/ work?


Solution

  • What you have right now:

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

    This allows me to access files across domains, which is very problematic. For example, I could request http://app1.mydomain.com/../app2.mydomain.com/index.html and get the files for app2 served from the domain of app1. Do not put directories for a domain under the public directory for another domain.

    Instead, the folder structure for each domain should be completely separate, and non-overlapping. Each domain should be defined by its own separate vhost in the web server configuration, and each should have its own separate html directory. For example, in nginx, you'd do something like:

    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;
    }
    

    And your files would be laid out like:

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

    This creates a standalone site for your images, basically a local CDN. In your HTML, you'd then just use an absolute reference with the full domain:

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