nginxaxiosnext.jsreverse-proxymoralis

Configuring proxy_pass multiple params w/ dynamic route + API call


I need to configure a reverse proxy for a dynamic route that appends a variable chainId to an axios request to an outside moralis API. Getting a 502 Bad Gateway.

Server Route Structure

/moralis/ERC20/${token.address}?chainId=${chainId}

Route Implementation

app.get('/moralis/ERC20/:address', async (req, res) => {
    ...

    try {
      const chainId = req.query.chainId ?? 1;
      const chainIdHex = '0x' + chainId.toString(16);

      const response = await rateLimit.fetchAndRetry(() =>
        rateLimit.acquireToken(req, 'moralis', 3, () =>
          axios.get(
            `https://deep-index.moralis.io/api/v2/erc20/${req.params.address}/price?chain=${chainIdHex}`,
          ),
        ),
      );
     
    }
  });

NGINX config

    location ~* ^/moralis/ERC20/(.*) {
      proxy_pass http://127.0.0.1:4000$request_uri;
    }
  1. Do I need a reverse proxy for the axios call?
  2. Am I actually capturing the entire request_uri?

Solution

  • You don't need neither a regex location, nor a $request_uri part. Just use a simple prefix location and do not add anything after the upstream address, your request URI will be passed "as-is":

    location /moralis/ERC20/ {
        proxy_pass http://127.0.0.1:4000;
    }
    

    Do not add the trailing slash after the upstream name! It will be considered as a proxied URI part, completely changing the proxy_pass directive behavior. If you have regex locations that could potentially take over that /moralis/ECR20/... request URI, you can use a ^~ location modifier:

    location ^~ /moralis/ERC20/ {
        proxy_pass http://127.0.0.1:4000;
    }
    

    Read location and proxy_pass directives documentation to find out somewhat more complete explanation, there are not so many words to read.