Websocket on Docker with nginx

Related searches

I am trying to build a service with two parts: a backend and a frontend. Both are on a different docker container and are communicating through the docker-compose configuration and a nginx container.

For https access, everything's good, but when I am trying to work with websocket, I have an upgrade error, even if the Nginx configuration got this information

Error message : websocket: the client is not using the websocket protocol: 'upgrade' token not found in 'Connection' header

I am using a fasthttp and fasthttp/websocket for my Golang backend. The code is working on localhost (no nginx configuration), but the combinaison of Docker + nginx seems to break something. The front-end works with react and is a simple let socket = new WebSocket(wss.mydomain.com/ws/uploadPicture/);

EDIT :

  • when using ctx.Request.Header.ConnectionUpgrade() just before upgrader.Upgrade, the result is true, but ctx.Response.Header.ConnectionUpgrade() is false

Thank you !


Golang Backend

var upgrader = websocket.FastHTTPUpgrader{
    ReadBufferSize:  1024,
    WriteBufferSize: 1024,
    CheckOrigin: func(ctx *fasthttp.RequestCtx) bool {
        return true
    },
}
func    InitRouter() func(*fasthttp.RequestCtx) {
    router := fasthttprouter.New()

    router.GET("/ws/uploadPicture/", doWS)

    return router.Handler
}
func    doWS(ctx *fasthttp.RequestCtx) {
    err := upgrader.Upgrade(ctx, func(conn *websocket.Conn) {
        //SHOULD DO STUFF
    })
    if (err != nil) {
        logs.Error(err.Error()) //HIT THIS ERROR
        return
    }
}

...
fasthttp.ListenAndServe(`:8000`, InitRouter())

Nginx.conf

#############################################################################
## NGINX CONFIGURATION FOR THE WEBAPP
#############################################################################
upstream webapp {
    server webapp:3000;
    keepalive 4;
}
server {
    listen [::]:443 ssl;
    listen 443 ssl;

    server_name mydomain.com;

    ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    location / {
        proxy_pass http://webapp;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }
}

#############################################################################
## NGINX CONFIGURATION FOR THE PROXY
#############################################################################
upstream proxy {
    server proxy:8000;
    keepalive 4;
}
server {
    listen [::]:443 ssl;
    listen 443 ssl;

    server_name api.mydomain.com;

    ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    location / {
        add_header 'Access-Control-Allow-Origin' "$http_origin";
        add_header 'Access-Control-Allow-Credentials' true;
        proxy_pass http://proxy;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }
}


#############################################################################
## NGINX CONFIGURATION FOR THE PROXY
#############################################################################
upstream proxyws {
    server proxy:8000;
    keepalive 4;
}
server {
    listen [::]:443 ssl;
    listen 443 ssl;

    server_name wss.mydomain.com;

    ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    location / {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection upgrade;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_redirect off;
        proxy_pass http://proxyws;
    }

}

Docker-compose

  #############################################################################
  ## IMAGE FOR THE PROXY
  #############################################################################
  proxy:
    container_name: proxy
    build: ./src/Proxy
    restart: always
    env_file: .env
    ports:
      - "8000:8000"

  #############################################################################
  ## IMAGE FOR THE WEBAPP
  #############################################################################
  webapp:
    container_name: webapp
    build: ./src/Webapp
    restart: always
    volumes:
      - ./src/Webapp:/home/app
      - /home/app/.next
      - /home/app/node_modules
    ports:
      - 3000:3000

  #############################################################################
  ## IMAGE THE NGINX & CERTBOT FOR REVERSE PROXY
  #############################################################################
  nginx: 
    image: nginx:latest
    container_name: nginx
    volumes:
      - ./data/nginx:/etc/nginx/conf.d
      - ./data/certbot/conf:/etc/letsencrypt
      - ./data/certbot/www:/var/www/certbot
    ports:
      - 80:80
      - 443:443

Turns out it's not an issue with Nginx/Docker, but with the http package in golang that is supposed to upgrade the connection. I was using fasthttp with fasthttp/websocket wich is supposed to work, but which does not in a docker container.

I tried to switch to httprouter with the official (not fork as for the fasthttp ones) gorilla/websocket and the connection successfully upgrade.

Gonna check where the issue come from !

mikesir87/docker-nginx-elb-websocket, Forked from stockflare/docker-nginx-elb-websocket to add host header to forwarded requests. Container. OverviewTagsDockerfileBuilds. This� NGINX as a WebSocket Proxy reverse proxy server, WebSocket The WebSocket protocol provides a way of creating web applications that support real‑time bidirectional communication between clients and servers. Part of HTML5, WebSocket makes it much easier to develop these types of applications than the methods previously available.

I'm the maintainer of websocket package for fasthttp. The issue is fixed on master branch.

Downdload it with:

go get github.com/fasthttp/websocket@master

And try again, please.

If the issue continue, please open an issue in https://github.com/fasthttp/websocket/issues

Soon, I will release a new version.

Setting up websocket with docker nginx and react - General, 0down votefavorite 1 I was developing an app which receives twitter feed in live format and everything worked fine untill i tested the production� Problem trying to connect nginx with websocket in docker I have some issues trying to connect my nginx container to websocket. In context: I have a webapp made with django and I added some features with redis and django-channels, everything is correct but the websocket cannot connect, this is the message that returns:

I believe upgrade should be a string:

proxy_set_header Connection "upgrade";

Using NGINX as a WebSocket Proxy, One is that WebSocket is a hop‑by‑hop protocol, so when a proxy server intercepts an Upgrade request from a client it needs to send its own� Now that the basics of docker-compose are clear, lets move on to Nginx. Nginx Nginx is a web server with a wide array of features, including reverse proxying, which is what it is used for in this article. It is configured with a nginx.conf. By default it looks for it in /etc/nginx/nginx.conf, but it is of course possible to specify another file.

Websocket connection can't be made � Issue #412 � nginx-proxy , I do have an nginx proxy already, so don't use the "web" service. I followed (I tried at least) all mattermost / mattermost-docker � Watch 67 � Star 839 but still have the websocket connection error. any idea? nginx config:. Once you have logged into Docker, enter “NGINX” into the top search bar and press enter. The official NGINX image should be the first image in the search results. You will see the “OFFICIAL IMAGE” label in the top right corner of the search entry. Now click on the nginx result to view the image details.

websocket error when using with nginx proxy � Issue #282 , The goal of this guide is to help you build a docker-compose setup that runs nginx in one container and a service for obtaining and renewing HTTPS certificates in another.

I am using Activemq as broker, client side is Paho js to send websocket request(ws protocol). Everything work fine on server, but when the server start to set nginx as proxy, the client can not connect to server( but I can connect to Activemq UI web). My nginx setting is:

Comments
  • I tries it, unfortunately it's not the solution for this issue