skip to Main Content

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


  1. Chosen as BEST ANSWER

    Solved by using another solution proposed on some plesk support page. So instead of using

    if (!-e $request_filename) { rewrite ^(.+)$ /index.php?q=$1 last; }
    

    whe should use

    if (!-e $request_filename) { set $test P; }
    if ($uri !~ ^/(pagespeed|plesk-stat|webstat|webstat-ssl|ftpstat|anon_ftpstat|awstats-icon)) { set $test "${test}C"; }
    if ($test = PC) { rewrite ^/(.*)$ /index.php?$1; }
    

  2. I have never used plesk, but normally I would just replace those if lines with try_files, like so:

    try_files $uri /index.php?q=$uri&$args;
    

    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 the location 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).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search