Nginx Map Regex

Viewed 386

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

1 Answers

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.

Related