리디렉션하기 전에 js 스크립트를 실행하여 requestBody를 업데이트하세요.

리디렉션하기 전에 js 스크립트를 실행하여 requestBody를 업데이트하세요.

nginx 위치를 구성 중이지만 리디렉션 전에 requestBody를 업데이트하는 스크립트(njs)를 실행해야 합니다.

이것은 내 conf이지만 배포할 때 requestBody를 업데이트하지 않고 여전히 리디렉션하거나 오류를 반환합니다.

상위 nginx.conf:

     // .....
     js_import checkScript from  /etc/nginx/js/scripts/checkScript.js;
     // ....

checkScript.js에서:

export default {
    rights
}

function rights(r) {
    const body = JSON.parse(r.requestBody);
    if (body.isAdmin) {
        body.rights = ['ADMIN'];
        r.requestBody = JSON.stringify(body);
    } else {
        r.return(403, 'Not admin');
    }
}

products.http-service.conf :

location /api/data/products/new {
    set $gateway_role "dev.yumStore";
    set $gateway_realm "yumStore";

    auth_request /_tokenExchange;

    # check rights and update body
    js_content checkScript.rights;

    proxy_set_header "Authorization" $gateway_auth_header;

    # redirection
    proxy_pass $OUTGATEWAY/api/data/products/new;
}

도와 주셔서 감사합니다!!

답변1

함수에서 JSON 문서의 로컬 복사본을 업데이트하고 있습니다.

이것이 허용되는지는 모르겠지만 다음을 시도해 볼 수 있습니다.

function rights(r) {
    const body = JSON.parse(r.requestBody);
    if (body.isAdmin) {
        body.rights = ['ADMIN']
        r.requestBody = JSON.stringify(body); // I don't know if nginx JS allows overwriting the requestBody...
    } else {
        r.return(403, 'Not admin');
    }
}

이 접근 방식은 우회하기 매우 간단하기 때문에 이 외에도 관리 부분에 대한 적절한 인증이 있기를 바랍니다.

답변2

본문을 nginx로 보내고 detrevninjs 모듈은 문자열을 로 반전시키고 inverted, Proxy_pass는 로 응답할 업스트림 서버로 보냅니다 echoing: inverted.

다음 구성은 위 테스트에 작동합니다. 도움이 되길 바랍니다.

default.conf:

js_import main from invert.js;

server {
    
    location /ping {
        return 200 'pong';
    }

    location / {
        js_content main.invert;
    }

    location /api {
        proxy_pass http://host.docker.internal:5015;
    }
}

invert.js

async function invert(r) {
  let body = r.requestText;
  let inverted = body.split("").reverse().join("");
  let res = await r.subrequest("/api", { body: inverted });
  r.return(res.status, res.responseBody);
}

export default { invert };

API

const server = new Server(5015, async (request, response) => {
  const incoming = await payload(request);
  const answer = `echoing: ${incoming}`;
  console.log("answering with", answer);
  response.writeHead(200, { "content-type": "text/plain" });
  response.write(answer);
  response.end();
});

코드는 여기에서 찾을 수 있습니다:https://github.com/ericminio/learning-nginx/tree/master/njs-modify-body

관련 정보