node.js - Nginx - Conditionally serve WebP via Nginx proxy -


trying conditionally serve webp images in browsers accept mime type. i'm having issues implementing known solution existing nginx setup.

i'm attempting use below method of implementing webp:

  map $http_accept $webp_suffix {     "~*webp"  ".webp";   }    server {     listen       8081;     server_name  localhost;      location / {       try_files $uri$webp_suffix $uri =404;     }   } 

from here

but trying find way use existing nginx config has been getting me nothing errors. existing config (nginx proxy on aws elastic beanstalk node.js) appears reverse proxied node.js process.

where need put in existing nginx configuration below:

upstream nodejs {     server 127.0.0.1:8081;     keepalive 256; }  server {     listen 8080;      location / {         proxy_pass  http://nodejs;         proxy_set_header   connection "";         proxy_http_version 1.1;         proxy_set_header        host            $host;         proxy_set_header        x-real-ip       $remote_addr;         proxy_set_header        x-forwarded-for $proxy_add_x_forwarded_for;     } } 

how impliment try_files directive in above nginx config, in order conditionally serve webp images?

use named location default action of try_files directive (rather =404 response). see this document details. example:

map $http_accept $webp_suffix {     "~*webp"  ".webp"; }  upstream nodejs {     server 127.0.0.1:8081;     keepalive 256; }  server {     listen 8080;      root /path/to/document/root;      location / {         try_files $uri$webp_suffix $uri @nodejs;     }      location @nodejs {         proxy_pass http://nodejs;         proxy_set_header connection "";         proxy_http_version 1.1;         proxy_set_header host            $host;         proxy_set_header x-real-ip       $remote_addr;         proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;     } } 

if static file found in directory structure below /path/to/document/root, conditionally .webp appended, nginx serve file. otherwise control passed named location , uri sent node.js process.


Comments