Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access a Caddy server by IP

I have a website with docker and I use caddy for production. This is my configuration on my Caddyfile

mydomain.com {
    proxy / django:5000 {
        header_upstream Host {host}
        header_upstream X-Real-IP {remote}
        header_upstream X-Forwarded-Proto {scheme}
        header_upstream X-CSRFToken {~csrftoken}
    }
    log stdout
    errors stdout
    gzip
}

This configuration works well when I use mydomain.com, but when I try to access the server by IP it returns me the following error:

404 Site 156.130.11.8 is not served on this interface

I've tried using *, 156.130.11.8, and :80

* {
    proxy / django:5000...
    ...

156.130.11.8 {
    proxy / django:5000...
    ...

None of this is working either, does anyone of you know how can I solve this?

EDIT: The code is here: https://github.com/maguayo/django-starter-api Caddy configuration is under compose/production/caddy/ also the configuration I am running is on "production.yml"

like image 211
Marcos Aguayo Avatar asked May 15 '19 07:05

Marcos Aguayo


People also ask

What is Caddy reverse proxy?

Caddy is a powerful platform to serve your web applications and services. You can use Caddy as a reverse proxy to forward requests from the Internet to your Node. js application running on your server.

What are caddies in servers?

A Caddy web server works by proxying requests from clients to backend servers. Caddy can be configured to serve websites directly from its file system, or it can proxy requests to other web servers.

What is a Caddy file?

The Caddyfile is just a config adapter for Caddy. It is usually preferred when manually crafting configurations by hand, but is not as expressive, flexible, or programmable as Caddy's native JSON structure. If you are automating your Caddy configurations/deployments, you may wish to use JSON with Caddy's API.

Why use Caddy?

Caddy simplifies your infrastructure. It takes care of TLS certificate renewals, OCSP stapling, static file serving, reverse proxying, Kubernetes ingress, and more. Its modular architecture means you can do more with a single, static binary that compiles for any platform.


2 Answers

According to these tutorials:

https://www.digitalocean.com/community/tutorials/how-to-host-a-website-with-caddy-on-ubuntu-16-04

https://www.booleanworld.com/host-website-caddy-web-server-linux/

https://caddyserver.com/docs/caddyfile-tutorial

your Caddy server should serve the website on your ip using port 80 using one of the follwing configurations:

* {
    proxy / django:5000...
    ...

156.130.11.8 {
    proxy / django:5000...
    ...

:80 {
    proxy / django:5000...
    ...

http:// {
    proxy / django:5000...
    ...

Maybe http://156.130.11.8 { proxy / django:5000 ... } will do the job. Also try to use localhost instead of the ip address like this:

localhost {
    proxy / django:5000...
    ...
}

http://localhost {
    proxy / django:5000...
    ...
}

Good luck!

like image 173
Marvin Klar Avatar answered Sep 21 '22 23:09

Marvin Klar


Is it possible that your Django settings.py only has the mydomain.com as allowed host and not the IP? That way the server should return something like "is not served on this interface" and it shouldn't when the domain name is used.

I experienced a similar problem some time ago, not with Caddy but Apache also on Ubuntu. It would also explain why changing your Caddy config doesn't solve the problem, as it is correct.

settings.py should have a line like

ALLOWED_HOSTS = [IP, 'mydomain.com']

More on this parameter can be found in the documentation.

like image 30
creyD Avatar answered Sep 19 '22 23:09

creyD