Конфигурация nginx, ошибка «connection refused»?

Добрый вечер.

Возникла проблема с настройкой проксирования nginx в docker контейнере.
При попытке подключиться к websocket получаю ошибку "NS_ERROR_WEBSOCKET_CONNECTION_REFUSED".

Nginx gateway
server {
   listen 80;
   server_name domain.ru;
   client_max_body_size 300M;
   server_tokens off;

   include /etc/nginx/snippets/certbot.conf;

   rewrite ^(.*) https://domain.ru$1 permanent;

}

server {
    listen 443 ssl http2;
    server_name domain.ru;
    client_max_body_size 300M;
    server_tokens off;

    ssl_certificate /etc/letsencrypt/live/domain.ru/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/domain.ru/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/domain.ru/chain.pem;

    include /etc/nginx/snippets/ssl.conf;
    include /etc/nginx/snippets/certbot.conf;

    location /ws {
        proxy_set_header  Host $host;
        proxy_set_header  Upgrade $http_upgrade;
        proxy_set_header  Connection "Upgrade";
        proxy_pass        http://websocket-nginx;
        proxy_http_version 1.1;
        proxy_redirect    off;
    }

    location / {
       proxy_set_header Host $host;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-Proto http;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header X-Forwarded-Host $remote_addr;
       proxy_set_header X-NginX-Proxy true;
       proxy_pass http://websocket-nginx;
       proxy_http_version 1.1;
       proxy_ssl_session_reuse off;
       proxy_redirect off;
    }
}


Websokcet nginx

server {
    listen 80;
    index index.html;
    root /app;
    charset utf-8;
    server_tokens off;

    add_header X-Frame-Options "SAMEORIGIN";

    location /ws {
        proxy_set_header  Host $host;
        proxy_set_header  Upgrade $http_upgrade;
        proxy_set_header  Connection "Upgrade";
        proxy_pass        http://websocket-nodejs:8000;
        proxy_redirect    off;
    }

    location / {
        proxy_set_header  Host $host;
        proxy_pass        http://websocket-nodejs:8000;
        proxy_redirect    off;
    }
}


Docker-compose

version: '3'
services:

  gateway:
    image: ${REGISTRY}/import:gateway-nginx-${IMAGE_TAG}
    restart: always
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /etc/letsencrypt:/etc/letsencrypt:ro
      - /var/www/html:/var/www/html:ro
    depends_on:
      - backend-nginx
      - frontend-nginx
      - import-rabbitmq
      - websocket-nginx

  websocket-nginx:
    image: ${REGISTRY}/import:ws-nginx-${IMAGE_TAG}
    restart: always
    volumes:
      - ./websocket:/websocket
    depends_on:
      - websocket-nodejs

  websocket-nodejs:
    image: ${REGISTRY}/import:nodejs-${IMAGE_TAG}
    volumes:
      - ./websocket:/websocket
    ports:
      - "8000:8000"
    tty: true


Пытаюсь обратиться к WS из backend сайта

let url = window.location.hostname;
let socket = new WebSocket('wss://' + url + '/ws');


Соединение в WS не происходит.

Как решить проблему?
  • Вопрос задан
  • 547 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы