Image Optimization Pipeline: cwebp and Nginx for Faster Page Loads
Images often carry most of the weight on a WordPress page. A single uncompressed photo can weigh more than the HTML, CSS, and JavaScript combined, and a gallery page can carry dozens of them. That is why cwebp nginx image optimization is a practical starting point for a self-hosted site. It gives you a repeatable way to shrink image files at the server level, serve the smaller version when a browser can use it, and keep the original file as a fallback.
This guide walks through that pipeline on a typical LEMP server: install cwebp, convert one image, convert a whole folder, serve WebP from Nginx, and add cache headers. The examples use Ubuntu paths and a WordPress uploads folder.
Why Images Slow Down a Page
A browser cannot paint a page until it has downloaded the resources that page needs. Large images take longer to download, and they compete with stylesheets, scripts, and fonts for the same connection.
Resizing an image in CSS does not fix this. The browser still downloads the full file, even if it shows it at a smaller size. WebP is a modern format that often produces noticeably smaller files than JPEG or PNG at similar visual quality. The exact saving depends on the source file and the quality setting, so check your own files after conversion. For a broader view, read the WordPress Core Web Vitals checklist.
What cwebp Does in This Pipeline
cwebp is the command line encoder for WebP, and it ships with the WebP tools package on Ubuntu and Debian. It reads a JPEG, PNG, or TIFF file and writes a WebP file. You control the quality, the compression method, and the metadata that stays in the output.
In this pipeline, cwebp handles conversion only. Nginx handles delivery. Keeping the two jobs separate makes the setup easier to debug. If a file looks wrong, check the conversion. If the wrong file reaches the browser, check Nginx.
Install cwebp and Convert Your First Image
On Ubuntu or Debian, run:
sudo apt update
sudo apt install webp
cwebp -version
Convert one image to see how the tool behaves. The basic command takes a quality value, an input file, and an output file:
cwebp -q 80 photo.jpg -o photo.webp
The -q flag sets quality for lossy compression, from 0 to 100. A value of 80 is a sensible starting point for photos. Lower values make smaller files, and higher values keep more detail. For a PNG with sharp lines, text, or a logo, try lossless mode instead:
cwebp -lossless logo.png -o logo.webp
Compare the two files with ls -lh, then open the WebP file in a browser at full size. Pay attention to gradients, skin tones, and text edges, because those areas show compression artifacts first. If the image looks soft, raise the quality and convert it again. To convert images to webp with cwebp in a way you can repeat, keep the quality value in a script.
Batch Conversion for a Whole Uploads Folder
A WordPress site can hold hundreds of images. A small shell loop converts every JPEG in the current folder:
for f in *.jpg; do
cwebp -q 80 "$f" -o "$f.webp"
done
This writes photo.jpg.webp next to photo.jpg. That naming matters for the Nginx fallback in the next section, so keep it. Change the pattern to *.jpeg or *.png for other formats, and check a few outputs before you run the loop on the whole uploads tree.
For a recursive conversion across subfolders, use find with your own uploads path:
find /var/www/example.com/wp-content/uploads -type f -name "*.jpg" -exec sh -c 'cwebp -q 80 "$1" -o "$1.webp"' _ {} ;
This leaves the original JPEG files in place, which the fallback needs. Do not delete originals until you have tested Nginx. Run the script with cron, or after each batch of uploads, so new files get a WebP copy without manual work.
cwebp Nginx Image Optimization: Serve WebP with a Fallback
Conversion alone does not change what Nginx sends. A browser still requests photo.jpg, and Nginx still serves that file unless you tell it to check for a WebP copy first. The nginx webp fallback pattern solves this: Nginx looks at the Accept header, and if the browser supports WebP, it tries the WebP file first.
Add a map block in the http context of your Nginx configuration:
map $http_accept $webp_suffix {
default "";
"~*webp" ".webp";
}
Then use that suffix in a location block for images:
location ~* .(jpg|jpeg|png)$ {
add_header Vary Accept;
try_files $uri$webp_suffix $uri =404;
}
A browser that supports WebP sends an Accept header that includes webp, so the suffix becomes .webp and Nginx tries photo.jpg.webp first. A browser without WebP support gets an empty suffix, so Nginx serves the original file. The Vary: Accept header matters too: without it, a cache can serve a WebP file to a browser that cannot read it. See also the guide on serving WebP and AVIF images on WordPress with Nginx.
Test with curl:
curl -s -I -H "Accept: image/webp" https://example.com/wp-content/uploads/photo.jpg
curl -s -I -H "Accept: image/jpeg" https://example.com/wp-content/uploads/photo.jpg
The first request should return content-type: image/webp when the WebP file exists. The second should return the original JPEG. After any change, run sudo nginx -t and reload Nginx with sudo systemctl reload nginx.
Add Cache Headers for Images
Conversion cuts the size of each request, but cache headers cut how often the browser has to make that request at all. This section covers nginx cache headers for images.
Add a location block for image extensions with a long expiry and a public cache directive:
location ~* .(jpg|jpeg|png|webp|avif|gif|svg)$ {
expires 1y;
add_header Cache-Control "public, immutable";
access_log off;
}
The expires 1y directive tells the browser it can reuse the file for a year, and immutable tells the browser the file will not change during that time. That only holds if you never overwrite an image file in place. If you edit an image, upload it under a new name, so browsers fetch the new file.
Nginx picks one location block per request, so two blocks that match the same file can conflict. If you keep them separate, make sure the WebP block also carries the cache headers. For page caching, see the guide to Nginx FastCGI cache for WordPress without a plugin.
Check the Result and Keep Originals Safe
After the pipeline runs, verify it the way a visitor would. Open a page, open developer tools, and look at the network tab. Filter by images and check the file type and size for each one. A WebP file should appear where a JPEG or PNG used to appear. If an image still loads as JPEG, check whether its WebP copy exists and whether the Nginx location block matches its path.
Check from the command line as well, with curl and the WebP Accept header, and use ls -lh to compare sizes for a sample of files. If a particular image barely shrinks or looks worse, keep the original format for that file. A noisy photo, a screenshot with fine text, and a logo behave differently, and one quality value will not suit all three.
Keep the original files. They are your fallback for browsers without WebP support, and your source if you later switch to AVIF or a different quality setting. Back up the uploads folder in your regular backup routine. Image work also pairs with other speed steps: lazy loading images in WordPress keeps below-the-fold images from competing with the first paint, and gzip and Brotli compression in Nginx handles HTML, CSS, and JavaScript while WebP handles images.
Common Mistakes to Avoid
First, do not delete originals right after conversion. The fallback needs them, and a bad batch can leave you with no clean source to reconvert. Second, do not use one quality value for every image. Test a few files from each type and write those values into your conversion script. Third, do not forget the Vary: Accept header, or a cache can serve the wrong format to the wrong browser. Fourth, do not test in only one browser. Check the page in a browser with WebP support and in one without it, or use curl with different Accept headers.
A Pipeline You Can Repeat
A good image pipeline has four parts: convert with cwebp, serve WebP from Nginx with a fallback, set cache headers, and verify with curl and browser tools. Put the conversion command in a script, keep the Nginx blocks in a backup, and run the conversion after each batch of uploads. Start with one folder, check the result, then extend the pattern to the rest of the site.