E
E
Evgeny Romashkan2018-08-19 12:17:27
Nginx
Evgeny Romashkan, 2018-08-19 12:17:27

Why 502 bat gateway(nginx, websocket)?

Good afternoon.
I'm trying to set up nginx for webscoket.
Launched such a server

ws_server.php
<?php
$ws = new swoole_websocket_server('127.0.0.1', 9502);

$ws->on('open', function ($ws, $request) {
//    print_r($request);
    $ws->push($request->fd, "hello, welcome!\n");
});

$ws->on('message', function ($ws, $frame) {
    echo "We recieve message: {$frame->data}\n";
    $ws->push($frame->fd, "Got it!\n");
});

$ws->on('close', function ($ws, $fd) {
    echo "client-{$fd} is closed\n";
});

$ws->start();

Here is the config for the site:
nginx config
server {
    listen 80;
    listen [::]:80;

    server_name ws-chat.test;
    root /var/www/ws-chat;
    index index.php index.html index.htm;

    location /ws/ {
        proxy_pass http://127.0.0.1:9502;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass php-upstream;
        fastcgi_index index.php;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #fixes timeouts
        fastcgi_read_timeout 600;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }

    location /.well-known/acme-challenge/ {
        root /var/www/letsencrypt/;
        log_not_found off;
    }

    error_log /var/log/nginx/app_error.log;
    access_log /var/log/nginx/app_access.log;
}

For some reason, ws-chat.test/ws issues 502 Bad Gateway (nginx)
Based on this post - https://www.nginx.com/blog/websocket-nginx/
If anything, this is all done inside docker(laradock).
I connect via js UPD: I decided. In the nginx config it was necessary to registervar socket = new WebSocket("ws:ws-chat.test/ws/");
proxy_pass http://workspace:9502;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Romashkan, 2018-08-19
@EvgeniiR

UPD: Decided. In the nginx config it was necessary to registerproxy_pass http://workspace:9502;

K
Konstantin Malyarov, 2018-08-19
@Konstantin18ko

What's in the header?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question