NJS - API Calls
    Dmitry Volyntsev 
    xeioex at nginx.com
       
    Tue Jun  4 12:55:38 UTC 2019
    
    
  
On 04.06.2019 14:00, naidile.pn wrote:
> What does api write=on; mean ? Could you please explain.
> 
api is a plus-only nginx module 
(http://nginx.org/en/docs/http/ngx_http_api_module.html).
You do not need this to make subrequests.
> On Tue, Jun 4, 2019 at 4:26 PM naidile.pn <http://naidile.pn> 
> <naidilepn at gmail.com <mailto:naidilepn at gmail.com>> wrote:
> 
>     Hi Dmitry,
> 
>     I tried subrequest. But it doesn't work. Nginx is trying find the
>     API within /var/www/html folder.
Can you elaborate more what are you trying to do?
See the config below as an example:
-----------------
http {
     js_include proxy.js;
     js_set $summary summary;
     js_set $prop prop;
     # proxy server
     server {
         listen       8080;
         server_name  localhost;
         location / {
             js_content proxy;
         }
         location = /_prop {
             internal;
             proxy_pass http://127.0.0.1:8090;
         }
         location = /_proxy {
             internal;
             proxy_set_header Prop $prop;
             proxy_pass http://127.0.0.1:8070$request_uri;
         }
     }
     # prop server
     server {
         listen       8090;
         location / {
             return 200 '{"prop": "WAKA"}';
         }
     }
     # dest server
     server {
         listen       8070;
         location / {
             return 200 $summary;
         }
     }
}
-----------------
function proxy(r) {
	r.subrequest("/_prop", reply => {
		if (reply.status != 200) {
			r.return(500);
			return;
		}
		r.variables.prop = JSON.parse(reply.responseBody).prop;
		r.subrequest("/_proxy", res => {
			r.return(res.status != 200 ? 500 : 200,
					 res.responseBody);
		})
	});
}
function summary(r) {
	var s = "JS summary\n\n";
     s += "Method: " + r.method + "\n";
     s += "HTTP version: " + r.httpVersion + "\n";
     s += "Host: " + r.headersIn.host + "\n";
     s += "Remote Address: " + r.remoteAddress + "\n";
     s += "URI: " + r.uri + "\n";
     s += "Headers:\n";
     for (var h in r.headersIn) {
         s += "  header '" + h + "' is '" + r.headersIn[h] + "'\n";
     }
	return s;
}
var prop = (r) => r.variables.prop;
-----------------
curl http://127.0.0.1:8080/test
JS summary
Method: GET
HTTP version: 1.0
Host: 127.0.0.1:8070
Remote Address: 127.0.0.1
URI: /test
Headers:
  header 'Prop' is 'WAKA'
  header 'Host' is '127.0.0.1:8070'
  header 'Connection' is 'close'
  header 'User-Agent' is 'curl/7.64.0'
  header 'Accept' is '*/*'
-----------------
> 
>     And, with proxy_pass within subrequest, I'm not able to pass the
>     request headers and body. Kindly help here.
    
    
More information about the nginx-devel
mailing list