When preparing to launch a website, or debugging problems with a site that's already live, sometimes it can be helpful to bypass CDN and proxy layers when requesting content from the site by sending those web requests directly to a specific IP address without using the site's public DNS records. This practice of "pinning" a web request directly to a server can be accomplished by changing your /etc/hosts file, which will cause requests for a specified domain name (e.g: "www.example.com ") to be routed from your local machine to a specified IP address (e.g: 127.0.0.1) until the changes you've made to /etc/hosts are reverted.
However, what if you want to pin a single request to an IP address, without modifying your system's configuration files? Happily, this sort of "ad-hoc" request-pinning is possible via command-line with cURL , which provides a special
resolve option, formatted
--resolve [DOMAIN]:[PORT]:[IP], that routes all web requests performed during the execution of a cURL command that match a given [
DOMAIN] and [
PORT] to a specified [
IP] address. The values specified by this option (which can be invoked multiple times in a single command to route multiple domain/port combinations to various IP addresses) will apply to the initial request, and also to any redirects that cURL follows during the course of the command.
The following curl command:
curl http://www.example.com --resolve www.example.com:80:127.0.0.1
...will force cURL to use "127.0.0.1" as the IP address when requesting "www.example.com " over port 80 (HTTP).
The command above can be augmented to look like this:
curl http://www.example.com --resolve www.example.com:80:127.0.0.1 --resolve www.example.com:443:127.0.0.1
...which will force cURL to use "127.0.0.1" as the IP address for requests to "www.example.com " over ports 80 (HTTP and 443 (HTTPS). This can be useful for sites that automatically redirect HTTP requests to HTTPS requests as a security measure.
--resolve can be specified multiple times (and for multiple domain/port combinations) for a single cURL command, allowing you to establish complex routing rules for requests that you know will be redirected multiple times across various domains and ports.