Caddy rewrite not working

1. Caddy version (caddy version):

caddy:2.3.0-alpine

2. How I run Caddy:

docker-compose -f docker-compose-caddy.yml up -d

a. System environment:

Docker

b. Command:

paste command here

c. Service/unit/compose file:

version: "3"

services:
  caddy:
    image: caddy:2.3.0-alpine
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
      - "2019:2019"
    networks:
      - app
    volumes:
      - $PWD/Caddyfile:/etc/caddy/Caddyfile
      - $PWD/caddy_configs:/etc/caddy/caddy_configs
      - $PWD/site:/srv
      - caddy_data:/data
      - caddy_config:/config

volumes:
  caddy_data:
  caddy_config:

networks:
  app:
    external:
      name: shopify-sync_app

d. My complete Caddyfile or JSON config:

import /etc/caddy/caddy_configs/*.caddy

test.site1.co {
    reverse_proxy webserver:7051
}

https:// {
	tls {
		on_demand
	}

	reverse_proxy webserver:7051
}

3. The problem I’m having:

I have two sub-domains:

I have created a CNAME entry for test.site2.com pointing to test.site1.co

Now, when I visit test.site2.com it opens test.site1.co (which is what should happen in an ideal case)
but what I want instead to happen is upon visiting test.site2.com it should redirect to test.site2.com/new_page but visiting test.site2.com/new_page should just open directly

4. Error messages and/or full log output:

caddy_1  | {"level":"info","ts":1613413312.354229,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
caddy_1  | run: adapting config using caddyfile: parsing caddyfile tokens for 'rewrite': /etc/caddy/caddy_configs/calenvie.caddy:2 - Error during parsing: Wrong argument count or unexpected line ending after 'rewrite'
caddy_1  | {"level":"info","ts":1613413313.733398,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
caddy_1  | run: adapting config using caddyfile: parsing caddyfile tokens for 'rewrite': /etc/caddy/caddy_configs/calenvie.caddy:2 - Error during parsing: Wrong argument count or unexpected line ending after 'rewrite'
caddy_1  | {"level":"info","ts":1613413315.0032415,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
caddy_1  | run: adapting config using caddyfile: parsing caddyfile tokens for 'rewrite': /etc/caddy/caddy_configs/calenvie.caddy:2 - Error during parsing: Wrong argument count or unexpected line ending after 'rewrite'
caddy_1  | {"level":"info","ts":1613413316.4365928,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
caddy_1  | run: adapting config using caddyfile: parsing caddyfile tokens for 'rewrite': /etc/caddy/caddy_configs/calenvie.caddy:2 - Error during parsing: Wrong argument count or unexpected line ending after 'rewrite'
caddy_1  | {"level":"info","ts":1613413318.2655513,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
caddy_1  | run: adapting config using caddyfile: parsing caddyfile tokens for 'rewrite': /etc/caddy/caddy_configs/calenvie.caddy:2 - Error during parsing: Wrong argument count or unexpected line ending after 'rewrite'
caddy_1  | {"level":"info","ts":1613413320.892288,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
caddy_1  | run: adapting config using caddyfile: parsing caddyfile tokens for 'rewrite': /etc/caddy/caddy_configs/calenvie.caddy:2 - Error during parsing: Wrong argument count or unexpected line ending after 'rewrite'
caddy_1  | {"level":"info","ts":1613413325.1765468,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
caddy_1  | run: adapting config using caddyfile: parsing caddyfile tokens for 'rewrite': /etc/caddy/caddy_configs/calenvie.caddy:2 - Error during parsing: Wrong argument count or unexpected line ending after 'rewrite'
caddy_1  | {"level":"info","ts":1613413332.6646836,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
caddy_1  | run: adapting config using caddyfile: parsing caddyfile tokens for 'rewrite': /etc/caddy/caddy_configs/calenvie.caddy:2 - Error during parsing: Wrong argument count or unexpected line ending after 'rewrite'
caddy_1  | {"level":"info","ts":1613413346.630897,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
caddy_1  | run: adapting config using caddyfile: parsing caddyfile tokens for 'rewrite': /etc/caddy/caddy_configs/calenvie.caddy:2 - Error during parsing: Wrong argument count or unexpected line ending after 'rewrite'
caddy_1  | {"level":"info","ts":1613413373.4453888,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
caddy_1  | run: adapting config using caddyfile: parsing caddyfile tokens for 'rewrite': /etc/caddy/caddy_configs/calenvie.caddy:2 - Error during parsing: Wrong argument count or unexpected line ending after 'rewrite'
caddy_1  | {"level":"info","ts":1613413425.7973611,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
caddy_1  | run: adapting config using caddyfile: parsing caddyfile tokens for 'rewrite': /etc/caddy/caddy_configs/calenvie.caddy:2 - Error during parsing: Wrong argument count or unexpected line ending after 'rewrite'
caddy_1  | {"level":"info","ts":1613413486.974338,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
caddy_1  | run: adapting config using caddyfile: parsing caddyfile tokens for 'rewrite': /etc/caddy/caddy_configs/calenvie.caddy:2 - Error during parsing: Wrong argument count or unexpected line ending after 'rewrite'

5. What I already tried:

test.site2.com {
    rewrite / {
		to /new_page
	}
}

6. Links to relevant resources:

You haven’t shared that config file. What are the contents of that file?

That’s not correct syntax. Please take a look at the docs:

I don’t see that you’ve configured the global options for on_demand_tls. Please do so, because otherwise you open yourself up for abuse. Anyone can make a request to your server with any hostname and Caddy will issue a certificate for it, without limit. This means that an attacker could have your server continually issue certificates, eating up storage space and forcing your server to hit rate limits. It’s very important that you configure limits, typically by configuring an ask endpoint so Caddy can ask your app “is this domain ok to get a cert for?”:

This is the complete Caddyfile

import /etc/caddy/caddy_configs/*.caddy

test.site1.co {
    reverse_proxy webserver:7051
}

https:// {
	tls {
		on_demand
	}

	reverse_proxy webserver:7051
}

This statement import /etc/caddy/caddy_configs/*.caddy is importing the other config file (calenvie.caddy)

Content in calenvie.caddy

test.site2.com {
    redir / https://test.site2.com/new_page{uri} permanent
}

This is still not working. Now, there’s no error but I get an empty page rather than the actual page.

I have also tried the following but with no luck:

test.site2.com {
    rewrite / /new_page
}

This is still on a test server and I am learning Caddy. I will definitely implement the ask endpoint before going to production.

Well you’re not actually doing anything in that site, so of course nothing will be served. You need to enable file_server or reverse_proxy or whatever for it to do something useful.

Caddy will respond to unhandled requests will an empty response with 200 status. This is because Caddy “did what you told it to do”, i.e. nothing; which isn’t an error on Caddy’s part, but a misconfiguration.

Hi

I have updated the Caddyfile to this:

test.site1.co {
    reverse_proxy webserver:7051
}

https:// {
	tls {
		on_demand
	}

	handle_path / {
		rewrite / /new_page{path}
		reverse_proxy webserver:7051
	}
	
}

Still no luck :frowning:

I want https://test.site2.com to go to https://test.site2.com/new_page and directly going to https://test.site2.com/new_page should also work.

handle_path / will only accept requests to exactly / and nothing else. Path matching in Caddy v2 is exact.

It also doesn’t make sense to use handle_path with no path matcher, because it exists as a shortcut to handle + uri strip_prefix. If you don’t need the strip_prefix logic, then use handle instead.

But in this case you probably don’t even need handle, it doesn’t do anything useful for you here.

I think the misunderstanding you’re having is that you think that site blocks will fall through to the https:// block if they don’t do anything with the request, but that’s not the case. If you make a test.site2.com site block, then you need to handle the request there. The https:// block is for requests to hostnames that you don’t have otherwise configured. So test.site3.com will be handled by https:// because you didn’t define that as a site you’re handling separately.

Thanks a lot (@francislavoie) for your patience and help. Finally, it is working now. Here’s my Caddyfile

test.site1.co {
    reverse_proxy webserver:7051
}

https:// {
	tls {
		on_demand
	}

	handle {
		redir / https://{host}/new_page{uri} permanent
		reverse_proxy webserver:7051
	}

	handle /new_page* {
		reverse_proxy webserver:7051
	}
	
}

You can just simplify this to:

redir / https://{host}/new_page{uri} permanent
reverse_proxy webserver:7051

You don’t need handle for your usecase, it’s redundant.

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