I’m using Plesk with nginx + ModPagespeed + WordPress
To lazy load images there is a placeholder image which should be loaded form this kind of URL
https://example.com/pagespeed_static/1.JiBnMqyl6S.gif it’s dynamic, there is not actual folder so WordPress tries to send it to index.php, because of this WordPress rule set in nginx:
if (!-e $request_filename) { rewrite ^(.+)$ /index.php?q=$1 last; }
Which is the correct approach to allow it to load image? Should I check if the URL is equal to that request and then inside adding the WordPress rule?
if($url != 'pagespeed_static/1.JiBnMqyl6S.gif') {
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php?q=$1 last;
}
}
Ps. Just noticed that there is the same problem with .js which is caught by WordPress related rule, the URL is https://example.com/pagespeed_static/js_defer.I4cHjq6EEP.js
Here the contents of nginx directives file:
# PAGESPEED - - - - - - - - - -
pagespeed on;
pagespeed FileCachePath /var/ngx_pagespeed_cache;
location ~ ".pagespeed.([a-z].)?[a-z]{2}.[^.]{10}.[^.]+" { add_header "" ""; }
location ~ "^/pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon$" { }
pagespeed CriticalImagesBeaconEnabled true;
pagespeed PreserveUrlRelativity on;
pagespeed InPlaceResourceOptimization off;
pagespeed EnableFilters add_head;
pagespeed EnableFilters combine_heads;
pagespeed EnableFilters extend_cache;
pagespeed EnableFilters inline_import_to_link;
pagespeed EnableFilters outline_css;
pagespeed EnableFilters outline_javascript;
pagespeed EnableFilters remove_comments;
pagespeed EnableFilters collapse_whitespace;
pagespeed EnableFilters combine_css;
pagespeed EnableFilters rewrite_css;
pagespeed EnableFilters inline_css;
pagespeed EnableFilters inline_google_fonts_css;
pagespeed EnableFilters fallback_rewrite_css_urls;
pagespeed EnableFilters rewrite_style_attribute;
pagespeed EnableFilters rewrite_style_attributes_with_url;
pagespeed EnableFilters flatten_css_imports;
pagespeed EnableFilters prioritize_critical_css;
pagespeed EnableFilters sprite_images;
pagespeed EnableFilters rewrite_javascript;
pagespeed EnableFilters combine_javascript;
pagespeed EnableFilters inline_javascript;
pagespeed EnableFilters defer_javascript;
pagespeed EnableFilters inline_images;
pagespeed EnableFilters recompress_images;
pagespeed EnableFilters convert_png_to_jpeg;
pagespeed EnableFilters resize_images;
pagespeed EnableFilters convert_to_webp_lossless;
pagespeed EnableFilters insert_image_dimensions;
pagespeed EnableFilters inline_images;
pagespeed EnableFilters jpeg_subsampling;
pagespeed EnableFilters recompress_jpeg;
pagespeed EnableFilters recompress_png;
pagespeed EnableFilters recompress_webp;
pagespeed EnableFilters lazyload_images;
pagespeed LazyloadImagesAfterOnload off;
pagespeed EnableFilters strip_image_color_profile;
pagespeed EnableFilters strip_image_meta_data;
pagespeed EnableFilters resize_images;
pagespeed EnableFilters resize_rendered_image_dimensions;
pagespeed EnableFilters inline_preview_images;
pagespeed EnableFilters resize_mobile_images;
pagespeed DisableFilters elide_attributes;
pagespeed DisableFilters include_js_source_maps;
pagespeed EnableFilters insert_dns_prefetch;
# WORDPRESS - - - - - - - - - - - - -
location ~ /.ht { deny all; }
location ~* wp-config.php { deny all; }
location ~* "^/wp-content/(?!plugins/).*.php" { deny all; }
if (!-e $request_filename) { rewrite ^(.+)$ /index.php?q=$1 last; }
2
Answers
Solved by using another solution proposed on some plesk support page. So instead of using
whe should use
I have never used plesk, but normally I would just replace those
if
lines withtry_files
, like so:This sends all requests to existing files to those files and all other requests to
index.php
with the requested URL and possible other arguments as argument, like in your code.As you only showed part of the config I can’t judge whether the
try_files
line needs to be outside thelocation
blocks or inside one.As a general guideline, following the nginx documentation, I try to avoid
if
wherever possible (sometimes, admittedly, it isn’t possible).