nginx redirect to output of command

You can use the lua plugin (sudo apt -y install libnginx-mod-http-lua) in order to use this technique to redirect to an URL that is the output of a command line command:

location = /myurl {
    access_by_lua_block  {
        local process = io.popen("curl -fsSL", "r")
        local output = process:read('*a')

By default, this redirects using the status code 302 (temporary redirect – not cached). In case you want a 301 (moved permanently) instead, use

ngx.redirect(output, 301)