Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need a failover proxy to localhost/remote server #1

Closed
McPizza0 opened this issue Feb 16, 2024 · 17 comments
Closed

Need a failover proxy to localhost/remote server #1

McPizza0 opened this issue Feb 16, 2024 · 17 comments

Comments

@McPizza0
Copy link
Owner

TL;DR: Have 1 address abc.123.com. when I am running dev server locally, route to me. If not, route to another address.

Long version
UnInbox, has some long running servers that live in a vps and send out webhook messages

I need a proxy with failover capabilities to live permanently online (vps is fine)
when the app is running locally on my machine, it will route to me
when im not, it will route to another web server

I tried with Cloudflare Tunnels and CaddyServer
i can access the tunnel directly using https://tunnelAddress.mydomain.dev

CaddyServer installed on a vps and given it the following config

proxy.mydomain.dev {
reverse_proxy * {
                to https://tunnelAddress.mydomain.dev https://failover.mydomain.dev
                lb_policy first
                lb_try_duration 5s
                lb_try_interval 250ms

                fail_duration 30s
                max_fails 1
                unhealthy_status 5xx
                unhealthy_latency 2s
                unhealthy_request_count 1
        }
}

the path https://tunnelAddress.mydomain.dev works fine when accessed directly - validating that the tunnel is up
when accessing via proxy.mydomain.dev i get an error ERR_TOO_MANY_REDIRECTS

Offering a bounty for a solution that works

Copy link

algora-pbc bot commented Feb 16, 2024

💎 $25 bounty created by McPizza0
🙋 If you start working on this, comment /attempt #1 along with your implementation plan
👉 To claim this bounty, submit a pull request that includes the text /claim #1 somewhere in its body
📝 Before proceeding, please make sure you can receive payouts in your country
💵 Payment arrives in your account 2-5 days after the bounty is rewarded
💯 You keep 100% of the bounty award
🙏 Thank you for contributing to McPizza0/random!

👉 Add a bountyShare on socials

Attempt Started (GMT+0) Solution
🟢 @TheVixhal Feb 17, 2024, 7:48:50 AM WIP

@McPizza0 McPizza0 changed the title Need a failover proxy to localhost/remove server Need a failover proxy to localhost/remote server Feb 16, 2024
@McPizza0
Copy link
Owner Author

conditions for bounty:

you must provide a working config via PR and explain how it works/what needs to be set up
if you're using cloudflare tunnels, you need to provide details config
if using caddy, provide detailed config

put configs in a pr, I will test, if it works, bounty is yours

@TheVixhal
Copy link

TheVixhal commented Feb 17, 2024

/attempt #1

@McPizza0
Copy link
Owner Author

the caddy server lives in a vps, not on my local machine, can you clarify how @Local would help?

have you tried this config with a cloudflare tunnel?

the Caddy config was fine, forwarding requests to the cloudflare tunnel
but somewhere between them, there were some issues with too many redirects

https://tunneladdress.mydomain.dev/ will be a cloudflare tunnel into my local system

@TheVixhal
Copy link

TheVixhal commented Feb 17, 2024

#1

Thank you for the clarification. If CaddyServer is running on a VPS, the @Local matcher wouldn't be applicable because it's designed to identify requests originating from localhost, which would be the local machine where CaddyServer isn't running in this case.

For your scenario with Cloudflare Tunnel and CaddyServer on a VPS, you can simplify the configuration to directly proxy requests to the Cloudflare Tunnel address and the failover server.

proxy.mydomain.dev {
    reverse_proxy {
        to https://tunnelAddress.mydomain.dev
        to https://failover.mydomain.dev
    }
}

This configuration file instructs CaddyServer to proxy all incoming requests to either https://tunnelAddress.mydomain.dev or https://failover.mydomain.dev based on their availability and health.

Please replace proxy.mydomain.dev, https://tunnelAddress.mydomain.dev, and https://failover.mydomain.dev with your actual domain and addresses respectively.

Once you've updated the configuration file, save it as Caddyfile in your CaddyServer's configuration directory. Then, restart CaddyServer for the changes to take effect.

@McPizza0
Copy link
Owner Author

Sounds a bit like a GPT

Re-read the issue again, that is how the config file was
the issue is not there
test it yourself

@TheVixhal
Copy link

Re-read the issue again, that is how the config file was
the issue is not there
test it yourself

share your cadyserver logs.

@McPizza0 McPizza0 reopened this Feb 17, 2024
@ezhil56x
Copy link

ezhil56x commented Feb 17, 2024

@McPizza0

  • You are running an application on your VPS which is accessible through abc.123.com. And when you are running the same on your local machine you need abc.123.com to point to your local machine. Is this the expected behaviour?
  • And do you have a Static IP or want to make it public with Cloudflare tunnel?
  • Is the Caddy server and the application running on same VPS?

@McPizza0
Copy link
Owner Author

@ezhil56x

  • I am running a service online on VPS A which is accessible through service.123.com
  • I also occasionally run the same service locally on my machine, which is accessible online at local.123.com through a cloudflare tunnel
  • Caddy is running on a different vps online, through caddy.123.com

I want to be able send webhook messages to caddy.123.com and if I am online, it forwards them to local.123.com, if I am offline it forwards them to service.123.com

@ezhil56x
Copy link

@McPizza0

Thanks for the info. It would be easier to troubleshoot if we could schedule an online meet

@McPizza0
Copy link
Owner Author

its easy enough to replicate the setup
dont need a meeting for that

im not looking for "technical support" and diagnosis, i need a solution that works

@TheVixhal
Copy link

@ezhil56x

  • I am running a service online on VPS A which is accessible through service.123.com
  • I also occasionally run the same service locally on my machine, which is accessible online at local.123.com through a cloudflare tunnel
  • Caddy is running on a different vps online, through caddy.123.com

I want to be able send webhook messages to caddy.123.com and if I am online, it forwards them to local.123.com, if I am offline it forwards them to service.123.com

caddy.123.com {

    # Define a route for handling errors
    handle_errors {
    
        # Define a condition for when to trigger this route
        @offline {
            expression {http.error.status_code} == 502 # 502 Bad Gateway
        }
        
        # Proxy requests to service.123.com when offline
        reverse_proxy @offline https://service.123.com
    }

    # Define a route for normal operation
    reverse_proxy https://local.123.com {
    
        # Set the Host header to the upstream host
        header_up Host {http.reverse_proxy.upstream.hostport}
    }
}

Replace caddy.123.com, local.123.com, and service.123.com with your actual domain names.

@McPizza0
Copy link
Owner Author

@TheVixhal
in theory, should work great, nice directive with the handle_errors

In practice, it dosnt work
when the tunnel is down, initially it gives a 504, but the proxy condition didnt work
then it switched to a different error
image

so it seems that the tunnel always responds with something

this may not be viable after all
would need to add a way for it to check if the host is up by hitting a /health endpoint and only forwarding if it gets back a 200 code

@McPizza0
Copy link
Owner Author

Thanks for trying @ezhil56x and @TheVixhal

closing this issue and removing the bounty
already spent 6 hours on this

will build out something better in less time: https://github.com/uninbox/webhook-proxy

@francislavoie
Copy link

@francislavoie
Copy link

francislavoie commented Feb 17, 2024

@TheVixhal stop using ChatGPT. That config is complete nonsense.

Source: I'm a Caddy maintainer. I wrote most of the Caddyfile docs.

@TheVixhal
Copy link

@TheVixhal stop using ChatGPT. That config is complete nonsense.

Source: I'm a Caddy maintainer. I wrote most of the Caddyfile docs.

Sorry 😔😔😐

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants