Networking Server behind nginx

Hallo,
like the title says, i want to host an instance of the networking server(GitHub - needle-engine/networking-local: Run the Needle Engine Networking server locally or host your own) behind nginx inside a docker container.

I manage to get it to work without nginx, but not behind the reverse-proxy.
Every article on the internet says more or the less the same how to proxy a websocket connection, so my conclusion is, that there are some details on the Needle Engine side that make problems.

My nginx config is as follows:

server {
listen 80;
listen [::]:80;

    location /game/server {
        proxy_pass http://websocket/socket;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
    }
}

upstream websocket  {
    # game-net is the service name of the container holding the server
    server game-net:9001;
}

My dockerfile containing the server is as follows:

# create node container
FROM alpine:3.17 as build

# install git
RUN apk add git

# set working dir
WORKDIR /repo

# install repo
RUN git clone https://github.com/needle-engine/networking-local.git

# create final image
FROM node:18.16.0-alpine3.17

WORKDIR /app

# copy only what is needed
COPY --from=build /repo/networking-local/express ./

# install dependencies
RUN npm install

# set port
ENV port=9001

# expose port
EXPOSE 9001

# run websocket server
CMD [ "npm", "start"]

And in Unity on the networking component my settings are:

 Localhost: http://localhost/game/server

I am not an expert at websockets, so i don’t really know where the problem could be. As i said before, without nginx and

Localhost: ws://localhost:9001/socket

it works. I hope i am at the right place to ask this here.
Many thanks in advance.
Wutterlfy

Original Post on Discord

by user 1122855088558456862

Hello there – we have an experimental AI bot :robot: that might be able to help you with your question. Would you like to try this out?

How it works:
Reply to the messages of the bot that end with a thread_id to interact with it and ask follow-up questions. You can also mention the bot directly in this channel to get help.

Help requested: please wait a moment…

Here is what I found for you :brain: Discord

Hello Wutterfly,

It seems that you are trying to set up an Nginx reverse proxy to work with a WebSocket server for the Needle Engine networking component. Your configuration files look generally correct, but there may be a few adjustments and checks to ensure that the WebSocket connection is properly proxied.

Here are the steps to troubleshoot and potentially resolve the issue:

  1. Inspect Logs: Check both Nginx and Docker container logs for any error messages that could provide more insight into the issue.

  2. Correct Proxy Pass: Ensure the proxy_pass directive points to the correct internal WebSocket path. If the WebSocket server is expecting connections at /socket, the proxy_pass should directly match this.

  3. Use WebSocket Scheme: Try changing the scheme in the Unity networking component to ws:// instead of http:// since it is a WebSocket connection.

  4. Server Name: Make sure the server_name directive is correctly set in your Nginx configuration if you have multiple subdomains or specific hostname patterns.

  5. Correct Headers: Verify that the WebSocket server expects the exact headers you are setting in Nginx. The necessary headers typically include Upgrade, Connection, and Host, but there might be additional ones needed by your specific setup.

  6. Firewall Rules: Check if there are any firewall rules on the host system that could be blocking the WebSocket connections to the port Nginx is listening on.

  7. Nginx Configuration Test: Use the nginx -t command to test your Nginx configuration for syntax errors.

  8. Nginx Access: Confirm that Nginx is accessible from outside your Docker network, ensuring that it listens on the correct external interfaces.

  9. WebSocket Test Tool: Use a WebSocket testing tool to directly interact with the Nginx server and observe the behavior.

thread_J39g0vxxVthesffjxe3735qD

  1. SSL/TLS Configuration: If you eventually move to using wss:// (WebSocket Secure), ensure that the SSL/TLS certificates are properly configured and accessible to Nginx.

Adjust your settings based on these recommendations and attempt to reconnect through the proxy.

If you continue to experience issues, please double-check the documentation for Nginx WebSocket proxying and the specifics of the Needle Engine’s networking expectations. Additionally, consider reaching out to both Nginx and Needle Engine communities for support specific to these technologies.

1 messages left GPT 4
thread_J39g0vxxVthesffjxe3735qD
Thanks for your feedback!

i am impressed, very nice!

by user 1122855088558456862

I’m glad you found the information helpful, Wutterfly! If you encounter any more challenges or have additional questions, feel free to ask. Good luck with setting up the Nginx reverse proxy for the Needle Engine networking server!

0 messages left GPT 3.5
thread_J39g0vxxVthesffjxe3735qD

Cool :slightly_smiling_face: mind letting us know which of the recommendations fixed your particular issue?

Specially point 3 was usefull. In the describtion of the URL field it says that the protocol can be http, ws, https or wss. And because websockets Start from a http connection, i thought the correct protocol would be http.

by user 1122855088558456862