Reverse Proxy doesn't works

1. The problem I’m having:

I have a python app running on port 5000, and Caddy running on port 80. I’m trying to make a reverse proxy, but when I call http://127.0.0.1/soc it shows me the index.html page from the projeto_html folder, and not the page from the python app running on port 5000.

2. Error messages and/or full log output:

No error message. Only, the app running on port 5000 doesn’t shows.

3. Caddy version:

v2.7.6 h1:w0NymbG2m9PcvKWsrXO6EEkY9Ru4FJK8uQbYcev1p3A=

4. How I installed and ran Caddy:

Binary file

a. System environment:

Debian Linux 11

b. Command:

sudo caddy run --config Caddyfile

d. My complete Caddy config:

:80 {

	root * /home/paulo/projeto_html
	file_server {
		browse
	}

     reverse_proxy /soc/* http://127.0.0.1:5000	

	try_files {path} index.html

}

There’s two problems here.

First, try_files has a higher directive order than reverse_proxy, so it always runs first and rewrites the request because the file doesn’t exist on disk.

Second, your matcher is /soc/*, which doesn’t match /soc.

You should use handle to make it mutually exclusive:

:80 {
	handle /soc* {
		reverse_proxy 127.0.0.1:5000
	}

	handle {
		root * /home/paulo/projecto_html
		try_files {path} /index.html
		file_server browse
	}
}

If you plan on running your server long-term, don’t run caddy run directly, run Caddy as a service (and move your files to /srv since the service won’t have access to /home):

1 Like

Hi, really works now!!!

But I needed to make a replace inside, because the “/soc” was being passed to python app, calling 127.0.0.1:5000/soc. But with replace, it worked.

handle /soc* {
    uri replace /soc /
    reverse_proxy 127.0.0.1:5000	
}

thank you very much!!!

Use handle_path instead, and it does the stripping for you. You can remove uri replace.

2 Likes
Use `handle_path` instead, and it does the stripping for you. You can remove `uri replace` 

Really!! Thanks again!!!

1 Like