How to Serve WebP Images on Nginx

After the images are optimized, they are served as webp on Apache servers automatically. However, this situation is not possible on nginx servers, so it is necessary to set it manually.

By default the config file is /etc/nginx/nginx.conf and thanks to the include /etc/nginx/conf.d/*.conf; line in this config file, all files in the /etc/nginx/conf.d folder are included.

You need to create a file called webp.conf in the /etc/nginx/conf.d/ directory and paste the code below.

Finally, you need to find the config file of the site. Since the path of the config file may change depending on the control panel, we cannot say the config file path. After you find the config file, you can paste the code below inside of the server{} section.

If the browser caching rules exists in the config file, the webp rules must be added before the browser caching rule because the browser caching rule prevents the webp rules from working.