Access a Caddy server by IP
Asked Answered
I

2

16

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"

Incompetence answered 15/5, 2019 at 7:24 Comment(8)
Can you provide sample git repo to see the problem in action?Zellers
@TarunLalwani yes. Here you have it: github.com/maguayo/django-starter-api Caddy is under compose/production/caddy and production.yml on the rootIncompetence
I ran docker-compose -f production.yml up and it is given some errors?Zellers
No. There should not be errors using docker-compose -f production.yml up, but trying to access the website through a browser it returns 404 Site 156.130.11.8 is not served on this interfaceIncompetence
Have you defined the DOMAIN_NAME environment variable in the .env file. It should be *:80 then only your setup will workZellers
@TarunLalwani I know, that should work but it doesn't. I can't access the website at all, with the IP or the domain.Incompetence
Let us continue this discussion in chat.Zellers
My answer probably was way too broad, was it?Saltpeter
M
6

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!

Mechling answered 5/6, 2019 at 7:49 Comment(2)
I already tried that and I am getting the same error: 404 Site 156.130.11.8 is not served on this interfaceIncompetence
is the 404 coming from caddy or django? Have a look at django's logs.Lapidate
S
1

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.

Saltpeter answered 24/6, 2019 at 10:50 Comment(5)
My settings have ALLOWED_HOSTS=["*"] :(Incompetence
Ah, my bad. And you didn't find a solution in the meantime?Saltpeter
And in your docker config your have the settings to port forward the ports?Saltpeter
I am not sure. this is my config: github.com/maguayo/django-starter-api/blob/master/… Probably has to do with docker..Incompetence
When you tried to access the IP address, did you specify the port? If you want you can take a deeper look on docker networking in the documentation, but as far as I can tell, yours looks pretty solid, as long as someone specifies the port.Saltpeter

© 2022 - 2024 — McMap. All rights reserved.