Score:1

Convert NGINX var from uppercase to lowercase

co flag

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

Gerard H. Pille avatar
in flag
What's a "VAR" ?
Score:1
za flag

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.

Nick Hatfield avatar
co flag
Thanks for the insight. I'm using the latest nginx docker container which has version 1.14.2. Can you give an example of how to solve the above problem with perl or lua? Thanks
drookie avatar
za flag
perl example is under the first link I gave.
Nick Hatfield avatar
co flag
Awesome, thanks!!
Nick Hatfield avatar
co flag
Hey, me again... so the perl example seems to only show and fit a use case where the client is sending a URI like `SoMeThInG` and the perl command will convert the `nginx.var.uri`, to a lower case string. I'm having a hard time getting this to work with the scenario I describe above where the user is sending in an HTTP header `-H "apikey: SoM3RaNd0m"`, and we need to capture this and make it lowercase and then send it to the location and have it match as lowercase there. `http_apikey: S0m3RaD0m3` `(perl => http_apikey_lowercase)` `location ~ ^/hnt-$http_apikey_lowercase { }`
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.