9

First time with nginx.
I have a nodejs WebSocket server listening at ws://service_name:3600.
I'm using docker-compose:

version: "2"
services:
   # stuff

   service_name:
      image: imagename
      ports:
        - 3600:3600
      links:
        # stuff
        - proxy

   proxy:
     image: image-from-nginx-with-custom-config
     ports:
       - 80:80
       - 443:443
       - 8443:8443

My config:

// stuff 

server {
    listen          8443;
    server_name     localhost;
    ssl on;

    ssl_certificate      /etc/nginx/certs/crt.pem;
    ssl_certificate_key  /etc/nginx/certs/key.pem;

    keepalive_timeout    60;

    proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https;

    location / {
        proxy_pass ws://service_name:3600;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

I get nginx: [emerg] invalid URL prefix in /etc/nginx/conf.d/default.conf at startup.
So nginx doesn't recognize ws, what do I do?

andr
  • 93
  • 2
  • 4
  • Possible duplicate of [Nginx invalid URL prefix](https://stackoverflow.com/questions/32992908/nginx-invalid-url-prefix) – Luca Davanzo Oct 18 '17 at 10:03
  • In nginx your still need to use http for your url. `proxy_pass http://service_name:3600;` – Tarun Lalwani Oct 18 '17 at 11:35
  • @TarunLalwani I've tried doing just that but it doesn't seem to work. I may have something wrong in my configuration then... – andr Oct 18 '17 at 12:28
  • @TarunLalwani It works! thank you, add your answer and I'll mark it as correct – andr Oct 18 '17 at 12:40

1 Answers1

18

In nginx you still need to use http for protocol in your url and not ws.

proxy_pass http://service_name:3600;

The ws and wss protocol is required for browser, on server side you add below to handle the websockets over http

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade"; 
Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265