3

我试图在两个 docker 容器之间建立连接。一个是 nginx 服务器 + Symfony 应用程序,第二个是 Mercure。

我的应用程序运行良好,docker-compose build 也可以正常运行。问题是当我尝试测试它并尝试从 Symfony 应用程序向 Mercure 发布内容时,我收到 500 内部服务器错误,并显示“无法发送更新”。信息。

在堆栈跟踪中,我可以看到我得到了 TransportException 错误,其 env 变量与我在.env文件中的不同。应用程序正在尝试使用 HTTPS 而不是 .env 文件中定义的 HTTP。

Symfony\Component\HttpClient\Exception\TransportException:“https://mercure/.well-known/mercure”的 SSL 连接错误。在供应商/symfony/http-client/Chunk/ErrorChunk.php:65

###> symfony/mercure-bundle ###
# See https://symfony.com/doc/current/mercure.html#configuration

# The URL of the Mercure hub, used by the app to publish updates (can be a local URL)
MERCURE_URL=http://mercure/.well-known/mercure

# The public URL of the Mercure hub, used by the browser to connect
MERCURE_PUBLIC_URL=http://localhost:9090/.well-known/mercure

MERCURE_PUBLISH_URL=http://mercure/.well-known/mercure

# The secret used to sign the JWTs
MERCURE_JWT_SECRET=<Proper JWT Token is here>
###< symfony/mercure-bundle ###

在美居容器中,我得到:

mercure_1   | {"level":"debug","ts":1619097073.714639,"logger":"http.stdlib","msg":"http: TLS handshake error from 172.21.0.5:57420: no certificate available for 'mercure'"}

我的 docker-compose 配置

services:

    api:
        build:
            context: ./api
            dockerfile: Dockerfile
        volumes:
            - ./api:/app
            - ./api/storage/logs/php-fpm:/var/log/php-fpm
        networks:
            - redio_network
        ports:
            - "7000:80"
        environment:
            <<: *common-variables

    mercure:
        image: dunglas/mercure
        environment:
            MERCURE_PUBLISHER_JWT_KEY: '!ChangeMe!'
            MERCURE_SUBSCRIBER_JWT_KEY: '!ChangeMe!'
        ports:
            - "9090:80"
        networks:
            - redio_network
        volumes:
            - ./_data/caddy_data:/data
            - ./_data/caddy_config:/config
        command: /usr/bin/caddy run -config /etc/caddy/Caddyfile.dev

这是我的控制器,当我发布到美居主题时:

class ChatController extends AbstractController
{
    /**
     * @Route("/push", name="push")
     * @param Request $request
     * @param HubInterface $publisher
     * @return Response
     */
    public function push(Request $request, HubInterface $publisher): Response
    {
        $publisher->publish(new Update(
            '/chat',
            json_encode(['message' => 'hello!'])
        ));

        return $this->json('Done');
    }
}
4

1 回答 1

2

好吧,我找到了。如果有人遇到同样的问题,请将 SERVER_NAME 变量添加到您的 docker-compose 配置中。Mercure 使用默认设置为强制 HTTPS 的 Caddy 服务器。

mercure:
    image: dunglas/mercure
    environment:
        MERCURE_PUBLISHER_JWT_KEY: '!ChangeMe!'
        MERCURE_SUBSCRIBER_JWT_KEY: '!ChangeMe!'
        SERVER_NAME: ":80"
于 2021-04-22T14:09:16.240 回答