There are several compelling reasons why people might want to improve API performance:
We will be setting up a Proxy as a central management system to achieve improved API performance. A proxy acts as an intermediary between clients and servers. It sits between the client making API requests and the server that hosts the APIs. When a client makes an API request, it goes through the proxy first, which then forwards the request to the server. The server processes the request and sends the response back to the proxy, which then forwards it to the client. This allows the proxy to intercept, modify, or cache the request or response as needed, providing opportunities to optimize API performance.
For macOS (or Linux), install Homebrew on your system.
There are following steps to install the Nginx on macOS:
1️⃣Download Homebrew
To install the Nginx on macOS, Homebrew must be installed on the system. Homebrew is a package manager for Mac operating system that allows us to install various Unix applications easily. If you don't have Homebrew, use the following link to install: https://brew.sh/
Or simply type the following command on the terminal:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
2️⃣Install Nginx
The homebrew package installer will help to install the Nginx web server on the macOS. To install the Nginx, use the following command:
brew install nginx
3️⃣Edit configuration file
By default, the location of Nginx configuration file is:
/opt/homebrew/etc/nginx/nginx.conf
To edit the Nginx configuration file, you can use any text editor of your choice. For example, you can use nano, vim, or emacs. Here's an example command to edit the Nginx configuration file using nano:
nano /opt/homebrew/etc/nginx/nginx.conf
We will be editing the server block that listens on port 80.
Search for server block
Subsequently, proceed to include the three Ethereum mainnet RPC endpoints below:
location /nodereal {
proxy_pass https://eth-mainnet.nodereal.io/v1/<API KEY>;
proxy_set_header Content-Type "application/json";
}
location /RPCProviderA {
proxy_pass <https URI endpoint>;
proxy_set_header Content-Type "application/json";
}
location /RPCProviderB {
proxy_pass <https URI endpoint>;
proxy_set_header Content-Type "application/json";
}
It is possible to include multiple RPC endpoints as necessary and subsequently preserve the configuration file.
To ensure the absence of syntax errors, kindly proceed with the testing of the Nginx configuration file:
nginx -t
🎊 In the event that there are no errors present, the following outcome shall be displayed.
nginx: the configuration file /opt/homebrew/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /opt/homebrew/etc/nginx/nginx.conf test is successful
To restart the Nginx server, kindly execute the following command:
brew services restart nginx
4️⃣Sending API method via Nginx Proxy
To test the Nginx Proxy, we are checking the gas price on Ethereum via eth_gasPrice. We will be sending a curl command to send an HTTP POST request to the "/nodereal" location of a server running on the local machine (at "http://localhost") with a JSON payload seen below:
curl -X POST --data '{"jsonrpc":"2.0","method":"eth_gasPrice","params":[],"id":1}' -H "Content-Type: application/json" http://localhost/nodereal
✅ {"jsonrpc":"2.0","id":1,"result":"0xdec36a8d1"}
The response you received after running the curl command is a JSON-RPC response from an Ethereum node. Here's a brief explanation of the response:
The decimal value of "0xdec36a8d1" is 59797579985. Therefore, the current gas price on the Ethereum network at the time the request was made was 59797579985 wei (the smallest denomination of Ether) or 58 Gwei.
Test it out with the remaining servers, /RPCProviderA & /RPCProviderB, by running on the local machine (at "http://localhost"):
curl -X POST --data '{"jsonrpc":"2.0","method":"eth_gasPrice","params":[],"id":1}' -H "Content-Type: application/json" http://localhost/RPCProviderA
🥳 You may commence the testing of your own proxy.
✅Advantages of using Nginx as a proxy for an API gateway:
❌ Disadvantages of using Nginx as a proxy for an API gateway:
In conclusion, improving API performance is crucial for businesses and developers. Using Nginx as a proxy for API gateway offers advantages like load balancing, caching, scalability, and security. However, there are limitations such as limited API management features, configuration complexity, and lack of advanced authentication and authorization capabilities. Careful consideration of these pros and cons is essential. Overall, leveraging Nginx as a proxy can be a powerful tool to improve API performance. Stay tuned for the next tutorial series as we will be sharing more about the common issue faced and how to debug it.
Also published here.