In Colly, a proxy is a proxy function attached to the collector's (Collector) HTTP client. c.SetProxy binds a single address, while c.SetProxyFunc binds a function that picks an address for every request. Both modify the underlying http.Transport and turn keep-alive off. The trap is ordering: call WithTransport after the proxy and the proxy is silently wiped. This post covers a single proxy setup, SOCKS5 and credentials, round-robin rotation, seeing which proxy a request left through, and rate limiting with LimitRule.
One proxy and SOCKS5
SetProxy parses the address with url.Parse and attaches an http.ProxyURL function to the collector. In an existing Colly program, this is the only line that changes. If it returns an error, the address could not be parsed, so check the return value.
package main
import (
"log"
"github.com/gocolly/colly/v2"
)
func main() {
c := colly.NewCollector()
if err := c.SetProxy("http://user:pass@pr.proxynet.io:8000"); err != nil {
log.Fatal(err)
}
c.OnHTML("title", func(e *colly.HTMLElement) {
log.Println("title:", e.Text)
})
c.OnError(func(r *colly.Response, err error) {
log.Println("error:", r.StatusCode, err)
})
c.Visit("https://example.com/")
}The address starts with http:// even when the target is HTTPS. Go asks the proxy for a CONNECT tunnel and sets up TLS inside that tunnel. For a plain http:// target, it sends the request to the proxy with the full URL.
Always write the scheme. Colly's documentation says an empty scheme counts as http, but in our test a scheme-less address broke in two different ways. For 127.0.0.1:27181, which starts with an IP, SetProxy returned a parse error and the request went out without a proxy. localhost:27181, which starts with a name, returned no error. Go took the name itself as the scheme, and the request failed with proxyconnect tcp: dial tcp :0.
For SOCKS5, only the scheme changes: c.SetProxy("socks5://user:pass@pr.proxynet.io:8000"). Colly hands this address to Go's built-in SOCKS5 client, and the username and password travel in SOCKS5's own authentication step.
The proxy resolves DNS. According to the Go documentation, the socks5 scheme is handled the same as socks5h. In our test, our SOCKS5 server received a domain name, not an IP, with both schemes. The target name is resolved where the proxy sits.
The Proxynet SOCKS5 Proxy also carries UDP. Colly only makes HTTP requests, so only TCP is used here.
Ordering with Transport
WithTransport replaces the client's Transport as a whole. SetProxy writes the proxy field onto the existing *http.Transport. That is why the order matters.
c := colly.NewCollector()
c.WithTransport(&http.Transport{
TLSHandshakeTimeout: 10 * time.Second,
MaxIdleConns: 10,
})
c.SetProxy("http://user:pass@pr.proxynet.io:8000") // after the TransportIn the reverse order, the new Transport's Proxy field stays empty and the request goes out directly, without an error. In our test, the .test target could only be resolved by the proxy. In the reverse order, the request never reached the proxy log and got stuck at the DNS step. On a real site, the page arrives with your own IP and you do not notice.
If you set no proxy at all, Colly uses Go's default Transport. That one reads the HTTP_PROXY and HTTPS_PROXY variables. In our test, with only HTTP_PROXY set, the request went through the proxy. Once SetProxy is called, these variables are no longer read. Details on environment variables and NO_PROXY are in our post Golang HTTP Proxy: How to Use a Proxy With net/http.
Credentials and 407
On an HTTP proxy, the user:pass in the address becomes a Proxy-Authorization: Basic header that Go adds itself. If the credentials are rejected, the proxy returns 407, and Colly reports it in two different ways.
| Target | r.StatusCode in OnError | Error text |
|---|---|---|
http:// | 407 | Proxy Authentication Required |
https:// | 0 | Get "https://…": Proxy Authentication Required |
On HTTPS, the 407 arrives during the CONNECT stage and never becomes a response object. If you only check r.StatusCode == 407, you miss HTTPS failures, so check the error text too. Causes and fixes for 407 are in our post How to Fix HTTP 407 Proxy Authentication Required.
If the password contains @, # or /, use percent-encoding: pass%40123 instead of pass@123. In our test, the encoded password authenticated without issues.
Rotation and RoundRobinProxySwitcher
Rotation in Colly comes from one of two places: a rotating gateway or RoundRobinProxySwitcher. Both rest on the same setting. SetProxy and SetProxyFunc set DisableKeepAlives to true on the Transport. This is hard-coded in Colly's source, and every request opens a new connection to the proxy.
In our test, two HTTPS requests to the same host left two separate CONNECT lines in the proxy log. On a rotating gateway, the exit is picked when the tunnel opens. So even with a single gateway address, every request gets a new exit IP and no switcher is needed.
If you really have different addresses, RoundRobinProxySwitcher from the proxy package does the job. It produces a colly.ProxyFunc that cycles through the addresses you give it in order. The counter increments atomically, so the order holds under concurrent requests too.
rp, err := proxy.RoundRobinProxySwitcher(
"http://user1:pass@pr.proxynet.io:8000",
"http://user2:pass@pr.proxynet.io:8000",
)
if err != nil {
log.Fatal(err)
}
c.SetProxyFunc(rp)user1 and user2 are placeholders. Each line in the list stands for a separate proxy identity. In our test, four requests were spread over two proxies in the order A, B, A, B.
In a logged-in session, a new IP per request works against you. If the server expects to see you from the same IP, you need a sticky session that pins the address. The session ID travels in the username and keeps the same exit for 1-60 minutes. The mechanism is in our post What Is a Sticky Session?.
Which request left through which proxy
The r.Request.ProxyURL field exists to answer this question, but with default settings it stays empty. In Colly's source, only the switcher fills it; SetProxy never writes it. In our test (Colly v2.3.0, Go 1.26), the field returned "" on every request inside OnResponse. The reason lies in Go's client.
Colly's default request timeout is 10 seconds. On a client with a timeout, Go hands a copy of the request to the Transport. The switcher writes the proxy address into that copy's context, while Colly looks at the original request. With SetRequestTimeout(0) the field got filled, but removing the timeout is not a good trade.
The second problem: when the field is filled, it carries the password in plain text (http://user:pass@…). The moment you write it to a log, the credentials leak.
The reliable way is to record the choice inside the proxy function:
c.SetProxyFunc(func(r *http.Request) (*url.URL, error) {
u, err := rp(r)
if err == nil {
log.Printf("%s -> %s", r.URL, u.Redacted())
}
return u, err
})Redacted() replaces the password with xxxxx. In our test, each request produced a line like /product/1 -> http://user:xxxxx@…, and it matched the proxy log one to one. To see the exit IP, visit an IP echo address with the same collector.
Async and LimitRule
colly.Async(true) starts requests concurrently. LimitRule caps concurrency and delay per target domain. Adding a proxy does not change the target site's capacity. Set the rule for the site.
c := colly.NewCollector(colly.Async(true))
c.IgnoreRobotsTxt = false
err := c.Limit(&colly.LimitRule{
DomainGlob: "*example.com*",
Parallelism: 2,
Delay: 1 * time.Second,
RandomDelay: 500 * time.Millisecond,
})
if err != nil {
log.Fatal(err)
}
if err := c.SetProxy("http://user:pass@pr.proxynet.io:8000"); err != nil {
log.Fatal(err)
}
for i := 1; i <= 6; i++ {
c.Visit(fmt.Sprintf("https://example.com/product/%d", i))
}
c.Wait()In our test, six requests with Parallelism: 2 and a 1-second Delay took about 3 seconds. The same six requests without a limit finished in 100 milliseconds, and all of them connected to the proxy at once. Round-robin over two proxies did not change the limit either. The rule is tied to the target domain, not to the proxy.
Three details break silently:
- Rule without a pattern. Without
DomainGloborDomainRegexp,LimitreturnsNo pattern defined in LimitRuleand the rule matches no request. - Missing
Wait. In async mode, ifc.Wait()is not called, the program exits before the requests finish. In our test, no request reached the proxy. - robots.txt is ignored. In Colly v2,
IgnoreRobotsTxtdefaults totrue. Once you set it tofalse,VisitreturnsURL blocked by robots.txtfor a disallowed path. The robots.txt request also goes through the proxy.
Follow the site's terms and robots.txt. If you see 429, raise the Delay value.
Troubleshooting
| Symptom | Likely cause |
|---|---|
| The page loads but the proxy log shows no request | WithTransport was called after SetProxy |
SetProxy returned an error | The address has no scheme, or the password has an unencoded character |
proxyconnect tcp: dial tcp :0 | An address starting with a name has no http:// scheme |
r.StatusCode 0, text says Proxy Authentication Required | 407 on an HTTPS target; wrong credentials |
r.Request.ProxyURL is empty | Default timeout; record the choice in the proxy function |
| Async program exits without fetching anything | No c.Wait() |
LimitRule has no effect | No pattern, or it does not match the target host |
How we tested
On December 2, 2026, we used Go 1.26.10 on Windows 11 and Colly v2.3.0 pulled in with go get. The whole test ran locally: an HTTP and HTTPS test page, two HTTP proxies requiring Basic credentials, and a SOCKS5 server requiring a username and password. The proxies logged each connection with its number, method and target. The SOCKS5 server recorded whether the target arrived as a domain name or an IP.
Target names used the .test suffix and were mapped to a local address only by our proxies. That way, any request that skipped the proxy failed at the DNS step. Each scenario ran in a separate process. The code snippets in this post passed go vet with the real endpoint address. In the tests, the same code ran against local addresses.
The bottom line
Use SetProxy for one address and RoundRobinProxySwitcher for several, and record the choice in the proxy function with the password hidden. Above all, check the order of WithTransport and SetProxy: the reverse order disables the proxy without an error. For a new exit on every request, a single Rotating Proxy address is enough, given how Colly handles connections.
Sources
- Colly v2 package docs, Collector and LimitRule: pkg.go.dev
- net/http, the Transport.Proxy field: pkg.go.dev
- Colly examples, Proxy switcher: go-colly.org
- Colly examples, Rate limit: go-colly.org
Frequently asked questions
How do I set a proxy in Colly?
With the line c.SetProxy("http://user:pass@pr.proxynet.io:8000"). The function parses the address, writes it as the proxy on the collector's Transport and turns keep-alive off. Check the returned error, because a scheme-less or broken address can silently turn into a request without a proxy. If you use a custom Transport, call WithTransport before this line.
Does Colly support SOCKS5 proxies?
Yes. Pass the address as socks5://user:pass@pr.proxynet.io:8000 to SetProxy or to the switcher list. Colly uses Go's built-in SOCKS5 client. Go treats the socks5 scheme like socks5h, so the target domain is resolved on the proxy side. In our test, the SOCKS5 server received a domain name instead of an IP with both schemes.
Does RoundRobinProxySwitcher change the proxy on every request?
Yes. On each call, the counter goes up by one and the next address in the list is picked. Because Colly turns keep-alive off once a proxy is set, every request goes out on a new connection and the choice really takes effect. In our test, four requests were spread across two proxies in the order A, B, A, B. The counter is atomic, so the order holds in async mode too.
Why is r.Request.ProxyURL empty?
Because of the default 10-second timeout. On a client with a timeout, Go hands a copy of the request to the Transport, and the switcher writes the address into that copy. Colly looks at the original request. Even when the field is filled, it carries the password in plain text. Recording the choice with u.Redacted() in a small function that wraps the proxy function is the more reliable way.
Is LimitRule applied per proxy?
No. LimitRule matches the host in the request URL and has nothing to do with the proxy. Even with ten different proxies, a rule with Parallelism: 2 sends at most two concurrent requests to that site. The rule needs DomainGlob or DomainRegexp to work. A rule without a pattern returns an error and applies to no request.
Does Colly read the HTTPS_PROXY environment variable?
Yes, as long as you set no proxy. When Colly's client is given no Transport of its own, Go's default Transport takes over and reads the HTTP_PROXY, HTTPS_PROXY and NO_PROXY variables. Once SetProxy or SetProxyFunc is called, a new Transport is set up and the variables are no longer considered. The default Transport does not use a proxy for localhost targets.




