Pagespeed rename the static resources (like images, CSS, etc.) file name to mark as optimized and there instances where the Pagespeed optimized web pages' resources returns 404 not found and for several browser refresh they eventually loads up. In this case, Pagespeed is still in the process of optimizing the web pages' resources. We can handle this issue by the following Nginx script:
## Pagespeed optimized resources that returns 404, redirect to original resource
location ~ (.+)/x(.+),(.+)\.pagespeed\.[\.\-_[:alnum:]]+$ {
# Handle resources with query string and Pagespeed optimized resources with
# file name prefixed with x (eg. xMyImage.png,qitok=qwer.pagespeed.asdf.webp)
error_page 404 = @orig_resource;
set $orig_resource_uri $1/$2?$3;
try_files $uri $uri/ $orig_resource_uri;
}
location ~ (.+),(.+)\.pagespeed\.[\.\-_[:alnum:]]+$ {
# Handle resources with query string
error_page 404 = @orig_resource;
set $orig_resource_uri $1?$2;
try_files $uri $uri/ $orig_resource_uri;
}
location ~ (.+)/x(.+)\.pagespeed\.[\.\-_[:alnum:]]+$ {
# Handle Pagespeed optimized resources with file name prefixed with x
# (eg. xMyImage.png.pagespeed.asdf.webp)
error_page 404 = @orig_resource;
set $orig_resource_uri $1/$2;
try_files $uri $uri/ $orig_resource_uri;
}
location ~ (.+)\.pagespeed\.[\.\-_[:alnum:]]+$ {
# Default handler
error_page 404 = @orig_resource;
set $orig_resource_uri $1;
try_files $uri $uri/ $orig_resource_uri;
}
## Redirect Pagespeed optimized resources that returns 404 to
## original resource.
location @orig_resource {
return 302 $scheme://$server_name$orig_resource_uri;
}
The script above will catch the Pagespeed renamed resource static files then check the existence of Pagespeed renamed resource static file first and if not found it redirect to original resource. It is recommended to place the script in this manner:
server {
# ... your server context script
location / {
# ... some location context script
## Pagespeed optimized resources that returns 404, redirect to original resource
location ~ (.+)/x(.+),(.+)\.pagespeed\.[\.\-_[:alnum:]]+$ {
# Handle resources with query string and Pagespeed optimized resources with
# file name prefixed with x (eg. xMyImage.png,qitok=qwer.pagespeed.asdf.webp)
error_page 404 = @orig_resource;
set $orig_resource_uri $1/$2?$3;
try_files $uri $uri/ $orig_resource_uri;
}
location ~ (.+),(.+)\.pagespeed\.[\.\-_[:alnum:]]+$ {
# Handle resources with query string
error_page 404 = @orig_resource;
set $orig_resource_uri $1?$2;
try_files $uri $uri/ $orig_resource_uri;
}
location ~ (.+)/x(.+)\.pagespeed\.[\.\-_[:alnum:]]+$ {
# Handle Pagespeed optimized resources with file name prefixed with x
# (eg. xMyImage.png.pagespeed.asdf.webp)
error_page 404 = @orig_resource;
set $orig_resource_uri $1/$2;
try_files $uri $uri/ $orig_resource_uri;
}
location ~ (.+)\.pagespeed\.[\.\-_[:alnum:]]+$ {
# Default handler
error_page 404 = @orig_resource;
set $orig_resource_uri $1;
try_files $uri $uri/ $orig_resource_uri;
}
}
## Redirect Pagespeed optimized resources that returns 404 to
## original resource.
location @orig_resource {
return 302 $scheme://$server_name$orig_resource_uri;
}
}
Comments
You save my time, TNX!
You save my time, TNX!
mod pagespeed
hi my friend i get a 404 on images that are rewritten too. do you have a solution for apache server wordpress?
Hi, I'm sorry i have no…
Hi, I'm sorry i have no solution for Apache yet but you can make the nginx codes above as reference to create version for Apache.
Thanks
Hi, arpeggio!
You helped me a lot!
Multiple query string parameters.
Haven't tested it yet, but do you think your location regex catches query strings with multiple parameters. Also with the newer versions of PageSpeed, URL rewrite rules have changed a bit.
For example PageSpeed would do the following rewrite:
http://example.com/css/site.css?ver=20190125 -> http://example.com/css/site.css,qver=20190125.pagespeed.cf.J2zeOQA4c2.css
Notice the 'q' for query. Chaining even more parameters is similar.
arpeggio
Sun, 03/03/2019 - 18:53
In reply to Multiple query string parameters. by synner (not verified)
I think it will not work…
I think it will not work because of the comma "," before "qver". I haven't encountered that case yet. Can you try this codes:
## Pagespeed optimized resources that returns 404, redirect to original resource
location ~ (.+)/x(.+),(.+)\.pagespeed\.[\.\-,_[:alnum:]]+$ {
# Handle resources with query string and Pagespeed optimized resources with
# file name prefixed with x (eg. xMyImage.png,qitok=qwer.pagespeed.asdf.webp)
error_page 404 = @orig_resource;
set $orig_resource_uri $1/$2?$3;
try_files $uri $uri/ $orig_resource_uri;
}
location ~ (.+),(.+)\.pagespeed\.[\.\-,_[:alnum:]]+$ {
# Handle resources with query string
error_page 404 = @orig_resource;
set $orig_resource_uri $1?$2;
try_files $uri $uri/ $orig_resource_uri;
}
location ~ (.+)/x(.+)\.pagespeed\.[\.\-,_[:alnum:]]+$ {
# Handle Pagespeed optimized resources with file name prefixed with x
# (eg. xMyImage.png.pagespeed.asdf.webp)
error_page 404 = @orig_resource;
set $orig_resource_uri $1/$2;
try_files $uri $uri/ $orig_resource_uri;
}
location ~ (.+)\.pagespeed\.[\.\-,_[:alnum:]]+$ {
# Default handler
error_page 404 = @orig_resource;
set $orig_resource_uri $1;
try_files $uri $uri/ $orig_resource_uri;
}
## Redirect Pagespeed optimized resources that returns 404 to
## original resource.
location @orig_resource {
return 302 $scheme://$server_name$orig_resource_uri;
}
Thanks !
Thanks !