Support » Plugin: WebP Express » nginx server 404 not found when convert test images

  • Hello I have tried method 1 and 2 from the FAQ about Nginx server but can’t get webp express to work, when I convert test image after configuration, I got a 404 not found.
    It’s a classic wp installation on ubuntu 18.04 nginx server on digitalocean.
    My file /etc/nginx/sites-available looks like:

    `server {
    root /var/www/mysite.be;
    index index.php index.html index.htm index.nginx-debian.html;
    server_name mysite.be http://www.mysite.be;

    location / {
    # try_files $uri $uri/ =404;
    try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    }

    location ~ /\.ht {
    deny all;
    }

    location = /favicon.ico { log_not_found off; access_log off;
    }

    location ~* ^/wp-content/.*\.(png|jpe?g)$ {
    add_header Vary Accept;
    expires 365d;
    }
    location ~* ^/wp-content/.*\.webp$ {
    expires 365d;
    if ($whattodo = AB) {
    expires 365d;
    if ($whattodo = AB) {
    add_header Vary Accept;
    }
    }
    if ($http_accept ~* “webp”){
    set $whattodo A;
    }
    if (-f $request_filename.webp) {
    set $whattodo “${whattodo}B”;
    }
    if ($whattodo = AB) {
    rewrite ^(.*) $1.webp last;
    }
    if ($whattodo = A) {
    rewrite ^/wp-content/.*\.(jpe?g|png)$ /wp-content/plugins/webp-express/w$
    }
    location = /robots.txt { log_not_found off; access_log off; allow all; }
    location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
    expires max;
    log_not_found off;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/mysite.be/fullchain.pem; # managed$
    ssl_certificate_key /etc/letsencrypt/live/mysite.be/privkey.pem; # manag$
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    }

    server {
    if ($host = http://www.mysite.be) {
    return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = mysite.be) {
    return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($http_accept ~* “webp”){
    rewrite ^/(.*).(jpe?g|png)$ /wp-content/plugins/webp-express/wod/webp-on-de$
    }

    listen 80;
    server_name mysite.be http://www.mysite.be;
    return 404; # managed by Certbot

    } `

    What to do to get it to work ?

Viewing 10 replies - 16 through 25 (of 25 total)
  • Plugin Author rosell.dk

    (@roselldk)

    Note that you must beware that the WebP Express settings for destination must match the rules. There are many things to beware of. I have written a whole list in the FAQ.

    bakkerhenk

    (@bakkerhenk)

    I got the whole thing to work using the config below

    # WebP Express rules
    # --------------------
    location ~* ^/?wp-content/.*\.(png|jpe?g)$ {
    	add_header Vary Accept;
    	expires 365d;
    	if ($http_accept !~* "webp"){
    		break;
    	}
    	try_files
    	/wp-content/webp-express/webp-images/doc-root/$uri.webp
    	$uri.webp
    	/wp-content/plugins/webp-express/wod/webp-on-demand.php?xsource=x$request_filename&wp-content=wp-content
    	;
    
    }
    
    # Route requests for non-existing webps to the converter
    location ~* ^/?wp-content/.*\.(png|jpe?g)\.webp$ {
    	#set $test "${test}A:${uri}---";
    	set $test "${test}A";
    
    	try_files
    	$uri
    	/wp-content/plugins/webp-express/wod/webp-realizer.php?wp-content=wp-content
    	;
    }
    # ------------------- (WebP Express rules ends here)
    
    if (!-e $request_filename) {
    	set $test P;
    }
    
    # If the uri points to webp-on-demand.php or a webp file, append D to the test variable
    if ($uri !~ ^/(.*)(webp-on-demand\.php|\.webp)$) {
    	set $test "${test}D";
    }
    
    if ($test = PD) {
    	rewrite ^/(.*)$ /index.php?$1;
    }
    Plugin Author rosell.dk

    (@roselldk)

    Let me see if I understand this. Please confirm that the following is correctly understood.

    The problem was actually the following code for permalinks:

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

    which redirected non-existing files (and dirs and softlinks) to index.php.

    And you changed that code so it doesn’t trigger when that missing file is a webp or webp-on-demand.php.

    $test is set to P if file is non-existing. And appended “D” if the missing file isn’t a webp or webp-on-demand.php. So – only redirect if both conditions are set (PD).

    Plugin Author rosell.dk

    (@roselldk)

    But do you need the “# WORDPRESS PERMALINKS” block at all?
    You already got your try_files in your location block:

    
    location / {
    # try_files $uri $uri/ =404;
      try_files $uri $uri/ /index.php$is_args$args;
    }
    

    It should take care of redirecting custom permalinks to WordPress. And as this uses a “prefix match” it does not overrule the regex match.

    But ok, you state that you already tried this and it for some reason doesn’t work. I’m wondering if this rewrite hack is used widely. In that case, I should mention your solution in the FAQ.

    Btw, this solution of course doesn’t help @tanuki1986. But thank you for posting it regardless 🙂

    Plugin Author rosell.dk

    (@roselldk)

    I can see that the rewrite hack for permalinks is out here and there alright

    • This reply was modified 1 month ago by  rosell.dk.
    Plugin Author rosell.dk

    (@roselldk)

    Summing up the hack so I can link to it from the FAQ.

    Problem:
    On some setups, the following hack has been used in order to get permalinks working. The hack however overrules the standard Nginx rules used for WebP Express.

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

    Solution:
    If that hack is present in you configuration, it needs to be substituted with this:

    
    # WORDPRESS PERMALINKS HACK, HACKED TO WORK WITH WEBP EXPRESS
    
    if (!-e $request_filename) {
    	set $redirectToIndex YES;
    }
    
    # If the uri points to webp-on-demand.php or a webp file, cancel that redirect
    if ($uri ~ ^/(.*)(webp-on-demand\.php|\.webp)$) {
    	set $redirectToIndex NO;
    }
    
    if ($redirectToIndex = YES) {
    	rewrite ^/(.*)$ /index.php last;
    }
    

    The solution was conceived by bakkerhenk here (in this thread). I modified it a bit in order to make it slightly more readable.

    • This reply was modified 1 month ago by  rosell.dk.
    • This reply was modified 1 month ago by  rosell.dk.
    • This reply was modified 1 month ago by  rosell.dk.
    • This reply was modified 1 month ago by  rosell.dk.
    • This reply was modified 1 month ago by  rosell.dk.
    • This reply was modified 1 month ago by  rosell.dk.
    • This reply was modified 1 month ago by  rosell.dk.
    Plugin Author rosell.dk

    (@roselldk)

    @bakkerhenk: How come you are using
    rewrite ^/(.*)$ /index.php?$1; rather than
    rewrite ^/(.*)$ /index.php?q=$1 last; ?

    Plugin Author rosell.dk

    (@roselldk)

    According to this link, wordpress does not need the query string, but can read from REQUEST_URI. So I suppose the following will do:

    rewrite ^/(.+)$ /index.php last;

    I suppose the “last” flag is good to keep other rewrites from overruling our rewrite.

    • This reply was modified 1 month ago by  rosell.dk.
    bakkerhenk

    (@bakkerhenk)

    Your conclusion is correct.
    I have no clue why the code below isn’t working. I’ve checked pretty much every config for NGINX that I’ve found on the server. (Plesk Onyx 17.8.11)

    location / {
    # try_files $uri $uri/ =404;
      try_files $uri $uri/ /index.php$is_args$args;
    }

    The redirect hack is a solution that Plesk offers on it’s support pages:
    After switching a WordPress website to FPM served by nginx in Plesk, it fails to load with “404 Not Found” on all pages except start page

    I’m not sure why I omitted the ‘last’ flag, but it would be better to use it.

    Plugin Author rosell.dk

    (@roselldk)

    Thanks for the coffee, @bakkerhenk 🙂

Viewing 10 replies - 16 through 25 (of 25 total)
  • You must be logged in to reply to this topic.