1

我有点困惑。我以这种方式在本地启动达芙妮:daphne common.asgi:channel_layer --port 8338一切都“没问题”。当我使用curl -v 127.0.0.1:8338得到以下输出

* Rebuilt URL to: 127.0.0.1:8338/
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 8338 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1:8338
> User-Agent: curl/7.51.0
> Accept: */*

但是当我尝试使用端口分配启动 docker 容器时,并没有说 8338 端口已经被使用:

    docker run \
    -tid \
    -p 8338:8338 \
    -v $(PWD):/app \
    --network matryoshka_net \
    --hostname matryoshka_daphne \
    --name matryoshka_daphne \
    matryoshka_daphne

在 daphne 已经启动的情况下,运行上面的代码是“好的”。所以在我看来,端口分配不正确。

我错过了什么?

所以这产生了下一个问题,我无法通过 nginx 将信号重定向到 websockets 到我的 docker 容器。因为端口 8338 上没有任何内容(仅启动容器时)。这是 nginx.conf:

server {
    listen 127.0.0.1;

    gzip on;
    gzip_types text/plain application/json text/css application/x-javascript text/javascript application/javascript;

    location / {
        proxy_set_header Host $host;
        proxy_read_timeout 20s;
        client_max_body_size 10m;
        proxy_pass http://127.0.0.1:8000;
    }

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

1 回答 1

1

我了解您在运行 docker 时在同一台机器上安装了 nginx。在 proxy_pass 中,您应该指向 docker IP,它应该是 0.0.0.0,因此 conf 中的行应该如下所示:

proxy_pass http://0.0.0.0:8338;

这两个位置应该是相同的:location /并且location /ws/因为您daphne在端口 8338 上运行。

请看一下我的示例conf(它正在使用daphneand gunicorn)但是可以更改为仅使用daphne.

于 2018-10-25T11:30:25.733 回答