Unable to configure proxy with puma socket

1. Caddy version (v2.0.0 h1:pQSaIJGFluFvu8KDGDODV8u4/QRED/OPyIR+MWYYse8=):

2. How I run Caddy:

As ubuntu service

a. System environment:

Ubuntu 18.04

b. Command:

sudo systemctl restart caddy
sudo systemctl status caddy

c. Service/unit/compose file:

[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target

[Service]
User=caddy
Group=caddy
ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target

d. My complete Caddyfile or JSON config:

mysize.xyz {
  encode gzip
  log stdout
  root * /home/deploy/apps/rails-app/current/public
  reverse_proxy {
    to unix:///home/deploy/apps/rails-app/shared/tmp/sockets/rails-app-puma.sock
 #   except /assets # this is /public/assets directory
 #   except /solr
 #   transparent
 #   websocket
 #   policy round_robin
  }
  errors stdout
  header / {
        Strict-Transport-Security "max-age=31536000"
  }
}

e. Caddyfile from V1, worked

mydomain.com {
  gzip
  log stdout
  root /home/deploy/apps/rails-app/current/public
  proxy / unix:///home/deploy/apps/rails-app/shared/tmp/sockets/rails-app-puma.sock {
    except /assets # this is /public/assets directory
    except /solr
    transparent
    websocket
    policy round_robin
  }
  errors stdout
  header / {
        Strict-Transport-Security "max-age=31536000"
  }

  proxy /solr localhost:8983 {
    transparent
  }
}

3. The problem I’m having:

I’m trying to configure my Rails application to work with Caddy V2 but I’m having problems with the proxy

4. Error messages and/or full log output:

● caddy.service - Caddy
   Loaded: loaded (/etc/systemd/system/caddy.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Fri 2020-05-08 15:40:50 UTC; 4s ago
     Docs: https://caddyserver.com/docs/
  Process: 81425 ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile (code=exited, status=1/FAILURE)
 Main PID: 81425 (code=exited, status=1/FAILURE)

May 08 15:40:50 ubuntu-staging caddy[81425]: PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
May 08 15:40:50 ubuntu-staging caddy[81425]: HOME=/var/lib/caddy
May 08 15:40:50 ubuntu-staging caddy[81425]: LOGNAME=caddy
May 08 15:40:50 ubuntu-staging caddy[81425]: USER=caddy
May 08 15:40:50 ubuntu-staging caddy[81425]: INVOCATION_ID=870e93a87191407db37d6cd753181a0c
May 08 15:40:50 ubuntu-staging caddy[81425]: JOURNAL_STREAM=9:223042
May 08 15:40:50 ubuntu-staging caddy[81425]: {"level":"info","ts":1588952450.4012098,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":""}
May 08 15:40:50 ubuntu-staging caddy[81425]: run: adapting config using caddyfile: parsing caddyfile tokens for 'reverse_proxy': /etc/caddy/Caddyfile:6 - Error during parsing: for now, URLs
May 08 15:40:50 ubuntu-staging systemd[1]: caddy.service: Main process exited, code=exited, status=1/FAILURE
May 08 15:40:50 ubuntu-staging systemd[1]: caddy.service: Failed with result 'exit-code'.

Install Caddy with Ubuntu packages.

echo "deb [trusted=yes] https://apt.fury.io/caddy/ /" \
    | sudo tee -a /etc/apt/sources.list.d/caddy-fury.list
sudo apt update
sudo apt install caddy

unix:///home/deploy/apps/rails-app/shared/tmp/sockets/rails-app-puma.sock

change with

unix//home/deploy/apps/rails-app/shared/tmp/sockets/rails-app-puma.sock

1 Like

Caddy is already installed. Just installed a couple hours ago

your caddy config is v1. v2 config is much different.

That is my previous version. Above that is the new version. You probably have problem reading. I can’t get the formatting correct on this site

The socket part fixed that issue. Thanks

so is it completely ok now?

I think so. I’m getting the rails error page. So probably now related to Caddy

But I think I still need to update the file to handle assets. They should not be in the proxy. In V1 I used except /assets

there is no policy round_robin directive in v2.

its lb_policy

I think they should add Rails config in Caddy wiki.

1 Like

This is my current Caddyfile

mysite.com {
  encode gzip
  log stdout
  root * /home/deploy/apps/we-vote/current/public
  reverse_proxy {
    to unix//home/deploy/apps/we-vote/shared/tmp/sockets/we-vote-puma.sock
 #   except /assets # this is /public/assets directory
 #   except /solr
 #   transparent
 #   websocket
 #   policy round_robin
  }
  # errors stdout
  header / {
        Strict-Transport-Security "max-age=31536000"
  }

  #proxy /solr localhost:8983 {
  #  transparent
  #}
}

@tweeniev2 Are you able to help with serving /assets or should I create a new issue?

just remove except line.

But how do I handle that in V2. The socket from puma does not serve assets. They should come from public/assets. @tweeniev2

add
file_server

in your caddyfile.

@tweeniev2 It’s still not rendering assets

  file_server /assets/*

just file_server ?

@tweeniev2 Nope tried that first

Also tried

  file_server /assets/* {
    root /home/deploy/apps/rails-app/current/public/assets
  }

Just a hunch, but notice the order that directives are evaluated for requests: Caddyfile Directives — Caddy Documentation

reverse_proxy is before file_server, and you don’t have a matcher on reverse_proxy, so it’s proxying all requests before it gets to file_server; in fact it never gets to file_server.

Either use a matcher to select specifically which requests you want to be proxied, or you have to use the route or handle directives. Similar to the explanation for the utility of the route directive here: route (Caddyfile directive) — Caddy Documentation

route {
    file_server /assets*
    reverse_proxy ...
}