RackSpace Cloud Strips $ _SESSION if the URL has specific file extensions

Situation

I am creating a video processing site for a client in the RackSpace cloud using the traditional LAMP stack (the RackSpace cloud has a Windows and LAMP stack). The videos and other multimedia files that I serve on this site should be protected, as my client charges money for access to them. We don’t have DRM or funny business, in fact, we store files outside the root of the website and use PHP to authenticate the user before they can access the files using mod_rewrite to run the request through PHP.

So, let's say a user requests a file at this URL:

http://www.example.com/uploads/preview_image/29.jpg 

I use mod_rewrite to rewrite this url for:

 http://www.example.com/files.php?path=%2Fuploads%2Fpreview_image%2F29.jpg 

Here is a simplified version of the files.php script:

 <?php // Setups the environment and sets $logged_in // This part requires $_SESSION require_once('../../includes/user_config.php'); if (!$logged_in) { // Redirect non-authenticated users header('Location: login.php'); } // This user is authenticated, continue $content_type = "image/jpeg"; // getAbsolutePathForRequestedResource() takes // a Query Parameter called path and uses DB // lookups and some string manipulation to get // an absolute path. This part doesn't have // any bearing on the problem at hand $file_path = getAbsolutePathForRequestedResource($_GET['path']); // At this point $file_path looks something like // this: "/path/to/a/place/outside/the/webroot" if (file_exists($file_path) && !is_dir($file_path)) { header("Content-Type: $content_type"); header('Content-Length: ' . filesize($file_path)); echo file_get_contents($file_path); } else { header('HTTP/1.0 404 Not Found'); header('Status: 404 Not Found'); echo '404 Not Found'; } exit(); ?> 

Problem

Let me start by saying that this works great for me. On local test machines, this works like a charm. However, after it is deployed in the cloud, it stops working. After some debugging, it turns out that if the cloud request has certain file extensions, such as .JPG, .PNG or .SWF (i.e. extensions of usually static media files.), The request is sent to a caching system called Varnish. The end result of this routing is that by the time this whole process has turned into my PHP script, there is no session.

If I change the extension in the URL to .PHP, or even if I add a request parameter, then Varnish bypasses, and the PHP script can get the session. No problems? I will just add a meaningless query parameter to my queries!

Here is rub:. The multimedia files that I serve through this system are requested through compiled SWF files with which I have zero control. They are created by third-party software, and I do not hope to add or change the URLs that they request.

Are there any other options that I have?

Update:. I should note that I tested this behavior with RackSpace support, and they said that they can do nothing about it.

+4
source share
2 answers

If the requesting flash application performs the following redirects, I would try to redirect the first request and rewrite the second one, for example.

 GET .../29.jpg 

to

 header("Status: 302 Moved temporarily"); header("Location: .../r.php?i=29.jpg&random=872938729348"); 

Then your r.php delivers the file at the second request.

If not (btw. Always), I would explicitly send the headers along with the delivery of static files that Varnish accepts and acts accordingly, something like

 header("Cache-Control: no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0"); header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); 

A: I would put exit(); after your first header() statement to make sure the rest of the script is not executed. header() sends only headers.

I find it more reliable to use ob_start() since spaces in your PHP file can lead to annoying errors when adding headers.

+2
source

I have the same situation, and I contacted Rackspace hoping for a better answer.

I have one! They put together a FAQ that outlines half a dozen ways to get around / changing caching:

http://cloudsites.rackspacecloud.com/index.php/How_can_I_bypass_the_cache%3F

0
source

Source: https://habr.com/ru/post/1301060/


All Articles