Syntax for Submitting Subrequests to FastCGI
Is there a syntax to submit subrequests to the FastCGI backend?
For example, if you want to use js_content to do some work before the content is retrieved, issue a subrequest to a FastCGI backend, and then do some work on the response (that can't be done by FastCGI and what it hands off to), then a subrequest to FastCGI would be needed to do that.
Right now, the documentation seems to imply that subrequests only work for proxy style locations. But is there a way to route the subrequest to a FastCGI backed?
I've tried using a subrequest to a named location, but that doesn't seem to work (it only works for internal redirection). Is there something that is supported like the below as a URI?
fastcgi://path/to/socket fastcgi://ip:port
We use socket mode if that is relevant. But ultimately, I'm just looking for a way to issue a subrequest to a FastCGI backend.
Is there a syntax to submit subrequests to the FastCGI backend?
Making a subrequest to fastcgi_pass location works as well. r.subrequest() operates with nginx locations, so any location will work.
What is the syntax for that?
So you're saying that the same syntax applies in the subrequest method? Just confirming. I am already familiar with that syntax but was having a bit of trouble implementing.
So you're saying that the same syntax applies in the subrequest method?
r.subrequest() takes as its first argument a nginx location,
location /secure {
js_content main.handler;
}
location /fastcgi {
fastcgi_pass fastcgi://path/to/socket;
}
function handler(r) {
let reply = await r.subrequest('/fastcgi');
...
}
export default {handler};
Odd. I actually tried this exact thing. It didn't work. That is why I wrote the issue. The subrequest attempted to go to docroot/fastcgi rather than the actual fastcgi backend.
Hi @lancedockins,
I am not very familiar with fastcgi, can you please provide me with complete, but minimal example? What you do, what you expect, and what you get. I will take a look at it.
But, from njs perspective there is no difference between locations. Only a content phase directive (proxy_pass, fastcgi_pass, ...) at the specified location decides what to do with the incoming request.
@xeioex Just for context, I'm actually quite familiar with fastcgi_pass and related params. I've been using it successfully for years. This doesn't have anything to do with fastcgi params. That's for certain.
The ONLY thing that I'm trying to do with this is pass a subrequest to a fastcgi backend. It's essentially just a replacement for doing fastcgi_pass just like you can do with subrequests as a replacement for proxy_pass.
I've tried a few different methods:
Using an internal location and passing the request directly to the location: e.g. let res = await r.subrequest('/internal-location');
Using the unix socket name e.g. let res = await r.subrequest('unix:/path/to/sock');
Using a named location e.g. let res = await r.subrequest('@location');
All of these ultimately fail. In every case, they are trying to go to document root/subrequest location. That sort of path is relevant for a URL. It's not relevant for a FastCGI backend. That's why I was asking about the syntax.
The failures are like this:
internal location: Either fails with 404 or 403 due to the security limits inherent in how fastcgi works (the request never makes it to the backend)
named location: 404 - /docroot/@location doesn't exist
unix socket: 404 - /docroot/unix:path/to/sock doesn't exist
As far as I can see, NJS subrequest seems to only work for proxy. It doesn't seem to support anything other than proxy right now.