webp

The 'webp' directive enables on-the-fly conversion of JPEG/PNG images to the WebP format for supported clients.

Syntaxwebp;
Defaultnone
Contextlocation
Argumentsnone

Description

The 'webp' directive allows NGINX to serve images in the WebP format based on the client's Accept header. When a client requests an image, if the Accept header includes 'image/webp', the module triggers a conversion process for JPEG or PNG images on the fly. This is achieved by invoking the 'cwebp' tool, a command-line utility that converts images to the WebP format, which is advantageous for its smaller file sizes and efficient compression.

When an image in a location containing this directive is requested, NGINX checks the specified Accept header. If the client supports WebP, the module attempts to create a new WebP version of the requested JPEG or PNG file. It does so by forking a new process that executes the 'cwebp' tool with the appropriate file paths. The output is then served to the client, provided the conversion is successful. If the conversion fails, NGINX defaults to serving the original image format, ensuring that clients always receive a valid image response.

When configuring the directive, it is crucial to include the necessary path for the 'cwebp' tool and ensure adequate permissions for the associated image files. It is also essential to consider caching strategies, as image conversion can introduce latency. Implementing caching solutions will help mitigate performance impacts associated with on-the-fly processing.

Config Example

location ~ '\.jpg$' {
    webp;
}

Ensure the 'webp' script is correctly located in '/etc/nginx/' and is executable.

The 'cwebp' tool must be properly installed and accessible to NGINX.

Performance may be impacted during image conversion; consider caching strategies to mitigate this.

← Back to all directives