• Resolved morcth

    (@morcth)


    Hi Mateusz,

    I might have a bit of a tough one for you.

    I use Trellis and Bedrock for all my WP sites: https://roots.io/

    It’s an awesome system for versioning and deployment.

    It uses NGINX and also creates an alias for the uploads folder.

    I got your plugin working but saw it was only working for themes and plugin folders… not uploads.

    I looked into it more and saw it was because the uploads folder is an alias to: /srv/www/site.com/shared/uploads

    When i look on the server for the path to all the webp images, it is: /srv/www/site.com/current/web/app/uploads-webpc/srv/www/site.com/shared/uploads

    So, it looks like the plugin is getting the real path and recreating that in the file system instead of using the alias. It should be: /srv/www/site.com/current/web/app/uploads-webpc/uploads and not have the: /srv/www/site.com/shared

    I was then able to make the plugin work for the uploads folder by changing the try line for the nginx settings to: try_files /app/uploads-webpc/srv/www/site.com/shared/$path.$ext.webp $uri =404;

    This works fine for uploads folder but of course plugins and themes folders do not work anymore.

    Any ideas on what I can do here? I tried a bunch of different paths for add_filter functions but solved all issues. Maybe there is a way to get the plugin to obey the alias? Or some different nginx settings?

    Thanks for any help!
    Josh

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author Mateusz Gbiorczyk

    (@mateuszgbiorczyk)

    Hi @morcth,

    Thank you for your message.

    Please tell me, are the images in the uploads directory converted correctly to WebP format?

    Could you please describe the directory structure to me like this:

    ├── directory_a
        ├── directory_aa
        ├── directory_ab
    ├── directory_b
        ├── directory_ba
        ├── directory_bb
    ├── directory_c
    Thread Starter morcth

    (@morcth)

    Hi @mateuszgbiorczyk,

    Yes, everything is converted flawlessly. If I have plugins, themes and uploads checked, a themes and plugins folder is correctly in the uploads-webpc folder but there is also an srv folder which should be the uploads folder and that srv folder then goes to www then site.com -> shared and finally uploads. Below, the “current” folder is also an alias to the current release.. the server holds the most recent 4 or 5.

    Folder structure after conversion:

    from /srv/www/site.com/current/web/

    ├── app 
        ├── mu-plugins
        ├── plugins
        ├── themes
        ├── uploads -> /srv/www/site.com/shared/uploads
        ├── uploads-webpc
            ├── plugins
            ├── themes
            ├── srv
                ├── www        
                    ├── site.com
                        ├── shared
                            ├── uploads
    

    All webp images are correctly in uploads.
    So, if I leave your nginx settings from your faq as is, all plugin and theme images work but uploads do not. If I change the try statement and put in /app/uploads-webpc/srv/www/site.com/shared/$path.$ext.webp $uri =404; Uploads work but plugins and themes do not.

    Thank you for looking into it!

    Plugin Author Mateusz Gbiorczyk

    (@mateuszgbiorczyk)

    In that case, try the following configuration:

    server {
      # ...
    
      location ~ /wp-content/(?<dir>plugins|themes)/(?<path>.+)\.(?<ext>jpe?g|png|gif)$ {
        if ($http_accept !~* "image/webp") {
          break;
        }
        add_header Vary Accept;
        expires 365d;
        try_files /wp-content/uploads-webpc/$dir/$path.$ext.webp $uri =404;
      }
    
      location ~ /wp-content/srv/(?<path>.+)\.(?<ext>jpe?g|png|gif)$ {
        if ($http_accept !~* "image/webp") {
          break;
        }
        add_header Vary Accept;
        expires 365d;
        try_files /app/uploads-webpc/srv/www/site.com/shared/srv/$path.$ext.webp $uri =404;
      }
    }
    Thread Starter morcth

    (@morcth)

    I had tried some if statements in the same location block. I was hoping it would be a simple nginx config addition but I am unfamiliar with the format.

    I had to change around what you sent a bit but got it working!
    Thanks so much for being a freakin genius! Bought you a few cups of coffee.

    Awesome plugin and easy setup!

    Working code below in case it helps anyone!

    location ~ /app/(?<dir>plugins|themes)/(?<path>.+)\.(?<ext>jpe?g|png|gif)$ {
      if ($http_accept !~* "image/webp") {
        break;
      }
      add_header Vary Accept;
      expires 365d;
      try_files /app/uploads-webpc/$dir/$path.$ext.webp $uri =404;
    }
    
    location ~ /app/(?<dir>uploads)/(?<path>.+)\.(?<ext>jpe?g|png|gif)$ {
      if ($http_accept !~* "image/webp") {
        break;
      }
      add_header Vary Accept;
      expires 365d;
      try_files /app/uploads-webpc/srv/www/site.com/shared/uploads/$path.$ext.webp $uri =404;
    }
    Plugin Author Mateusz Gbiorczyk

    (@mateuszgbiorczyk)

    Thank you for the detailed description of the case and cooperation. It was a pleasure helping you!

    @morcth, if you are satisfied with using the plugin, I will be grateful for adding a review.

    Thread Starter morcth

    (@morcth)

    Done!

    Thanks again

    Thread Starter morcth

    (@morcth)

    Hi @mateuszgbiorczyk,

    New but connected issue that you can hopefully help with.

    So I realized that everytime I deploy a new version, the uploads-webpc folder disappears because the “current” folder is another alias to a new release folder: /srv/www/site.com/releases/20200730120153/web/

    So I tried creating the uploads-webpc folder in the shared folder where the uploads folder actually lives. This works fine… all images are correctly converted.

    The issue now though is that the uploads folder does not have the long path anymore because no alias’s are being used and the themes and plugins folders do.

    New folder structure:
    From /srv/www/site.com

    
    ├── current -> /srv/www/site.com/releases/20200730120153
     ├──web
      ├── app 
        ├── mu-plugins
        ├── plugins
        ├── themes
        ├── uploads -> /srv/www/site.com/shared/uploads
    ├──shared
      ├── uploads 
      ├── uploads-webpc
        ├── uploads
        ├── srv
          ├── www        
            ├── site.com
              ├── releases
                ├── 20200730120153 --This will always change but maybe can use "current" alias in nginx?
                  ├── web
                    ├── app
                      ├── plugins
                      ├── themes
    ├── logs
    ├── releases
    

    SO, we need the server to check /srv/www/site.com/shared for the uploads-webpc folder which is outside the web/app folder and we need to be able to use “current” alias instead of release location as this will change with every new deployment.

    Can we use alias path in nginx conf? Am I able to have the try_files statement in nginx check folders outside of the site root? I also did see that there is an “alias” command for nginx conf.

    Any help is appreciated.

    Thanks!

    Plugin Author Mateusz Gbiorczyk

    (@mateuszgbiorczyk)

    @morcth, unfortunately, I am not able to help you here.

    I do not have much experience in the configuration server nginx, but I assume that if the directory /uploads will be the root /app directory it will not work.

    Please contact the Administrator of your server.

    Thread Starter morcth

    (@morcth)

    Sadly, I am the admin.

    I just saw this:

    add_action(‘init’, function()
    {
    do_action(‘webpc_regenerate_all’);
    });

    I’m thinking of running a cronjob to check for the uploads-webpc folder every 15 minutes or so. If there, do nothing. If not there, then run the regenerate all action.

    So at max, 15 minutes after a new deploy, it will check and see no webp folder and regenerate all.

    Can you think of any issues this might cause?

    Thanks!

    Plugin Author Mateusz Gbiorczyk

    (@mateuszgbiorczyk)

    By running the action webpc_regenerate_all, only new images will be converted. The existing ones will not be converted a second time.

    The following code should help you:

    add_action('webpc_regeneration', function() {
      do_action('webpc_regenerate_all');
    });
    
    add_action('init', function() {
      if (wp_next_scheduled('webpc_regeneration')) return;
      wp_schedule_event(time(), 'hourly', 'webpc_regeneration');
    });

    Everything should run smoothly in the background.

    Thread Starter morcth

    (@morcth)

    Ok great… so I don’t have to check if the folder exists… just if the cron does.

    Every hour it will run and 99% of the time have nothing to convert and once in a while, due to a new deploy, it will not find the uploads-webpc folder at all and will convert all.

    Love it.

    Will implement tomorrow and report back.

    Thanks!

    • This reply was modified 4 years, 7 months ago by morcth.
    Thread Starter morcth

    (@morcth)

    All seems to be working well!

    Thanks again!

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Upload folder alias – Almost working’ is closed to new replies.