rewrite in custom 404 location

Артём Томюк atomyuk at
Tue Jun 27 10:40:08 UTC 2017

is it possible to create "if" rule with rewrite action inside custom 404
the task is fallback to jpg if upstream returns 404 for .webp request.
i've double-checked regexp's - they seems to be write....

location ~* \.(jpg|mp4|svg|jpeg|gif|png|css|bmp|js|swf|webp|jp2|ico)$ {

etag on;

        proxy_cache site;

        proxy_cache_valid  404 302 1m;

        expires max;

        proxy_cache_valid 2h;

        proxy_pass http://cdn;

        proxy_intercept_errors on;

        proxy_connect_timeout 5s;

        proxy_next_upstream error timeout http_404 http_403 http_500
http_502 http_503 http_504;

        error_page 404 /webp.html;


location = /webp.html {

        if ($request_filename ~* ^.+.webp$) {

                rewrite ^/(.*)\.webp$ /$1.jpg redirect;


        root /var/www/;

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the nginx mailing list