Skip to content

Conversation

xeioex
Copy link
Contributor

@xeioex xeioex commented Oct 11, 2025

This patch adds forward proxy support to the ngx.fetch() method in njs, enabling HTTP and HTTPS requests through a proxy server with optional
authentication.

Key Features:

  • HTTP proxying: Uses absolute-form URIs when sending requests through proxy
  • HTTPS tunneling: Implements CONNECT method for HTTPS requests through proxy
  • Proxy authentication: Supports Basic authentication via Proxy-Authorization header
  • Keepalive support: Works with existing js_fetch_keepalive directive

New Directives:

  • js_fetch_proxy - Configures forward proxy URL

Example nginx.conf:

...
  http {
      js_import script.js;

      server {
          listen 8080;
          
          resolver   127.0.0.1:5353;

          # Configure forward proxy
          location /api {
              js_fetch_proxy http://user:[email protected]:3128;
              js_content script.fetch_data;
          }
      }
  }

JavaScript usage:

  async function fetch_data(r) {
      // Will automatically use configured proxy
      let reply = await ngx.fetch('https://api.example.com/data');
      let body = await reply.text();
      r.return(200, body);
  }
  
 export default {fetch_data};
  

@xeioex xeioex force-pushed the forward_proxy branch 8 times, most recently from 438861e to fb71984 Compare October 15, 2025 01:39
@xeioex xeioex force-pushed the forward_proxy branch 5 times, most recently from 3009f25 to 8ee857b Compare October 16, 2025 02:17
@xeioex xeioex marked this pull request as ready for review October 16, 2025 02:47
@xeioex xeioex requested a review from VadimZhestikov October 16, 2025 02:47
Copy link
Contributor

@VadimZhestikov VadimZhestikov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically looks good, except for few comments.

May be it would be good discuss solution with Ivan, because it is bit different from #956

Supports Basic authentication via Proxy-Authorization header.

- js_fetch_proxy - configures forward proxy URL

example.conf:
...
  http {
      js_import main.js;

      server {
          listen 8080;

          resolver   127.0.0.1:5353;

          location /api {
              js_fetch_proxy http://user:[email protected]:3128;
              js_content main.fetch_handler;
          }
      }
  }

This implements nginx#956 feature request on Github.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants