Mercure Hub with NGinx and docker compose

Hello, I have a problem with the implementation of mercure in my docker compose I can not make the service available for nginx from my Symfony application

My docker compose

version: '3.7'

services:
  web:
    image: nginx:alpine
    restart: unless-stopped
    ports:
      - "8000:80"
    volumes:
      - .:/var/www:delegated
      - ./site.conf:/etc/nginx/conf.d/default.conf
    links:
      - php
  php:
    restart: unless-stopped
    build: ./docker/php
    volumes:
      - .:/var/www:delegated
    depends_on:
      - db
  db:
    image: jbergstroem/mariadb-alpine:latest
    restart: unless-stopped
    ports:
      - "8888:3306"
    environment:
      MYSQL_ROOT_PASSWORD: mdsolutions
      MYSQL_DATABASE: mdsolutions
      MYSQL_USER: mdsolutions
      MYSQL_PASSWORD: mdsolutions
    volumes:
      - db-data:/var/lib/mysql
      - ./var:/var/www/var
  redis:
    restart: unless-stopped
    image: redis:6-alpine
  messenger:
    build: ./docker/php
    volumes:
        - .:/var/www:delegated
    depends_on:
        - db
    restart: unless-stopped
    command: [ 'php', 'bin/console', 'messenger:consume', 'async', '--limit=10', '-vvv' ]
    environment:
      MESSENGER_TRANSPORT_DSN: redis://redis:6379/messages
      MESSENGER_TRANSPORT_FAILED_DSN: redis://redis:6379/messages_failed

  #Dev Tools
  redis-gui:
    image: rediscommander/redis-commander
    environment:
      - REDIS_HOSTS=local:redis:6379
    ports:
      - "8081:8081"
  mercure:
    image: dunglas/mercure
    restart: unless-stopped
    ports:
      - "3000:3000"
    environment:
      SERVER_NAME: ':3000'
      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
    command: /usr/bin/caddy run --config /etc/caddy/Caddyfile.dev
    volumes:
      - mercure_data:/data
      - mercure_config:/config

volumes:
  db-data:
  mercure_data:
  mercure_config:

My site .conf nginx

server {
    listen 80;
    server_name localhost;
    root /var/www/public;
    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

     # Mercure
    location /.well-known/mercure/ {
        proxy_pass http://mercure:3000/.well-known/mercure/;
        proxy_read_timeout 24h;
        proxy_http_version 1.1;
        proxy_set_header Connection "";

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;

        access_log    off;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

.env.local

MERCURE_URL=http://localhost:3000/.well-known/mercure
MERCURE_PUBLIC_URL=http://localhost:3000/.well-known/mercure
MERCURE_JWT_SECRET="!ChangeThisMercureHubJWTSecretKey!"

My error on symfony :

Failed to connect to localhost port 3000: Connection refused for "http://localhost:3000/.well-known/mercure".

I am blocked for 3 days, I tried with previous versions for mercury, changed the network, tried to modify the proxy but nothing works.

Thanks a lot for your help

1 Like

Every container will have its own localhost and not the same as the localhost of the host operating system. Use the name of the compose services to access them from another container.

2 Likes

Thank you itā€™s work i change this for futur users

MERCURE_URL=http://mercure:3000/.well-known/mercure
MERCURE_PUBLIC_URL=http://xxx.localhost:3000/.well-known/mercure