skip to Main Content

I’m on a really old Laravel 4.2 project. I stored all files in /app/files/ for security reasons. I would like to make one of my directories to public access, this one /app/files/logo_path/.


Let’s say … I have a file cat.png located at

Ex. /app/files/logo_path/cat.png

I want my domain to return it like this domain.com/logo_path/cat.png.

Should I use routes.php or Nginx configuration to solve this ?

Basically, I want to map all images from /app/files/logo_path/* to domain.com/logo_path/*

2

Answers


  1. With nginx, you can do it with the following configuration:

    location ^~ /logo_path/ {
        root /app/files; # you should specify full path to /app/files folder here!
    }
    

    I assume you haven’t had any other files than images in /app/files/logo_path/, that’s why I use ^~ location modifier to prevent any attempt to request any PHP script from this path (if such a request would come, it won’t be intercepted with PHP handler location and interpreted with the PHP-FPM engine).

    This configuration isn’t a security flaw, because this location block would be used only for requests starting with /logo_path/ prefix. If you want to use different prefix, root directive won’t be suitable for you, for that case you would need to use an alias directive instead. Here is an excerpt from the nginx documentation:

    For example, with the following configuration

    location /i/ {
        root /data/w3;
    }
    

    The /data/w3/i/top.gif file will be sent in response to the /i/top.gif request.

    A path to the file is constructed by merely adding a URI to the value of the root directive. If a URI has to be modified, the alias directive should be used.

    Login or Signup to reply.
  2. nginx configuration is the way to solve this problem. There is nothing much to be gained by passing these static image objects through php unless you want to show them only to authorized site visitors.

    nginx is faster than php for delivering static objects. It mmaps the objects’ files to RAM and then sends them to browsers. Fast!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search