Convert NGINX var from uppercase to lowercase

I need a little help with nginx conf setup. My config is basically this...

map $http_apikey $api_client_name {
    default "";

    "CLIENT_ID" "client_one";
}

server {
  access_log /dev/stdout main;

  listen 443 ssl;
  server_name localhost;

  # TLS config
  ssl_certificate      /etc/nginx/ssl/cert.pem;
  ssl_certificate_key  /etc/nginx/ssl/key.pem;
  ssl_session_cache    shared:SSL:10m;
  ssl_session_timeout  5m;
  ssl_ciphers          HIGH:!aNULL:!MD5;
  ssl_protocols        TLSv1.2 TLSv1.3;

  proxy_intercept_errors on;     # Do not send backend errors to the client
  default_type application/json; # If no content-type then assume JSON

  location ~ ^/index-$http_apikey {
      if ($http_apikey = "") {
          return 401; # Unauthorized
      }

      if ($api_client_name = "") {
          return 403; # Forbidden
      }

      proxy_pass http://elasticsearch:9200;
  }

....

The idea is to get the http_apikey from the header information on POST and use it as a part of the link. However the VAR, http_apikey, has upper case letters in it as well as lower case letters and numbers. The URI is expected to be in all lowercase though, so essentially:

  location ~ ^/index-$http_apikey.lower() {
      if ($http_apikey = "") {
          return 401; # Unauthorized
      }

      if ($api_client_name = "") {
          return 403; # Forbidden
      }

      proxy_pass http://elasticsearch:9200;
  }

location ~ ^/index-$http_apikey.lower()

Is there a way to do this in nginx? Like in bash I would just ${http_apikey,,} ... is there an nginx equivalent?

Thanks


Solution 1:

Short version: no, that’s not possible.

Short but optimistic version: that’s not possible when using only nginx, but it’s possible when using lua extension. Or any other programming language nginx extension, like perl.

Long version: so you are trying to write a code inside nginx config files. Although nginx configuration does provide some instruments of programming (setting variables, using conditional branches), it’s config language is not a code (the main difference between programming language and config is that code statements are treated in the order they appear, and config statements effect is independent from it’s position- that’s why there’s that lot of noise around nginx conditional branches (If’s are evil, and similar.) And that’s probably the thing Igor Sysoev and it’s team realized long ago and started to implement nginx full-fledged programming extension - lua, instead of fixing issues in nginx config programming embryo (that’s why it’s partial and incomplete - ifs don’t gave else, the order of ifs and sets is implicit and so on. So as soon you want to put a code inside nginx config files, you should start using lua (or something else). Lua can be added using the openresty nginx version, which contains properly built lua with bunch of extensions and examples, or merely adding thd lua nginx module separately, this depends on the distro you’re using.