Too many redirects

Caddy newbie.

Caddy version v2.4.6. Digital Ocean Ubuntu 20.4 server
Problem: Simple webpage fetch causes too many redirects. Caddyfile problem?

In Safari, request: https://theviewfromafar.com/index.html
Returns: Too many redirects occurred trying to open "https://theviewfromafar.com/index.html

Caddyfile:

```@shouldRedir {
```        path /sbwTweet/
```        expression {query} == '"" || {query} == "tab=links" || {query} == "tab=about"'
```}
```reverse_proxy @shouldRedir http://oldschool.scripting.com {
```        header_up Host {upstream_hostport}
```}
```redir http://theviewfromafar.com{uri}
```log

**Console recorded**:

2022/04/13 02:22:05.583   INFO    http.log.access handled request {"request": {"remote_addr": "67.241.21.212:53721", "proto": "HTTP/2.0", "method": "GET", "host": "theviewfromafar.com", "uri": "/index.html", "headers": {"Te": ["trailers"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:97.0) Gecko/20100101 Firefox/97.0"], "Accept-Language": ["en-US,en;q=0.5"], "Upgrade-Insecure-Requests": ["1"], "Sec-Fetch-Dest": ["document"], "Sec-Fetch-Mode": ["navigate"], "Sec-Fetch-Site": ["none"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8"], "Accept-Encoding": ["gzip, deflate, br"], "Sec-Fetch-User": ["?1"]}, "tls": {"resumed": true, "version": 772, "cipher_suite": 4865, "proto": "h2", "proto_mutual": true, "server_name": "theviewfromafar.com"}}, "common_log": "67.241.21.212 - - [13/Apr/2022:02:22:05 +0000] \"GET /index.html HTTP/2.0\" 302 0", "user_id": "", "duration": 0.000058352, "size": 0, "status": 302, "resp_headers": {"Server": ["Caddy"], "Location": ["http://theviewfromafar.com/index.html"], "Content-Type": []}}
2022/04/13 02:22:05.722 INFO    http.log.access handled request {"request": {"remote_addr": "67.241.21.212:53721", "proto": "HTTP/2.0", "method": "GET", "host": "theviewfromafar.com", "uri": "/index.html", "headers": {"Sec-Fetch-Dest": ["document"], "Sec-Fetch-Mode": ["navigate"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8"], "Accept-Language": ["en-US,en;q=0.5"], "Upgrade-Insecure-Requests": ["1"], "Sec-Fetch-User": ["?1"], "Te": ["trailers"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:97.0) Gecko/20100101 Firefox/97.0"], "Accept-Encoding": ["gzip, deflate, br"], "Sec-Fetch-Site": ["none"]}, "tls": {"resumed": true, "version": 772, "cipher_suite": 4865, "proto": "h2", "proto_mutual": true, "server_name": "theviewfromafar.com"}}, "common_log": "67.241.21.212 - - [13/Apr/2022:02:22:05 +0000] \"GET /index.html HTTP/2.0\" 302 0", "user_id": "", "duration": 0.000103587, "size": 0, "status": 302, "resp_headers": {"Server": ["Caddy"], "Location": ["http://theviewfromafar.com/index.html"], "Content-Type": []}}
2022/04/13 02:22:05.834 INFO    http.log.access handled request {"request": {"remote_addr": "67.241.21.212:53721", "proto": "HTTP/2.0", "method": "GET", "host": "theviewfromafar.com", "uri": "/index.html", "headers": {"Accept-Encoding": ["gzip, deflate, br"], "Upgrade-Insecure-Requests": ["1"], "Sec-Fetch-Dest": ["document"], "Sec-Fetch-Mode": ["navigate"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:97.0) Gecko/20100101 Firefox/97.0"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8"], "Accept-Language": ["en-US,en;q=0.5"], "Sec-Fetch-Site": ["none"], "Sec-Fetch-User": ["?1"], "Te": ["trailers"]}, "tls": {"resumed": true, "version": 772, "cipher_suite": 4865, "proto": "h2", "proto_mutual": true, "server_name": "theviewfromafar.com"}}, "common_log": "67.241.21.212 - - [13/Apr/2022:02:22:05 +0000] \"GET /index.html HTTP/2.0\" 302 0", "user_id": "", "duration": 0.00006023, "size": 0, "status": 302, "resp_headers": {"Server": ["Caddy"], "Location": ["http://theviewfromafar.com/index.html"], "Content-Type": []}}
2022/04/13 02:22:05.991 INFO    http.log.access handled request {"request": {"remote_addr": "67.241.21.212:53721", "proto": "HTTP/2.0", "method": "GET", "host": "theviewfromafar.com", "uri": "/index.html", "headers": {"User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:97.0) Gecko/20100101 Firefox/97.0"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8"], "Accept-Language": ["en-US,en;q=0.5"], "Sec-Fetch-User": ["?1"], "Accept-Encoding": ["gzip, deflate, br"], "Upgrade-Insecure-Requests": ["1"], "Sec-Fetch-Dest": ["document"], "Sec-Fetch-Mode": ["navigate"], "Sec-Fetch-Site": ["none"], "Te": ["trailers"]}, "tls": {"resumed": true, "version": 772, "cipher_suite": 4865, "proto": "h2", "proto_mutual": true, "server_name": "theviewfromafar.com"}}, "common_log": "67.241.21.212 - - [13/Apr/2022:02:22:05 +0000] \"GET /index.html HTTP/2.0\" 302 0", "user_id": "", "duration": 0.000101458, "size": 0, "status": 302, "resp_headers": {"Server": ["Caddy"], "Location": ["http://theviewfromafar.com/index.html"], "Content-Type": []}}
2022/04/13 02:22:06.153 INFO    http.log.access handled request {"request": {"remote_addr": "67.241.21.212:53721", "proto": "HTTP/2.0", "method": "GET", "host": "theviewfromafar.com", "uri": "/index.html", "headers": {"Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8"], "Accept-Language": ["en-US,en;q=0.5"], "Upgrade-Insecure-Requests": ["1"], "Sec-Fetch-Mode": ["navigate"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:97.0) Gecko/20100101 Firefox/97.0"], "Accept-Encoding": ["gzip, deflate, br"], "Sec-Fetch-Dest": ["document"], "Sec-Fetch-Site": ["none"], "Sec-Fetch-User": ["?1"], "Te": ["trailers"]}, "tls": {"resumed": true, "version": 772, "cipher_suite": 4865, "proto": "h2", "proto_mutual": true, "server_name": "theviewfromafar.com"}}, "common_log": "67.241.21.212 - - [13/Apr/2022:02:22:06 +0000] \"GET /index.html HTTP/2.0\" 302 0", "user_id": "", "duration": 0.000063913, "size": 0, "status": 302, "resp_headers": {"Location": ["http://theviewfromafar.com/index.html"], "Content-Type": [], "Server": ["Caddy"]}}
2022/04/13 02:22:06.312 INFO    http.log.access handled request {"request": {"remote_addr": "67.241.21.212:53721", "proto": "HTTP/2.0", "method": "GET", "host": "theviewfromafar.com", "uri": "/index.html", "headers": {"Upgrade-Insecure-Requests": ["1"], "Sec-Fetch-Site": ["none"], "Te": ["trailers"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:97.0) Gecko/20100101 Firefox/97.0"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8"], "Accept-Language": ["en-US,en;q=0.5"], "Sec-Fetch-User": ["?1"], "Accept-Encoding": ["gzip, deflate, br"], "Sec-Fetch-Dest": ["document"], "Sec-Fetch-Mode": ["navigate"]}, "tls": {"resumed": true, "version": 772, "cipher_suite": 4865, "proto": "h2", "proto_mutual": true, "server_name": "theviewfromafar.com"}}, "common_log": "67.241.21.212 - - [13/Apr/2022:02:22:06 +0000] \"GET /index.html HTTP/2.0\" 302 0", "user_id": "", "duration": 0.000066878, "size": 0, "status": 302, "resp_headers": {"Server": ["Caddy"], "Location": ["http://theviewfromafar.com/index.html"], "Content-Type": []}}
2022/04/13 02:22:06.500 INFO    http.log.access handled request {"request": {"remote_addr": "67.241.21.212:53721", "proto": "HTTP/2.0", "method": "GET", "host": "theviewfromafar.com", "uri": "/index.html", "headers": {"Accept-Encoding": ["gzip, deflate, br"], "Upgrade-Insecure-Requests": ["1"], "Sec-Fetch-Mode": ["navigate"], "Sec-Fetch-Site": ["none"], "Sec-Fetch-User": ["?1"], "Te": ["trailers"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:97.0) Gecko/20100101 Firefox/97.0"], "Accept-Language": ["en-US,en;q=0.5"], "Sec-Fetch-Dest": ["document"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8"]}, "tls": {"resumed": true, "version": 772, "cipher_suite": 4865, "proto": "h2", "proto_mutual": true, "server_name": "theviewfromafar.com"}}, "common_log": "67.241.21.212 - - [13/Apr/2022:02:22:06 +0000] \"GET /index.html HTTP/2.0\" 302 0", "user_id": "", "duration": 0.00006132, "size": 0, "status": 302, "resp_headers": {"Server": ["Caddy"], "Location": ["http://theviewfromafar.com/index.html"], "Content-Type": []}}
2022/04/13 02:22:06.662 INFO    http.log.access handled request {"request": {"remote_addr": "67.241.21.212:53721", "proto": "HTTP/2.0", "method": "GET", "host": "theviewfromafar.com", "uri": "/index.html", "headers": {"Sec-Fetch-Site": ["none"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:97.0) Gecko/20100101 Firefox/97.0"], "Accept-Language": ["en-US,en;q=0.5"], "Sec-Fetch-Mode": ["navigate"], "Sec-Fetch-Dest": ["document"], "Sec-Fetch-User": ["?1"], "Te": ["trailers"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8"], "Accept-Encoding": ["gzip, deflate, br"], "Upgrade-Insecure-Requests": ["1"]}, "tls": {"resumed": true, "version": 772, "cipher_suite": 4865, "proto": "h2", "proto_mutual": true, "server_name": "theviewfromafar.com"}}, "common_log": "67.241.21.212 - - [13/Apr/2022:02:22:06 +0000] \"GET /index.html HTTP/2.0\" 302 0", "user_id": "", "duration": 0.000084506, "size": 0, "status": 302, "resp_headers": {"Content-Type": [], "Server": ["Caddy"], "Location": ["http://theviewfromafar.com/index.html"]}}
2022/04/13 02:22:06.812 INFO    http.log.access handled request {"request": {"remote_addr": "67.241.21.212:53721", "proto": "HTTP/2.0", "method": "GET", "host": "theviewfromafar.com", "uri": "/index.html", "headers": {"Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8"], "Accept-Language": ["en-US,en;q=0.5"], "Accept-Encoding": ["gzip, deflate, br"], "Upgrade-Insecure-Requests": ["1"], "Sec-Fetch-Dest": ["document"], "Sec-Fetch-Site": ["none"], "Sec-Fetch-User": ["?1"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:97.0) Gecko/20100101 Firefox/97.0"], "Te": ["trailers"], "Sec-Fetch-Mode": ["navigate"]}, "tls": {"resumed": true, "version": 772, "cipher_suite": 4865, "proto": "h2", "proto_mutual": true, "server_name": "theviewfromafar.com"}}, "common_log": "67.241.21.212 - - [13/Apr/2022:02:22:06 +0000] \"GET /index.html HTTP/2.0\" 302 0", "user_id": "", "duration": 0.000070636, "size": 0, "status": 302, "resp_headers": {"Server": ["Caddy"], "Location": ["http://theviewfromafar.com/index.html"], "Content-Type": []}}
2022/04/13 02:22:06.979 INFO    http.log.access handled request {"request": {"remote_addr": "67.241.21.212:53721", "proto": "HTTP/2.0", "method": "GET", "host": "theviewfromafar.com", "uri": "/index.html", "headers": {"Sec-Fetch-Dest": ["document"], "Sec-Fetch-User": ["?1"], "Te": ["trailers"], "Accept-Language": ["en-US,en;q=0.5"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8"], "Accept-Encoding": ["gzip, deflate, br"], "Upgrade-Insecure-Requests": ["1"], "Sec-Fetch-Mode": ["navigate"], "Sec-Fetch-Site": ["none"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:97.0) Gecko/20100101 Firefox/97.0"]}, "tls": {"resumed": true, "version": 772, "cipher_suite": 4865, "proto": "h2", "proto_mutual": true, "server_name": "theviewfromafar.com"}}, "common_log": "67.241.21.212 - - [13/Apr/2022:02:22:06 +0000] \"GET /index.html HTTP/2.0\" 302 0", "user_id": "", "duration": 0.00006757, "size": 0, "status": 302, "resp_headers": {"Server": ["Caddy"], "Location": ["http://theviewfromafar.com/index.html"], "Content-Type": []}}
2022/04/13 02:22:07.134 INFO    http.log.access handled request {"request": {"remote_addr": "67.241.21.212:53721", "proto": "HTTP/2.0", "method": "GET", "host": "theviewfromafar.com", "uri": "/index.html", "headers": {"Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8"], "Accept-Encoding": ["gzip, deflate, br"], "Upgrade-Insecure-Requests": ["1"], "Sec-Fetch-Mode": ["navigate"], "Te": ["trailers"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:97.0) Gecko/20100101 Firefox/97.0"], "Accept-Language": ["en-US,en;q=0.5"], "Sec-Fetch-Dest": ["document"], "Sec-Fetch-Site": ["none"], "Sec-Fetch-User": ["?1"]}, "tls": {"resumed": true, "version": 772, "cipher_suite": 4865, "proto": "h2", "proto_mutual": true, "server_name": "theviewfromafar.com"}}, "common_log": "67.241.21.212 - - [13/Apr/2022:02:22:07 +0000] \"GET /index.html HTTP/2.0\" 302 0", "user_id": "", "duration": 0.000056616, "size": 0, "status": 302, "resp_headers": {"Server": ["Caddy"], "Location": ["http://theviewfromafar.com/index.html"], "Content-Type": []}}

**In terminal tried**: curl -v https://theviewfromafar.com/index.html
**It returned**:
sbw@sbwMacbookPro ~ % curl -v https://theviewfromafar.com/index.html
*   Trying 143.244.145.110...
* TCP_NODELAY set
* Connected to theviewfromafar.com (143.244.145.110) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/cert.pem
  CApath: none
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-ECDSA-AES128-GCM-SHA256
* ALPN, server accepted to use h2
* Server certificate:
*  subject: CN=theviewfromafar.com
*  start date: Apr 12 17:12:08 2022 GMT
*  expire date: Jul 11 17:12:07 2022 GMT
*  subjectAltName: host "theviewfromafar.com" matched cert's "theviewfromafar.com"
*  issuer: C=US; O=Let's Encrypt; CN=R3
*  SSL certificate verify ok.
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle 0x7fa52e80d600)
> GET /index.html HTTP/2
> Host: theviewfromafar.com
> User-Agent: curl/7.64.1
> Accept: */*
> 
* Connection state changed (MAX_CONCURRENT_STREAMS == 250)!
< HTTP/2 302 
< location: http://theviewfromafar.com/index.html
< server: Caddy
< content-length: 0
< date: Wed, 13 Apr 2022 02:17:22 GMT
< 
* Connection #0 to host theviewfromafar.com left intact
* Closing connection 0

Thank you.

Hey @sbwaters , welcome to the forums :slight_smile:

Your config line starting with redir doesn’t have a matcher on it, and it’s ordered before reverse_proxy, so it will be applied to all requests.

Caddy is automatically redirecting HTTP to HTTPS, but then your config is redirecting HTTPS to HTTP for some reason. :thinking:

1 Like

[quote=“matt, post:2, topic:15630, full:true”]

Your config line starting with redir doesn’t have a matcher on it, and it’s ordered before reverse_proxy, so it will be applied to all requests.[/quote]

Can one construct a “matcher” that says:

“NOT /sbwTweet/*”

That would allow every thing but the redirected through.

Yes, there’s a not matcher that can take any other matcher as input. So literally that, not path /foo/*

1 Like

You could alternatively use route to order the directives in the order they are in your route block. (Or use the order global option to sort redir after reverse_proxy.)

See Composing in the Caddyfile.

This topic was automatically closed after 29 days. New replies are no longer allowed.