简体   繁体   中英

How can I use the result api response to the proxy_pass attribut

My client come with an encrypted URI on the nginx server. NGINX have to decrypt it by sending a request to an API. The result of this API call is the URI decrypted that is use to be the proxy URI.

I've use NJS pluging for NGINX to make the API call, but I catch this error "async operation inside "redirect_uri" variable handler"

http{
    js_set $redirect_uri redirect_uri;
    server {
        location ~ ^/upload/(.*)$ {
            proxy_request_buffering off;
            resolver 127.0.0.1 ipv6=off;
            proxy_pass '$redirect_uri';
        }
    }
}
function redirect_uri(r) {
        r.uri.replace("/upload/", "");
        r.subrequest('/api/decrypt-uri/'+hash, { method: 'POST' }, function(res) {
        if (res.status != 200) {
            r.return(res.status);
            return;
        }
    var data=res.responseBody.toString();

        return data;
    });
}```



2019/07/30 15:55:07 [error] 10667#10667: *1 async operation inside "redirect_uri" variable handler, client: 127.0.0.1, server: api, request: "POST /upload/aHR0cHM6Ly9rbm94LmJpZ2RhdGEuaW5mcmEuZGdmaXAvZ2F0ZXdheS9kZWZhdWx0L3dlYmhkZnybXF5OHYtMDdWeFVaQ1JheFFTRTMxeTJ2Z0hhOHFmV002WkhsRzItQWtRLXVRd2ZvblZpX2lyMDlxZU9IZUtMWHdyejdNTnFJeEdMdFZqSk0= HTTP/1.1"

This looks similar to a question asked here: https://github.com/nginx/njs/issues/213 , with the answer:

r.subrequest() (or any async functions like setTimeout()) is not supported inside js_set variables handler. you need to use auth_request directive. See a related example .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM