Reverse proxy to S3 static websites 502

1. Caddy version (caddy version):

2.4

2. How I run Caddy:

Caddy API as a systemd service

a. System environment:

Ubuntu 20.04

b. Command:

service caddy-api start

c. Service/unit/compose file:

N/A

d. My complete Caddyfile or JSON config:

{
   "admin": {
      "disabled": false,
      "listen": "localhost:2019"
   },
   "apps": {
      "http": {
         "servers": {
            "srv0": {
               "listen": [
                  ":80",
                  ":443"
               ],
               "routes": [
                  {
                     "handle": [
                        {
                           "encodings": {
                              "gzip": {}
                           },
                           "handler": "encode"
                        },
                        {
                           "handler": "reverse_proxy",
                           "headers": {
                              "request": {
                                 "set": {
                                    "Host": [
                                       "s3.test.approximated.app"
                                    ],
                                    "X-Forwarded-Proto": [
                                       "https"
                                    ],
                                    "apx-incoming-host": [
                                       "s3.test.approximated.app"
                                    ]
                                 }
                              },
                              "response": {
                                 "set": {
                                    "apx-hit": [
                                       "true"
                                    ]
                                 }
                              }
                           },
                           "transport": {
                              "protocol": "http",
                              "tls": {}
                           },
                           "upstreams": [
                              {
                                 "dial": "s3.test.approximated.app.s3-website-us-west-2.amazonaws.com:80"
                              }
                           ]
                        }
                     ],
                     "match": [
                        {
                           "host": [
                              "s3.test.approximated.app"
                           ]
                        }
                     ],
                     "terminal": true,
                     "@id": "vhost_1012"
                  }
               ]
            }
         }
      },
      "tls": {
         "automation": {
            "on_demand": {
               "ask": "https://mydomain.com/check-domain"
            }
         }
      }
   },
   "logging": {
      "logs": {
         "default": {
            "level": "DEBUG"
         }
      }
   }
}

3. The problem I’m having:

I’ve setup an S3 bucket static website at:
http://s3.test.approximated.app.s3-website-us-west-2.amazonaws.com/

It’s been setup as a static website, will public read permissions, and even CORS is set to allow any domain. I can load it up in my browser okay, but whenever I try to reverse proxy to it, it times out and caddy says that the context cancelled or EOF.

4. Error messages and/or full log output:

{
   "level":"error",
   "ts":1630361328.6027532,
   "logger":"http.log.error",
   "msg":"EOF",
   "request":{
      "remote_addr":"93.187.218.219:57618",
      "proto":"HTTP/2.0",
      "method":"GET",
      "host":"s3.test.approximated.app",
      "uri":"/",
      "headers":{
         "User-Agent":[
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36"
         ],
         "Sec-Fetch-Site":[
            "none"
         ],
         "Sec-Ch-Ua":[
            "\"Chromium\";v=\"92\", \" Not A;Brand\";v=\"99\", \"Google Chrome\";v=\"92\""
         ],
         "Cache-Control":[
            "max-age=0"
         ],
         "Dnt":[
            "1"
         ],
         "Upgrade-Insecure-Requests":[
            "1"
         ],
         "Sec-Fetch-Dest":[
            "document"
         ],
         "Sec-Ch-Ua-Mobile":[
            "?0"
         ],
         "Accept-Encoding":[
            "gzip, deflate, br"
         ],
         "Accept-Language":[
            "en-US,en;q=0.9"
         ],
         "Accept":[
            "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
         ],
         "Sec-Fetch-Mode":[
            "navigate"
         ],
         "Sec-Fetch-User":[
            "?1"
         ]
      },
      "tls":{
         "resumed":true,
         "version":772,
         "cipher_suite":4865,
         "proto":"h2",
         "proto_mutual":true,
         "server_name":"s3.test.approximated.app"
      }
   },
   "duration":23.122204434,
   "status":502,
   "err_id":"ywnvu6yyz",
   "err_trace":"reverseproxy.statusError (reverseproxy.go:852)"
}
{
   "level":"debug",
   "ts":1630361504.542603,
   "logger":"http.handlers.reverse_proxy",
   "msg":"upstream roundtrip",
   "upstream":"s3.test.approximated.app.s3-website-us-west-2.amazonaws.com:80",
   "request":{
      "remote_addr":"93.187.218.219:54548",
      "proto":"HTTP/2.0",
      "method":"GET",
      "host":"s3.test.approximated.app.s3-website-us-west-2.amazonaws.com",
      "uri":"/",
      "headers":{
         "Accept-Encoding":[
            "gzip, deflate, br"
         ],
         "Accept":[
            "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
         ],
         "Upgrade-Insecure-Requests":[
            "1"
         ],
         "X-Forwarded-For":[
            "93.187.218.219"
         ],
         "Cache-Control":[
            "max-age=0"
         ],
         "Sec-Fetch-User":[
            "?1"
         ],
         "Sec-Fetch-Mode":[
            "navigate"
         ],
         "X-Forwarded-Proto":[
            "http"
         ],
         "Apx-Incoming-Host":[
            "s3.test.approximated.app"
         ],
         "Sec-Fetch-Site":[
            "none"
         ],
         "Sec-Fetch-Dest":[
            "document"
         ],
         "Accept-Language":[
            "en-US,en;q=0.9"
         ],
         "User-Agent":[
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36"
         ],
         "Sec-Ch-Ua":[
            "\"Chromium\";v=\"92\", \" Not A;Brand\";v=\"99\", \"Google Chrome\";v=\"92\""
         ],
         "Dnt":[
            "1"
         ],
         "Sec-Ch-Ua-Mobile":[
            "?0"
         ]
      },
      "tls":{
         "resumed":true,
         "version":772,
         "cipher_suite":4865,
         "proto":"h2",
         "proto_mutual":true,
         "server_name":"s3.test.approximated.app"
      }
   },
   "duration":2.87797264,
   "error":"context canceled"
}

5. What I already tried:

I’ve tried searching for other Caddy users reverse proxying to S3 static websites but haven’t found much aside from the plugin for it. Ideally I’d like to avoid the plugin because it should be able to reverse proxy to an s3 static website, according to their docs. I’ve found a number of NGINX examples doing so, some of which explicitly set a dns resolver but I’m not sure how to do that on Caddy if it’s possible.

This might be an issue with S3 or AWS and not Caddy, but I thought I’d see if there are any obvious mistakes I’m making.

6. Links to relevant resources:

Thanks very much for your time!

You have tls: {} in there, but your upstream is on port 80. This is in conflict. Remove tls: {} if your upstream is not HTTPS.

2 Likes

Thank you, that was exactly the issue.

1 Like

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