简体   繁体   English

PHP websockets with Workerman - nginx 配置

[英]PHP websockets with Workerman - nginx configuration

I'm trying to use Workerman in my project, but having issues with nginx configuration.我正在尝试在我的项目中使用 Workerman,但 nginx 配置存在问题。 I'm using docker on my local machine and socket.my-app.local is translated to 127.0.0.1 in my local host file.我在本地机器上使用 docker 并且socket.my-app.local在我的本地主机文件中被翻译为127.0.0.1 I've followed instructions for nginx reversed proxy, and set it up like this:我已按照 nginx 反向代理的说明进行操作,并将其设置如下:

server {
    listen 80;
    listen [::]:80;

    server_name socket.my-app.local;

    location /socket.io/ {
        proxy_pass http://localhost:2020/socket.io/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header X-Real-IP $remote_addr;
    }

    location / {
        proxy_pass http://localhost:2020;
    }
}

my PHP worker that should be websocket server is configured like this:我的 PHP 工作人员应该是 websocket 服务器配置如下:

require_once __DIR__ . '/../vendor/autoload.php';

use Workerman\Worker;

// Create a Websocket server
$ws_worker = new Worker("websocket://0.0.0.0:2020");

// 4 processes
$ws_worker->count = 4;

// Emitted when new connection come
$ws_worker->onConnect = function($connection) {
    echo "New connection\n";
};

// Emitted when data received
$ws_worker->onMessage = function($connection, $data) {
    // Send hello $data
    $connection->send('hello ' . $data);
};

// Emitted when connection closed
$ws_worker->onClose = function($connection) {
    echo "Connection closed\n";
};

// Run worker
Worker::runAll();

and html file I am testing with:和 html 文件我正在测试:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Workerman Sockets Test</title>
</head>
<body>
<h3>Hello</h3>
<script type="text/javascript">

    var socket = new WebSocket("ws://socket.my-app.local:2020/socket.io");

    socket.onopen = function() {
        alert("Connection established.");
    };

    socket.onclose = function(event) {
        if (event.wasClean) {
            alert('The connection is closed.');
        } else {
            alert('Connection failure'); // for example, the server process is "killed"
        }
        alert('Code: ' + event.code + ' reason: ' + event.reason);
    };

    socket.onmessage = function(event) {
        alert("Received data: " + event.data);
    };

    socket.onerror = function(error) {
        alert("Error: " + error.message);
    };
</script>
</body>
</html>

I'm unable to connect to ws server, and I'm not sure how to test, at which point it went wrong.我无法连接到 ws 服务器,并且我不确定如何测试,此时它出错了。

Could it be because php-fpm and webserver are on different docker containers?可能是因为php-fpmwebserver位于不同的 docker 容器上吗?

Issue was docker containers use internal namespace to communicate, so I had to change localhost to container name, php-fpm in my case, and I had to open websocket port on webserver container, as well as to listen to that port on webserver, in order to proxy those requests.问题是 docker 容器使用内部命名空间进行通信,所以我必须将localhost更改为容器名称,在我的情况下为php-fpm ,并且我必须在 webserver 容器上打开 websocket 端口,并在 webserver 上监听该端口,在为了代理这些请求。

server {
    listen 80;
    listen 2020;
    listen [::]:80;
    listen [::]:2020;

    server_name socket.my-app.local;

    location /socket.io/ {
        proxy_pass http://php-fpm:2020;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header X-Real-IP $remote_addr;
    }

    location / {
        
    }
}

and in my docker-compose在我的 docker-compose

services
  webserver:
    ...
    ports:
      - "80:80"
      - "2020:2020"
      - "2021:2021"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM