How to convert "listen" and "server_name" in nginx to Caddy

1. The problem I’m having:

How could I convert the nginx config like that to caddy?

server {
    listen 127.0.0.1:30000 proxy_protocol;
    server_name example.com;

    set_real_ip_from 127.0.0.1;
    real_ip_header proxy_protocol;
    location / {

         root /usr/share/nginx/html/;
    }
} 

I have tried this, according to caddy document, but it didn’t work:

example.com:30000 {

    root * /path/to/resource

    file_server

}

I am confused of how to convert “listen” and “server_name” to caddy perfectly.

Any help is appreciated!

2. Error messages and/or full log output:

No error message, it just couldn’t work.

3. Caddy version:

v2.8.4 h1:q3pe0wpBj1OcHFZ3n/1nl4V4bxBrYoSoab7rL9BMYNk=

4. How I installed and ran Caddy:

I download the binary release on GitHub - caddy-dns/cloudflare: Caddy module: dns.providers.cloudflare

a. System environment:

Debian 4.19.316-1
systemd 241

b. Command:

caddy up -d

c. Service/unit/compose file:

d. My complete Caddy config:

example.com:30000 {

    root * /path/to/resource

    file_server
} 

5. Links to relevant resources:

Howdy @Fortiter,

That’s pretty much how I’d have done it.

What do you mean by “it just couldn’t work”?

Did you try to start the server? Did it start up OK? Did you try to browse to it? Did you get a response? Did it not serve the files you expected to see?

A good way to ensure you get good help is by asking yourself and answering the following questions:

  1. What did I do?
  2. What result did I expect?
  3. What did I get instead?

e.g. “I opened example.com:30000 in my browser. I expected to see the index.html I put in the web root. Instead, the browser showed a timeout error.”

1 Like

Sorry to express unclearly. Here’s a detailed description:
I use another proxy server to transfer data from 433 to 127.0.0.1:30000, and I make nginx listen to port 127.0.0.1:30000 as a normal server. It worked smoothly, but if I tried to convert it to caddy, the browser showed a timeout error.
Thanks for your help!

Does the example.com domain in your Caddyfile have a DNS entry pointing it to your Caddy server?

Does your Caddy host have port 30000 unblocked?

A timeout generally means that your browser couldn’t reach Caddy at all; usually because it doesn’t know what the correct IP address is to find it, or because something (e.g. a firewall) was in the way.

1 Like

Yes, I do transfer data from example.com:443 to 127.0.0.1:30000. With my nginx config, I could just visit example.com (use port 443), but don’t need to visit example.com:30000. I am just wondering if caddy could function as nginx’s “listen 127.0.0.1:30000”. Thanks!

Caddy can indeed do this, and the Caddyfile you already posted should be functional; it might need tweaking, but it should work.

The issue you’ve mentioned is a timeout in the browser. That heavily implies a networking issue - but it doesn’t make sense if you can access nginx normally on this port but not Caddy.

Is Caddy actually running? caddy up -d is not a valid Caddy CLI command: Command Line — Caddy Documentation

1 Like

Yes, caddy works. I made a mistake, while I actually use systemd to run caddy; and I have done some tests to ensure it.
Is it probably because caddy’s “example:30000” is different from “127.0.0.1:30000”? Moreover, the timeout error might be because my firewall is open and only port 80 and 443 are allowed.

What listen does is tell nginx which IP address to bind the listener to.

Caddy, by default, binds to all possible interfaces.

You can limit it to 127.0.0.1 if you need to, using: bind (Caddyfile directive) — Caddy Documentation

As for the firewall - if you’re connecting from the same host to itself, the firewall might not matter. If you’re proxying between two hosts, you might need to allow your reverse proxy to access Caddy’s port 30000 specifically.

1 Like

Thanks! It does work by using bind.

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