Search code examples
nginx-reverse-proxyserver-sent-events

How do i tunnel multiple dynamic SSE connections from my back-end through an Nginx reverse proxy?


i have a backend server that listens to SSE connections on port 5001 once a call is being made and the connection is initialized, the backend server will chose an available port between the range of 5010-6000 and send a redirect response to the client, this is working well i try not to change this architecture.

now i want to add an Nginx reverse proxy in front of this server but when the client connects, the redirect request arrives with the localhost URL (probably of the nginx) and of course the remote client cannot connect there.

is there a way to maintain the "real" IP address of the back-end server in the response? or maybe another way to achieve this?

i have tried to mess around with various nginx flags and headers but i don't have a lead on which direction i should focus.


Solution

  • so apparently you can set a regex in nginx location:

    location ~ ^/path/to/sse/([^/]+) {
                proxy_pass https://127.0.0.1:$1;
    

    this will forward the request to the port requested