Ngnix setup to Caddy 2 Caddyfile

1. Caddy version (caddy version):

v2.2.1

2. How I run Caddy:

Docker on a Raspberry pi
My Caddyfile:

viktorli.hopto.org:80 {
        reverse_proxy http://192.168.200.184:6081
}

viktorli.hopto.org {
        reverse_proxy http://192.168.200.184:6081
}

192.168.200.184:80 {
        root * /var/www/html
        php_fastcgi wordpress:9000
        file_server

Then I installed varnish and configured it to look at my site using 192.168.200.184:80 in /etc/varnish/default.vcl:

backend default {
    .host = "192.168.200.184";
    .port = "80";
}

The default port varnish listening to is 6081, so when I visit the site 192.168.200.184:6081 I see the content served with varnish headers and everything works as expected using HTTP.

3. The problem I’m having:

I can not connect using Caddy proxy.

When trying to connect to https://viktorli.hopto.org I get:

The page isn’t redirecting properly

When trying to connect to http://viktorli.hopto.org I get:

Untitled3

I think the problem is with the reverse_proxy directive. I thought that the code from NGNIX could help.So I need to translate NGNIX configuration to Caddyfile and I do not really understand how to do it correctly.
The NGNIX config:

server {
        listen 443 ssl;

        server_name example.com;
        ssl_certificate /etc/nginx/ssl/nginx.crt;
        ssl_certificate_key /etc/nginx/ssl/nginx.key;

        location / {
            proxy_pass http://127.0.0.1:80;
            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto https;
            proxy_set_header X-Forwarded-Port 443;
            proxy_set_header Host $host;
        }
}

I understand that we can ommit the SSL part, because Caddy will solve it for us, but what about the proxy part?

4. Links to relevant resources:

Trying to implement Varnish behind Caddy:

Thank you in advance!

You don’t need to do anything special for the proxy in Caddy, because Caddy’s header handling will do what you need automatically:

reverse_proxy 127.0.0.1:8080

Please note that you should let Caddy bind on port 80 so that it can do ACME HTTP challenges, so you should run your app on a different port than what’s in your nginx config. Port 8080 is probably fine.

1 Like

I would give little input on my config.
I installed wordpress site which I serve using php_fastcgi in my local netowrk. This is the Caddy code:

192.168.200.184:80 {
        root * /var/www/html
        php_fastcgi wordpress:9000
        file_server
}

Code above works fine.

Then I installed varnish and configured it to look at my site using 192.168.200.184:80 in /etc/varnish/default.vcl:

backend default {
    .host = "192.168.200.184";
    .port = "80";
}

The default port for varnish is 6081, so when I visit the site 192.168.200.184:6081 I see the content served with varnish headers and everything works as expected using http.

So now my goal is to use Caddy to serve it from my domain name with SSL.

I tried what you suggested in two variations. When configuring proxy using https in Caddyfile:

viktorli.hopto.org {
        reverse_proxy http://192.168.200.184:6081
}

I get:

The page isn’t redirecting properly

in the browser.

If I configure Caddyfile using http:

viktorli.hopto.org:80 {
        reverse_proxy http://192.168.200.184:6081
}

I get error from varnish:

I think that it could be because of the Caddy’s default reverse_proxy config and maybe I should add some settings to make it work?

Basically what I want is to configure system as in the scheme below without the HTTP part. Only with HTTPS part and change ngnix to Caddy:
goal

You have a colon at the end of your site address there. That seems problematic.

That said, I know nothing about varnish so I’m afraid I won’t be much help there.

1 Like

Thank you. Sorry, it was a typo while copying. Edited the previous comment. Could it be that I should pass and recieve some headers back? Especially the:

        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for

We also have an adapter that converts your nginx to Caddy config for you: GitHub - caddyserver/nginx-adapter: Run Caddy with your NGINX config

Thank you.
I did translate the NGNIX to Caddy:

viktorli.hopto.org {
        reverse_proxy http://192.168.200.184:6081 {
                header_up Host {host}
                header_up X-Real-IP {remote_host}
                header_up X-Forwarded-For {remote_host}
                header_up X-Forwarded-Proto https
                header_up X-Forwarded-Port 443
        }
}

But it does not work.

Solved by standard reverse_proxy directive:

viktorli.hopto.org {
        reverse_proxy http://192.168.200.184:6081
}

Turned out my configuration was not correct.

1 Like

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