• If you are still using CentOS 7.9, it's time to convert to Alma 8 with the free centos2alma tool by Plesk or Plesk Migrator. Please let us know your experiences or concerns in this thread:
    CentOS2Alma discussion

Question NGINX webp fallback

Lear2000

New Pleskian
I have used the following in the past to serve fall back webp images,
Code:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_ACCEPT} image/webp
    RewriteCond %{REQUEST_URI}  (?i)(.*)(\.jpe?g|\.png)$
    RewriteCond %{DOCUMENT_ROOT}%1.webp -f
    RewriteRule (?i)(.*)(\.jpe?g|\.png)$ %1\.webp [L,T=image/webp,R]
</IfModule>

<IfModule mod_headers.c>
    Header append Vary Accept env=REDIRECT_accept
</IfModule>

AddType image/webp .webp

but now my sites when I curl and image respond back with NGINX which means that apache is never seeing this
is there a way to have nginx fall back to webp images.
I have done some searches on google but most of the things dont work, adding anything with location into the field seems to fail everytime

location ~* "^(?<path>.+)\.(png|jpeg|jpg|gif)$" {
try_files $path$webp_ext $path$file_ext =404;
}

does anyone know how this is done?
 
Um, what exactly are you trying to do?
Please explain
  • url the browser sends
  • under which conditions the url should be rewritten
  • resulting url
  • whether that url should be served from nginx or apache
 
If i upload a file me.png and me.webp if the browser allows it, serve me.webp if not serve me.png
this was working for me for a while but now it seems using nginx as a proxy it wont redirect to the webp file

 
Please go to "Websites & Domains" > "Apache & Nginx Settings" > "Serve static files directly by nginx".
Then check the the box "Serve static files directly by nginx", but remove webp from that box.
Click "OK" on the bottom of the page, wait until the server restart interval expires and the new configuration becomes active in your web server. From that time on webp will not be processed by Nginx any longer, but will be passed through to your Apache.
 
Do you have "Smart static files processing" and/or "Serve static files directly by nginx" set to on?

Argh, @Peter Debik ninja'd
Though I think you got it backwards. nginx may well serve webp directly, but apache is supposed to rewrite png, jpg, etc to webp, however apache doesn't see that requests because nginx serves those other image formats directly.
 
what error is logged when that .webp link is clicked? (It redirects to the main page)
That file does exist, yes?

Remove jpeg, jpg and png from the static files directly by nginx (so those are served by apache) and add webp (because if the client already requests webp, why not serve it directly?).
 
i just noticed that redirect too
both files are there, both are 644
doing the removal.. stand by


14915#0: *8289938 open() "/var/www/vhosts/newyorkplasticsurgicalgroup.com/httpdocs/wp-content/themes/nypsg/images/doc-deane1.webp" failed (2: No such file or directory) NGINX error
 

Attachments

  • Screen Shot 2021-04-29 at 10.56.37 AM.png
    Screen Shot 2021-04-29 at 10.56.37 AM.png
    10.9 KB · Views: 12
so i have narrowed it down to this:
location ~* \.(js|jpg|jpeg|gif|png|css|tgz|gz|rar|bz2|doc|pdf|ppt|tar|wav|bmp|rtf|swf|ico|flv|txt|woff|woff2|svg|webp)$ {
etag on;
if_modified_since exact;
add_header Pragma "public";
add_header Cache-Control "max-age=31536000, public";
}

if this is on.. the redirect never happens

if i omit this code it redirects..
is there nginx code i can do the same thing as the apache htacess?
redirect png to webp in the location entry?
 
well, when I try the png now in firefox, I get the webp, with wget I still get the png. Seems it works?
 
removed png from:
location ~* \.(js|jpg|jpeg|gif|png|css|tgz|gz|rar|bz2|doc|pdf|ppt|tar|wav|bmp|rtf|swf|ico|flv|txt|woff|woff2|svg|webp)$

have the serve static files box unchecked with webp in there

but should there not be a way to tell nginx to do the same?

whats faster here?
to serve static files from nginx or leave that off so apache does it. and if apache does it then htaccess works
or
check serve static files and try to get nginx to redirect to png?
mucho confused
 
when the serve static files box is unchecked, that location block should vanish from the nginx config because plesk rewrites the config without.
(so when it's unchecked it doesn't matter at all which extensions are listed in there)

which one is faster depends too much on your hardware and traffic to tell.
normally nginx is faster, but the 1:10 reduced size could tilt that in apache's favor.
 
Back
Top