skip to Main Content

i have a page where the user has static files (pdf) and images (created from the pdf on)
there is a structure like

/files/user1/img/foo.jpg
/files/user1/pdf/foo.pdf
/files/user2/img/bar.jpg
/files/user2/pdf/bar.pdf
...

At the moment i deliver these images and pdf files with a php script which checks if user1 is authenticated (with something like php?img=user1.jpg) but this is slow.
I want do serve it directly with nginx but it needs some sort of authentification to insure that only the logged in user1 can read files from user1 etc.

There is a PHP login done with codeigniter4, is there a way to let nginx at the login with php know that is allowed to server the files to user1?

Thanks for helping.
Best regards
Alex

2

Answers


  1. You can use the X-Accel-Redirect feature of Nginx.

    Login or Signup to reply.
  2. Yes, you can use X-Accel-Redirect. first check if the user is authenticated and authorized to access the file, then if the user is allowed, send an X-Accel-Redirect header pointing to an internal location. Configure Nginx to map that internal location to the actual file path.

    nginx configs could be like this:

    location /protected_files/ {
        internal;
        alias /files/;
    }
    

    and a sample for php code:

    if ($userIsAuthenticated) {
        header('X-Accel-Redirect: /protected_files/user1/img/foo.jpg');
        exit;
    } else {
        http_response_code(403);
        echo "Forbidden";
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search