Parcel: Nginx Config to allow both websocket and http forwarding?

Created on 30 Jan 2019  ·  2Comments  ·  Source: parcel-bundler/parcel

❔ Question

Are there any nginx configs available which allow me to forward port 80 and websocket port requests to my docker image?

🔦 Context

I am a beginner with nginx.

Parcel's dev server allows for files to be downloaded over port 80 and auto-reload using a websocket which can be configured to any free socket.

Since I am using docker and nginx, I would like to know if any configs exist which showcase how to proxy requests to the parcel dev server docker service

🌍 Your Environment

| Software | Version(s) |
| ---------------- | ---------- |
| Parcel | latest
| Node | docker node:8-stretch
| npm/Yarn | latest
| Operating System | ~

Question

Most helpful comment

A little late to the game but I got it working by doing this:

package.json

 "start": "parcel ./src/index.html --hmr-port 1235"

docker-compose add this for nginx service

    ports:
      - '80:80'
      - '1235:1235'

nginx config

events {
    worker_connections 1024;
}

http {
    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }

    upstream websocket {
        server localhost:1235;
    }

    server {
        listen 1235;
        server_name localhost;
        resolver 127.0.0.11;

        location / {
            set $upstream http://docker-compose-client-service-name:1235;

            proxy_pass $upstream;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
        }
    }

    server {
        listen 80;
        server_name localhost;
        resolver 127.0.0.11;

        location / {
            set $upstream http://docker-compose-client-service-name:1234;
            proxy_pass $upstream;
        }
  }
}

All 2 comments

Not really in the scope of parcel but this blog post shows how to do this:
https://www.nginx.com/blog/websocket-nginx/

Message me at the email in my profile if you would like more help.

A little late to the game but I got it working by doing this:

package.json

 "start": "parcel ./src/index.html --hmr-port 1235"

docker-compose add this for nginx service

    ports:
      - '80:80'
      - '1235:1235'

nginx config

events {
    worker_connections 1024;
}

http {
    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }

    upstream websocket {
        server localhost:1235;
    }

    server {
        listen 1235;
        server_name localhost;
        resolver 127.0.0.11;

        location / {
            set $upstream http://docker-compose-client-service-name:1235;

            proxy_pass $upstream;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
        }
    }

    server {
        listen 80;
        server_name localhost;
        resolver 127.0.0.11;

        location / {
            set $upstream http://docker-compose-client-service-name:1234;
            proxy_pass $upstream;
        }
  }
}

Was this page helpful?
0 / 5 - 0 ratings