Set up a simple reverse proxy
In this tutorial, you'll use Caddy to set up a reverse proxy to route data from Infura to your own node.
Prerequisites
- An Ethereum project on Infura
- Node.js installed
- Homebrew installed
Steps
1. Create a project directory
Create a new directory for your project. You can do this from the command line:
mkdir reverseProxy
Change into the new directory:
cd reverseProxy
2. Install Caddy
Install Caddy in the project directory using Homebrew:
brew install caddy
3. Create the reverse proxy
To create the reverse proxy, create a text file named Caddyfile
with the following content:
localhost
reverse_proxy https://goerli.infura.io {
header_up Host
}
Ensure you replace <YOUR_API_KEY>
with the API key for your Ethereum project.
In this example, the reverse proxy retrieves information from the Infura Goerli endpoint, and redirects it to localhost
. Using header_up Host
allows you to include your API key to both the Goerli and localhost endpoints.
4. Run the reverse proxy
In a new terminal window, from your project directory, run the reverse proxy using Caddy:
caddy run
5. Make a request
In a new terminal window, make a curl request to localhost
. The following example executes a web3_clientVersion
request:
- Example curl HTTPS request
- Example JS result
curl https://localhost/v3/<YOUR_API_KEY> \
-X POST \
-H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "method": "web3_clientVersion", "params": [], "id": 1}'
{"jsonrpc": "2.0", "id": 1, "result": "Geth/v1.10.8-omnibus-aef5bfb3/linux-amd64/go1.16.7"}
To ensure that the reverse proxy is working, execute the same request, replacing localhost
with goerli.infura.io
. You should get the same result:
- Example curl HTTPS request
- Example JS result
curl https://goerli.infura.io/v3/<YOUR_API_KEY> \
-X POST \
-H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "method": "web3_clientVersion", "params": [], "id": 1}'
{"jsonrpc": "2.0", "id": 1, "result": "Geth/v1.10.8-omnibus-aef5bfb3/linux-amd64/go1.16.7"}