我正在创建一个 API,但在使用 symfony 7 向 Mercure Hub 发送更新时遇到问题。我已遵循 Symfony 当前文档并使用 docker 实现 Mercure(仅限 Mercure)。在我的本地,一切正常,我发送更新,但在生产中,它无法发送更新。该错误返回给我“无法发送更新。”和“HTTP/2 404 返回“https://example.com/.well-known/mercure”。”该路由是 env 变量中的 MERCURE_URL 设置。我正在使用 ec2 AWS Linux 机器,并且不使用任何域名或 SSL 证书,我仅使用 Linux 机器的 IP 进行连接。我也使用 Apache 服务器。错误图像:这是我的文件,如 symfony 当前文档中所示:compose.override.yaml
version: '3'
services:
###> symfony/mercure-bundle ###
mercure:
ports:
- "80"
###< symfony/mercure-bundle ###
docker-compose.yaml
services:
###> symfony/mercure-bundle ###
mercure:
image: dunglas/mercure
restart: unless-stopped
environment:
SERVER_NAME: ':80'
MERCURE_PUBLISHER_JWT_KEY: '!ChangeThisMercureHubJWTSecretKey!'
MERCURE_SUBSCRIBER_JWT_KEY: '!ChangeThisMercureHubJWTSecretKey!'
# Set the URL of your Symfony project (without trailing slash!) as value of the cors_origins directive
MERCURE_EXTRA_DIRECTIVES: |
cors_origins http://127.0.0.1:8000
# Comment the following line to disable the development mode
command: /usr/bin/caddy run --config /etc/caddy/Caddyfile.dev
volumes:
- mercure_data:/data
- mercure_config:/config
###< symfony/mercure-bundle ###
volumes:
###> symfony/mercure-bundle ###
mercure_data:
mercure_config:
###< symfony/mercure-bundle ###
.env
###> 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=https://example.com/.well-known/mercure
# The public URL of the Mercure hub, used by the browser to connect
MERCURE_PUBLIC_URL=https://example.com/.well-known/mercure
# The secret used to sign the JWTs
MERCURE_JWT_SECRET="!ChangeThisMercureHubJWTSecretKey!"
###< symfony/mercure-bundle ###
config/packages/mercure.yaml
mercure:
hubs:
default:
url: '%env(MERCURE_URL)%'
public_url: '%env(MERCURE_PUBLIC_URL)%'
jwt:
secret: '%env(MERCURE_JWT_SECRET)%'
publish: '*'
我的 Apache 配置 /etc/apache2/sites-available/taxis-chrono.conf
<VirtualHost *:80>
ServerName localhost
# Uncomment the following line to force Apache to pass the Authorization
# header to PHP: required for "basic_auth" under PHP-FPM and FastCGI
#
# SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1
# <FilesMatch \.php$>
# when using PHP-FPM as a unix socket
# SetHandler proxy:unix:/var/run/php/php7.4-fpm.sock|fcgi://dummy
# when PHP-FPM is configured to use TCP
# SetHandler proxy:fcgi://127.0.0.1:9000
# </FilesMatch>
DocumentRoot /var/www/taxis-chrono/public
<Directory /var/www/taxis-chrono/public>
AllowOverride None
Require all granted
FallbackResource /index.php
</Directory>
# uncomment the following lines if you install assets as symlinks
# or run into problems when compiling LESS/Sass/CoffeeScript assets
# <Directory /var/www/project>
# Options FollowSymlinks
# </Directory>
ErrorLog /var/log/apache2/taxis-chrono_error.log
CustomLog /var/log/apache2/taxis-chrono_access.log combined
</VirtualHost>
需要帮助请。
经过几天的搜索,我终于找到了解决方案。问题出在我的 Mercure docker 身上。它正在启动,但无法与 symfony 应用程序通信。我所做的是在我的 compose.override.yaml 中设置端口,如下所示:
services:
###> symfony/mercure-bundle ###
mercure:
ports:
- "3000:80"
###< symfony/mercure-bundle ###
在我的 .env 中,我更改了 MERCURE_URL,如下所示:
# 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://127.0.0.1:3000/.well-known/mercure
# The public URL of the Mercure hub, used by the browser to connect
MERCURE_PUBLIC_URL=http://127.0.0.1:3000/.well-known/mercure
# The secret used to sign the JWTs
MERCURE_JWT_SECRET="!ChangeThisMercureHubJWTSecretKey!"
###< symfony/mercure-bundle ###
现在一切工作正常,我的 symfony 应用程序和 docker 中的 Mercure 可以高效通信。我希望这个答案能帮助其他人。