Reverse Proxy Randomly gives 404 Errors

48 views Asked by At

I am having an issue with a program that manages a map of remotes and server paths/subdomains to build a dynamic reverse proxy. So, domain/foo could be mapped to one server and domain/bar could be mapped to another server. It can also use subdomains like a traditional reverse-proxy

The way this works in code is that I use Gin middleware to intercept all requests. I then filter if it is a subdomain or if its a 404 error. These routes are not predefined whenever I setup the gin routes, so anything that should be routed to the reverse proxy gets an initial 404 error which I intercept. I then abort the middleware flow and then play with the routes and headers with both the Director and ModifyResponse methods of httputil.NewSingleHostReverseProxy until it suits my needs and then write it using httputil.ServeHTTP.

The problem is that I will randomly get 404 errors on any proxy that I setup using either the subdomain method or the path method. It is not consistent with which requests it gives a 404 on. If you refresh a static page with a bunch of links to the remote server, the same file that gave a 404 last refresh will give a 200 with the correct content, but there will be another 404 about a completely different file.

I have verified in every way that I could think that there are no 404's actually being given by the remote end and even setup logging in case there is a 404 error. I have tried creating a new Writer body for Gin to use in case something was already written to the old one. I honestly just cannot pinpoint where the issue is. Also, all of the code is made to only work with 1 domain that I use

This is the file for handling the remotes. The handler that is used is the function named "Handler". https://raw.githubusercontent.com/Masong19hippows/go-website/main/proxy/proxy.go

This is the file that calls the Handler. https://raw.githubusercontent.com/Masong19hippows/go-website/main/main.go

0

There are 0 answers