Skip to content

🐛 [Bug]: Proxy middleware and Vite dev server on localhost, tcp4 127.0.0.1:5173: connect: connection refused #2890

@sixcolors

Description

@sixcolors

Bug Description

When using the proxy middleware to proxy.BalanceForward([]string{"http://localhost:5173"})) to a Vite/react dev server running on port 5173 the connection is refused with the following output Error: The HTTP request failed with error dial tcp4 127.0.0.1:5173: connect: connection refused

How to Reproduce

Clone https://github.com/sixcolors/FiberReactTest

cd frontend
npm install --include=dev && npm run dev

The proxy demonstrates this behaviour as can be seen in the main.go:

if DevMode {
		app.Get("*", proxy.BalancerForward([]string{"http://localhost:5173"}))

However the issue has been isolated to the fasthttp client.

To verify run the following:

package main

import (
	"io"
	"log"
	"net/http"

	"github.com/valyala/fasthttp"
)

func main() {
	// test connection to http://localhost:5173
	resp, err := http.Get("http://localhost:5173")
	if err != nil {
		log.Fatalf("Error: The HTTP request failed with error %s", err)
	} else {
		defer resp.Body.Close()
		data, err := io.ReadAll(resp.Body)
		if err != nil {
			log.Fatalf("Error reading body: %v", err)
		}
		log.Printf("HTTP Response Status: %d %s", resp.StatusCode, http.StatusText(resp.StatusCode))
		log.Printf("Response Body: %s", string(data))
	}

	statusCode, body, err := fasthttp.Get(nil, "http://localhost:5173")
	if err != nil {
		log.Fatalf("Error: The HTTP request failed with error %s", err)
	} else {
		log.Printf("HTTP Response Status: %d", statusCode)
		log.Printf("Response Body: %s", body)
	}
}

Expected Behavior

fasthttp client should connect to http://localhost:5173 and the proxy should work.

Actual Behavior

fasthttp client will not try dialing IPv6 by default and only IPv4 is tried, resulting in a connection refused error.

Fiber Version

v2.52.2

Code Snippet (optional)

No response

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my problem prior to opening this one.
  • I understand that improperly formatted bug reports may be closed without explanation.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions