Serving a Next.js app returns 521 for any routes except /

1. Caddy version (caddy version): v2.0.0

2. How I run Caddy:

a. System environment:

b. Command:

sudo caddy reload

c. Service/unit/compose file:

I use pm2 for my next.js app, started with this command:

pm2 start npm --name "next" -- start

d. My complete Caddyfile or JSON config:

Caddyfile:

:80

reverse_proxy /* localhost:3000

JSON config (from caddy adapt):

{"apps":{"http":{"servers":{"srv0":{"listen":[":80"],"routes":[{"handle":[{"handler":"reverse_proxy","upstreams":[{"dial":"localhost:3000"}]}]}]}}}}}

3. The problem I’m having:

I am trying to setup a Next.js app on my VPS but for some reason all routes except / return 521 error. I want the caddy server to serve all pages under :3000. I use Cloudflare with A record pointed to my server IP.

4. Error messages and/or full log output:

020/06/06 13:10:42.677 INFO    using adjacent Caddyfile
2020/06/06 13:10:42.680 INFO    admin   admin endpoint started  {"address": "tcp/localhost:2019", "enforce_origin": false, "origins": ["localhost:2019", "[::1]:2019", "127.0.0.1:2019"]}
2020/06/06 13:10:42.680 INFO    http    server is listening only on the HTTP port, so no automatic HTTPS will be applied to this server      {"server_name": "srv0", "http_port": 80}
2020/06/06 13:10:42.682 INFO    tls     cleaned up storage units
2020/06/06 13:10:42.683 INFO    autosaved config        {"file": "/root/.config/caddy/autosave.json"}
2020/06/06 13:10:42.683 INFO    serving initial configuration
2020/06/06 13:10:42 [INFO] Started certificate maintenance routine
Successfully started Caddy - Caddy is running in the background

5. What I already tried:

I tried different configs.

This:

:80

reverse_proxy :3000

this:

:80

route /* {
  reverse_proxy localhost:3000
}

and this:

:80 {
  reverse_proxy localhost:3000
}

they all didn’t work :slightly_frowning_face:

6. Links to relevant resources:

HTTP status 521 is a non-standard status code returned by Cloudflare:

https://support.cloudflare.com/hc/en-us/articles/115003011431#521error

I think something is messed up on your Cloudflare config. I’d recommend turning off Cloudflare for the time being so you can properly investigate whether your Caddy config is doing what you expect.

I don’t know enough about Cloudflare to be able to help otherwise as I don’t use them.

1 Like

I figured it out. Yes, my CF was messed up. I should have set SSL encryption mode on flexible and page rules on flexible as well.

Thanks for showing me the right direction :smiley:

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