1

I'm rewriting a Flask application to use NGINX Unit and I am now trying to configure static resources. However I'm using flask blueprints and the folder structure is different from what NGINX Unit expects:

      {
        "match": {
          "uri": "/static/my-module/*"
        },
        "action": {
          "share": "/app/myapp/modules/my-module/static/"
        }
      },

Now what I would like is that everything after /static/my-module/ becomes added to the local path /app/myapp/modules/my-module/static/ like this:

/static/my-module/main.css => /app/myapp/modules/my-module/static/main.css

But what happens is:

/static/my-module/main.css => /app/myapp/modules/my-module/static/static/my-module/main.css

I don't see any way to use regex, or to set make $uri only be the matching part and not the full.

Given the size of the application it's not trivial to change local path. Now I could do exotic things like symlinking but that's a hassle to maintain.

I'm using unit:1.26.1-python3.9

Niklas B
  • 1,839
  • 18
  • 36

2 Answers2

2

This is not possible today. In the future you will be able to specify URI rewrites to reconstruct the URI to match the new layout.

For now you can mitigate the symlinking pain by using the $uri variable explicitly.

"share": "/app/myapp/modules/my-module$uri"

And

$ cd /app/myapp/modules/my-module/static
$ ln -s my-module .
1

Since 1.29 you can do this by using inline JavaScript expressions to modify the original URI.

This configuration strips the first 17 characters from the original URI before performing the "share". Note the template literal formatting to invoke the JavaScript.

    {
      "match": {
        "uri": "/static/my-module/*"
      },
      "action": {
        "share": "`/app/myapp/modules/my-module/static${uri.substring(17)}`"
      }
    }
  • This requires Unit to be built with support for NGINX JavaScript, otherwise you'll get an error when submitting the config with a template literal. – Magnus Lind Oxlund Jun 30 '23 at 10:19