'Nginx Map Regex

I am trying to return a 415 error when the content_type is not application/json using Nginx which means I need to allow all traffic which is of content_type application/json using maps I have achieved the below

map $content_type $ct_check{
"application/json" 'pass';
~*(.*)'failed';
default      'pass';

However my end customers sends multiple content type like application/json;charset-utf8 something like this so I need to have a regex to allow all the content_types starting with application/json tried multiple combinations but no luck



Solution 1:[1]

To test if $content_type begins with application/json, use a regular expression containing the "start of string anchor" (^).

For example:

~*^application/json  pass;

Place this regular expression above your ~*(.*) 'failed'; statement, as regular expressions are evaluated in order.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Richard Smith