V2: Rewrite for Onlyoffice Server

1. Caddy version (caddy version):

2.1.1

2. How I run Caddy:

a. System environment:

OS: Ubuntu Server 20.04
Caddy running in Docker (official image)

b. Command:

docker start caddy

c. Service/unit/compose file:

version: '2'

services:
  db:
    image: mariadb
    container_name: nextcloud_db
    command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
    restart: unless-stopped
    volumes:
      - /home/docker/config/nextcloud/db:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=xxx
      - MYSQL_PASSWORD=xxx
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud

  app:
    image: nextcloud:19-apache
    container_name: nextcloud_app
    restart: unless-stopped
    volumes:
      - /home/docker/data/nextcloud/data:/var/www/html/data
      - /home/docker/config/nextcloud/config:/var/www/html/config
      - /home/docker/config/nextcloud/apps:/var/www/html/apps

  onlyoffice-document-server:
    image: onlyoffice/documentserver:latest
    container_name: nextcloud_office
    restart: unless-stopped
    volumes:
      - /home/docker/config/nextcloud/doc/data:/var/www/onlyoffice/Data
      - /home/docker/config/nextcloud/doc/log:/var/log/onlyoffice
    environment:
      - JWT_ENABLED=true
      - JWT_SECRET=xxx

d. My complete Caddyfile or JSON config:

Caddyfile:

cloud.myserver.com {

	@office {
    	path_regexp test /ds-vpath/(.*)$
	}
	route /ds-vpath/* {
		rewrite @office /{http.regexp.test.1}
		reverse_proxy nextcloud_office:80
		import headerssnippet
	}

	reverse_proxy nextcloud_app:80
	import headerssnippet

}

nginx Config that works:

server {
    listen       443 ssl;
    server_name  cloud.myserver.com;

    client_max_body_size 1024M;

    location / {
        proxy_pass http://nextcloud_app;
    }

    location ~* ^/ds-vpath/ {
		rewrite /ds-vpath/(.*) /$1  break;
        proxy_pass http://nextcloud_office;
        proxy_redirect     off;
    }
    
}

3. The problem I’m having:

I want to make my Onlyoffice Server reachable under /ds-vpath/ just like in my nginx config, so I can use it in my Nextcloud. But when the way I do it in Caddy I can’t get a connection to the server. The Nextcloud is working fine under Caddy. How can I configure Caddy to do the same thing as nginx when I connect to /ds-vpath/ ?

4. Error messages and/or full log output:

There are no Error messages, it just redirects me to the Nextcloud page instead of the welcome screen of the Onlyoffice server when I open /ds-vpath/.

5. What I already tried:

I have already tried this:

uri strip_prefix /ds-vpath/

This is the standard approach for this kind of scenario:

example.com {
  route /ds-vpath/* {
    uri strip_prefix /ds-vpath
    reverse_proxy http://nextcloud_office
  }
  reverse_proxy http://nextcloud_app
}

You can test that Caddy is routing this logic correctly by running this exact Caddyfile:

http://:8080 {
  route /ds-vpath/* {
    uri strip_prefix /ds-vpath
    reverse_proxy localhost:8081
  }
  reverse_proxy localhost:8082
}

http://:8081 {
  respond "8081 {uri}" 200
}

http://:8082 {
  respond "8082 {uri}" 200
}

And I do some quick tests to confirm the expected behaviour:

~/Projects/test
➜ ./caddy version
v2.1.1 h1:X9k1+ehZPYYrSqBvf/ocUgdLSRIuiNiMo7CvyGUQKeA=

~/Projects/test
➜ curl -iL localhost:8080/stuff
HTTP/1.1 200 OK
Content-Length: 11
Date: Mon, 06 Jul 2020 23:29:10 GMT
Server: Caddy
Server: Caddy
Content-Type: text/plain; charset=utf-8

8082 /stuff⏎

~/Projects/test
➜ curl -iL localhost:8080/ds-vpath
HTTP/1.1 200 OK
Content-Length: 14
Date: Mon, 06 Jul 2020 23:29:16 GMT
Server: Caddy
Server: Caddy
Content-Type: text/plain; charset=utf-8

8082 /ds-vpath⏎

~/Projects/test
➜ curl -iL localhost:8080/ds-vpath/
HTTP/1.1 200 OK
Content-Length: 6
Date: Mon, 06 Jul 2020 23:29:18 GMT
Server: Caddy
Server: Caddy
Content-Type: text/plain; charset=utf-8

8081 /⏎

In this manner I have demonstrated that requests to /ds-vpath/ have the prefix stripped before being proxied to a specific upstream, whereas other requests (including /ds-vpath, no trailing slash) are sent to the catch-all upstream.

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