Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax for Submitting Subrequests to FastCGI #476

Open
lancedockins opened this issue Feb 20, 2022 · 9 comments
Open

Syntax for Submitting Subrequests to FastCGI #476

lancedockins opened this issue Feb 20, 2022 · 9 comments

Comments

@lancedockins
Copy link

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.

@xeioex
Copy link
Contributor

xeioex commented Feb 21, 2022

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.

@lancedockins
Copy link
Author

What is the syntax for that?

@xeioex
Copy link
Contributor

xeioex commented Feb 21, 2022

@lancedockins

Take a looks at fastcgi_pass.

@lancedockins
Copy link
Author

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.

@xeioex
Copy link
Contributor

xeioex commented Feb 21, 2022

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};

@lancedockins
Copy link
Author

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.

@xeioex
Copy link
Contributor

xeioex commented Feb 21, 2022

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
Copy link
Contributor

xeioex commented Feb 21, 2022

@lancedockins

May be you need to set additional params, and here.

@lancedockins
Copy link
Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants