Nginx autoindex format based on value of Accept header

I’d like to set autoindex_format value (html, xml, json) according to value of Accept HTTP header received in a request.

I’ve tried to do that with map, but get emerg during start Nginx:

nginx: [emerg] invalid value "$autoindex_type" in /etc/nginx/nginx.conf
http {
    ...
    map $http_accept $autoindex_type {
        default html;
        ~json json;
        ~xml xml;
    }
    ...
    location / {
        root /usr/share/nginx/files;
        autoindex on;
        autoindex_format $autoindex_type;
    }
}

Could somebody advise what proper way to set value for autoindex_format according to Accept value of HTTP request in Nginx?

Thanks!

Read more here: Source link