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

Websockets add path for proxy #859

Closed
bfoxstudio opened this issue Nov 9, 2021 · 8 comments
Closed

Websockets add path for proxy #859

bfoxstudio opened this issue Nov 9, 2021 · 8 comments

Comments

@bfoxstudio
Copy link

client connect from Inernet o wss://host.com:port1/proxy_nginx_path
nginx redirect to ws://host_locla_company_network:port2/proxy_path

I made a mistake. we need fix nats.go client library
https://github.com/nats-io/nats.go/blob/main/ws.go#L563
ustr += u.Path
...

@kozlovic
Copy link
Member

kozlovic commented Nov 9, 2021

@bfoxstudio Again, since URLs that are discovered will not have the path, I wonder if this should be an option and then added to the http's URL (unless of course the provided URL has already a path, then use that one instead). What do you think?

@mei-rune
Copy link

I had the same problem! I fix this problem by:

	// For http request, we need the passed URL to contain either http or https scheme.
	scheme := "http"
	if tlsRequired {
		scheme = "https"
	}
	ustr := fmt.Sprintf("%s://%s", scheme, u.Host)
	u, err = url.Parse(ustr)
	if err != nil {
		return err
	}

change to

	// For http request, we need the passed URL to contain either http or https scheme.
	u.Scheme = "http"
	if tlsRequired {
		u.Scheme = "https"
	}

@mei-rune
Copy link

mei-rune commented Dec 21, 2021

@bfoxstudio I deplpy nats-server behind the nginx;

nginx will route request to nats-server by the url path.

          +---------------+
          |               |                +-----------------+
          |               |   /queue       |                 |
+-------->+  Nginx        +---------------->   Nats-server   |
          |               +------+         |                 |
          |               |      |         +-----------------+
          +---------------+      |         +-----------------+
                                 |         |                 |
                                 +--------->   My app        |
                                 /myapp    |                 |
                                           +-----------------+

@shlomi-wexler-apolicy
Copy link

I had the same problem! I fix this problem by:

	// For http request, we need the passed URL to contain either http or https scheme.
	scheme := "http"
	if tlsRequired {
		scheme = "https"
	}
	ustr := fmt.Sprintf("%s://%s", scheme, u.Host)
	u, err = url.Parse(ustr)
	if err != nil {
		return err
	}

change to

	// For http request, we need the passed URL to contain either http or https scheme.
	u.Scheme = "http"
	if tlsRequired {
		u.Scheme = "https"
	}

We encountered the same issue and I think there are a few lines of codes missing in the solution. This is the minor change I ended up doing to include the url path:

scheme := "http"
if tlsRequired {
	scheme = "https"
}
ustr := fmt.Sprintf("%s://%s%s", scheme, u.Host, u.Path)
u, err = url.Parse(ustr)
if err != nil {
	return err
} 

@kozlovic
Copy link
Member

kozlovic commented May 2, 2022

@shlomi-wexler-apolicy The fix on initial connect here is one thing, but as I posted earlier, what about the case of discovered URLs from the server, which are bare host:port. So I wonder if instead it should not be an option that is then added any time a websocket connection is attempted.

@shlomi-wexler-apolicy
Copy link

@shlomi-wexler-apolicy The fix on initial connect here is one thing, but as I posted earlier, what about the case of discovered URLs from the server, which are bare host:port. So I wonder if instead it should not be an option that is then added any time a websocket connection is attempted.

@kozlovic looking at the code every websocket connection which is opened eventually goes through the wsInitHandshake function. In my proposed change above if its a bare url with only host:port the ustr variable will contain only that since the Path is empty. Am I missing something?

@kozlovic
Copy link
Member

kozlovic commented May 2, 2022

@shlomi-wexler-apolicy Suppose that you provide the initial connect URL as ws://host:port/my_path, what I am saying is that /my_path would be present only the first time you connect. Any other URL that has been discovered by the library (with the server gossiping the websocket listen hosts/ports of the NATS cluster) would not have this path, because wsInitHandshake() would be provided with an URL that does NOT have your initial path.

@kozlovic
Copy link
Member

Addressed by #974

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

No branches or pull requests

4 participants