Skip to content
This repository has been archived by the owner on Apr 21, 2023. It is now read-only.

PageSpeed prevent image loaded by my function in Laravel #1462

Open
vutaviva opened this issue Dec 26, 2016 · 3 comments
Open

PageSpeed prevent image loaded by my function in Laravel #1462

vutaviva opened this issue Dec 26, 2016 · 3 comments

Comments

@vutaviva
Copy link

I save uploaded image by users into Storage folder with customized path. The image's name will be parsed to know where image will be save in my Storage. Therefore, I write function to return image data when user request image by GET method. It means that image is not get directly. For example, user requests:

https://example.com/image/d3fe6896b9dcbc02031eaf36761009c6.jpg

I get image name (in this case, d3fe6896b9dcbc02031eaf36761009c6.jpg) and parse it to know where I save the image in Storage folder. Before applying PageSpeed, image is loaded normally. However, image is not found (404 error) after I use PageSpeed for optimizing. I don't modify any PageSpeed Filters (use CoreFilters). My ssl.conf in nginx as following:

pagespeed on;

# Needs to exist and be writable by nginx.  Use tmpfs for best performance.
pagespeed FileCachePath /var/ngx_pagespeed_cache;

# Ensure requests for pagespeed optimized resources go to the pagespeed handler
# and no extraneous headers get set.
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {
  add_header "" "";
}
location ~ "^/pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon$" { }

location ~* .(woff|eot|ttf|svg|mp4|webm|jpg|jpeg|png|gif|ico|css|js)$ {
    expires 365d;
}

When debugging, I see that my code for response image can not reach.

//PHP code
//Parse image name and generate full path
//After that, get image data and response to client
$imageFile = File::get($fullPath);
return response($imageFile, 200)->header('Content-type', 'image/jpeg');

Therefore, I think that PageSpeed fetched image link before my code. As a result, image is not found.

How to solve the problem?

@jmarantz
Copy link
Contributor

Hi -- I am not sure I fully understand the problem. If you have PHP code manipulating your image links, that should not be affected by PageSpeed because PageSpeed runs after PHP (or at least it should>

However, if you have javascript code that is sensitive to URL syntax, that can indeed be disturbed by PageSpeed's URL manipulations. In that case, one solution is to use "pagespeed ImagePreserveUrls on" or "OptimizeForBandwidth", so that you get the benefits of image file reduction, without the risk of URL modification.

Note that in that case you will not get the benefit of image inlining or cache extension (though given your PHP you probably don't need cache extension).

@vutaviva
Copy link
Author

vutaviva commented Dec 27, 2016

The problem come from the code:
location ~* .(woff|eot|ttf|svg|mp4|webm|jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}

When I comment it, the image is loaded normally.

@jmarantz
Copy link
Contributor

When the code is uncommented, what happens, exactly?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants