Ratchet icon indicating copy to clipboard operation
Ratchet copied to clipboard

How to pass request throw the nginx?

Open templton opened this issue 3 years ago • 1 comments

I have such config:


http {
  server {
      listen 80;

      location / {
          access_log off;
          proxy_pass http://localhost:8080;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

          proxy_read_timeout 86400s;
          proxy_send_timeout 86400s;

          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection "upgrade";
      }
  }
}

I use docker with server port 8180. I use main example from this repo for creating websocket server.


<?php
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

// Make sure composer dependencies have been installed
require __DIR__ . './../../vendor/autoload.php';

/**
 * chat.php
 * Send any incoming messages to all connected clients (except sender)
 */
class MyChat implements MessageComponentInterface {
    protected $clients;

    public function __construct() {
        $this->clients = new \SplObjectStorage;
    }

    public function onOpen(ConnectionInterface $conn) {
        $this->clients->attach($conn);
    }

    public function onMessage(ConnectionInterface $from, $msg) {
        foreach ($this->clients as $client) {
            if ($from != $client) {
                $client->send($msg);
            }
        }
    }

    public function onClose(ConnectionInterface $conn) {
        $this->clients->detach($conn);
    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
        $conn->close();
    }
}

// Run the server application through the WebSocket protocol on port 8080
$app = new Ratchet\App('localhost', 8080);
$app->route('/chat', new MyChat, array('*'));
$app->route('/', new MyChat, array('*'));
$app->route('/echo', new Ratchet\Server\EchoServer, array('*'));
$app->run();

And it works into container on port 8080

But if I call websocket outside of the container I get issue:

error: Unexpected server response: 502

templton avatar Jul 22 '21 09:07 templton

I use this;

location /chat/ {

  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection "upgrade";
proxy_pass http://127.0.0.1:8093;
proxy_http_version 1.1;

} Just modify it to make it meet your needs.

ahmeteminkocal avatar Aug 13 '21 20:08 ahmeteminkocal