php - Symfony + Mercure 连接问题。使用 HTTPS 而不是 HTTP

标签 php docker symfony docker-compose mercure

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

我的应用程序运行良好,docker-compose 构建也没有任何问题。问题是当我尝试测试它并尝试从 Symfony 应用程序向 Mercure 发布某些内容时,我遇到了 500 内部服务器错误“无法发送更新”。消息。

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

Symfony\Component\HttpClient\Exception\TransportException: SSL connect error for "https://mercure/.well-known/mercure". at vendor/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 容器中我得到:

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

这是我的 Controller ,当我发布到 Mercure 主题时:

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');
    }
}

最佳答案

好的,我找到了。如果有人遇到同样的问题,请将 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"

关于php - Symfony + Mercure 连接问题。使用 HTTPS 而不是 HTTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67214389/

相关文章:

php - jQuery 在前面添加一个自动获取 php 文件数据库

docker - 如何为每个 Jenkins 图像运行添加内部版本号?

docker - Flink 1.7.0仪表板不显示任务统计信息

docker - 使用 docker-compose 连接到 RabbitMQ 容器

php - 多个微服务 symfony 应用程序共享供应商文件夹

symfony - FOSUserBundle - Redis 中的用户实体

php - 使用 JOIN 和关联 ID 从两个表中选择和显示日期

php - 从数组中删除仅包含数字的值 - PHP

javascript - 如何将 PHP 脚本与 JavaScript 和 Bootstrap 链接

Symfony Forms - 如何更改 CollectionTypes 项目标签