server-side/nginx

nginx uri, query, POST body 컨텐츠 값으로 reverseProxy

C/H 2023. 11. 2. 02:32
# /etc/nginx/site-enable/custom-host.conf
http {
  js_path "/etc/nginx/njs/";

  js_import main from authorize.js;

  upstream backend1 {
      server 127.0.0.1:8081;
  }


  upstream backend2 {
      server 127.0.0.1:8082;
  }

  server {
      listen 80;

      location / {
          js_content main.authorize;
      }

      location @app-backend1 {
          proxy_pass http://backend1;
      }

      location @app-backend2 {
          proxy_pass http://backend2;
      }
  }
}
// /etc/nginx/njs/authorize.js
function authorize(r) {
  var body  = r.requestText;
  if (r.uri.indexOf('someValue') > -1 || JSON.stringify(r.args).indexOf('someValue') > -1 || String(body).indexOf('someValue') > -1)
  {
    r.internalRedirect('@app-backend1');
  }
  else
  {
    r.internalRedirect('@app-backend2');
  }
}

export default {authorize}
반응형