starname.me

How Do You Configure CORS for a Public RPC Endpoint?

Configuring Cross-Origin Resource Sharing (CORS) for a public nodes/rpc-endpoint-basics/">RPC endpoint means telling the node's HTTP server which web domains are allowed to make browser-based requests to it. If you run a public RPC node, you set CORS headers so that front-end applications - like a DEX interface or a block explorer - can call your endpoint directly from their users' browsers. Without the correct CORS configuration, those browsers will block the requests, and your endpoint will return errors even though the node itself is healthy.

The core task is editing your node's configuration file or startup flags to include the specific origins you want to permit, then restarting the node for changes to take effect. This applies to Ethereum execution clients like Geth and Erigon, as well as JSON-RPC servers like OpenRPC or a reverse proxy such as Nginx.

What CORS Does for an RPC Endpoint

CORS is a browser security mechanism. When a web page on app.example.com tries to fetch data from your RPC endpoint at https://rpc.example.com, the browser sends a preflight request (an HTTP OPTIONS call) to your server. The server must reply with an Access-Control-Allow-Origin header that includes the requesting domain. If that header is missing or does not match, the browser blocks the actual request.

You only need to configure CORS if you are serving a public endpoint meant to be called from browser-based JavaScript. Wallets like MetaMask, libraries like ethers.js or web3.js running in a browser, and decentralized app front-ends all depend on this. Server-side applications (Python scripts, Node backends, curl) do not enforce CORS; they can call your endpoint regardless.

Step 1: determine your node client's configuration method

Every client has a different way to set CORS. The two most common Ethereum execution clients you will encounter are Geth and Erigon.

For Geth: Geth uses the --http.corsdomain flag. You can pass it multiple times or specify a comma-separated list. For example:

geth --http --http.corsdomain "https://app.example.com" --http.corsdomain "https://explorer.example.org"

If you want to allow any domain (not recommended for a public node, but common for testing), you can use a wildcard:

geth --http --http.corsdomain "*"

For Erigon: Erigon uses the same flag name, but you should check the current documentation because the syntax can shift between releases. As of recent versions, it is:

erigon --http --http.corsdomain "https://app.example.com"

If you are running a different client - like Nethermind or Besu - look for their --http.cors or --cors-origins flags respectively. The principle is identical: specify the exact origin or a pattern.

Step 2: Decide Which Origins to Allow

Do not allow * unless you are running a private, non-sensitive node. A wildcard means any website can make requests to your endpoint. If your endpoint is public and rate-limited, that is fine for basic read calls, but it also lets malicious sites spam your node or probe for vulnerabilities.

For a genuinely public RPC, you typically allow the domains of the applications you know are using it. If you are running a node to support your own DApp, list that DApp's domain. If you are aggregating public RPCs for a service, you might list multiple known front-ends.

You can also use a wildcard subdomain pattern, like https://*.mydapp.io, but be careful: wildcards only match one subdomain level, not deeper paths. And never include a trailing slash; the origin should be the scheme plus the host, without a path.

Step 3: Apply the Configuration

Once you have edited the startup command or the configuration file (often a .toml or .json file in your client's data directory), you must restart the node. A live reload is not standard for CORS changes; the HTTP server reads the setting at boot.

If you are using a systemd service (common on Linux VPS setups), the command would be:

sudo systemctl restart geth

Check the logs after restart to confirm the node started successfully and that your CORS flags were accepted. If you mistyped a domain, the node will usually still start; the error will only appear when a browser tries to connect.

Step 4: Test the CORS Headers

You can test without writing any code using curl. Send an OPTIONS request with an Origin header to your endpoint:

curl -X OPTIONS https://rpc.example.com \
 -H "Origin: https://app.example.com" \
 -H "Access-Control-Request-Method: POST"

Inspect the response headers. You are looking for:

Access-Control-Allow-Origin: https://app.example.com
Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Allow-Headers: Content-Type, Authorization

If the Access-Control-Allow-Origin header either matches your test origin or is *, the configuration is working. If it is missing, the browser will block the request. Note that some clients also require you to explicitly allow the Content-Type: application/json header, which is the standard for RPC POST bodies.

A note on reverse proxies

If you run Nginx or Caddy in front of your node - which is common for SSL termination and load balancing - you may need to set CORS at the proxy level instead of (or in addition to) the node itself. In that case, the node's own CORS setting becomes less important, because the browser only sees the proxy's response. Add the headers in your server block or site configuration:

add_header 'Access-Control-Allow-Origin' 'https://app.example.com' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'Content-Type, Authorization' always;

if ($request_method = 'OPTIONS') {
 return 204;
}

The always parameter ensures the header is sent even on error responses. The OPTIONS short-circuit is necessary because many node clients do not handle preflight requests themselves; the proxy should answer them directly.

When you do not need to touch CORS

If you are only using your RPC endpoint from a backend service, a command-line tool, or a desktop wallet that does not run in the browser, you can skip CORS entirely. Those clients ignore CORS rules. Also, if you are using a third-party provider like Alchemy or Infura, their public endpoints already send permissive CORS headers; you only need to configure CORS if you run your own node.

One more thing: setting Access-Control-Allow-Origin to a specific origin does not make your endpoint private. It only relaxes browser protections. Anyone with curl can still call your endpoint regardless of the origin. If you need to restrict access, use an API key or IP allowlist instead. CORS is not authentication; it is just a gate for browsers.

Not financial advice. starname.me publishes market data and general information about digital assets. Crypto assets are volatile and you can lose everything you put in. Nothing here is a recommendation to buy, sell or hold, and we make no price predictions.

Prices are sourced from third parties and may be delayed or wrong. Verify anything you intend to act on against a primary source.

Back to rpc & nodes