Home > Net >  RabbitMQ messages being published twice with PHP AMQP Lib and Docker
RabbitMQ messages being published twice with PHP AMQP Lib and Docker

Time:10-21

I've setup a local instance of RabbitMQ using Docker Compose and am going through the hello world example on the RabbitMQ docs. Here is a simple attempt at connecting, creating a queue and pushing a message to it:

<?php

require_once __DIR__ . '/vendor/autoload.php';

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

$rabbitMqHost = 'rabbitmq';

if (php_sapi_name() === 'cli') {
    $rabbitMqHost = '0.0.0.0';
}

$connection = new AMQPStreamConnection($rabbitMqHost, 5672, 'guest', 'guest');

$channel = $connection->channel();

$channel->queue_declare('hello', false, false, false, false);

$message = new AMQPMessage('Hello, world!' . microtime(true));

$channel->basic_publish($message, '', 'hello');

echo '[x] Sent \'Hello, world!\'';

$channel->close();
$connection->close();

exit;

This connects fine and creates the queue if it doesn't exist and when this script is ran in the command line, 1 message is added to the queue as expected. However, if I access this script in my browser, 2 new items are added to the queue.

Payload: Hello, world!1666282109.6845
Payload: Hello, world!1666282109.8559

I now feel like this issue is further upstream and may be related more to my Nginx setup, however I'm not sure what is causing this and the page doesn't seem to perform any redirects.

Here is the contents of my docker-compose.yaml file:

version: "3.9"

services:
  nginx:
    build:
      context: ./nginx
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ${PROJECTDIR}:/var/www/html
      - ./nginx/conf.d:/etc/nginx/conf.d
  php:
    build:
      context: ./php-fpm
    volumes:
      - ${PROJECTDIR}:/var/www/html
  database:
    image: mysql:5.7
    ports:
      - "3306:3306"
    restart: on-failure
    environment:
      MYSQL_DATABASE: dev
      MYSQL_USER: admin
      MYSQL_PASSWORD: secret
      MYSQL_ROOT_PASSWORD: docker
  rabbitmq:
    image: rabbitmq:3-management-alpine
    ports:
      - "5672:5672"
      - "15672:15672"
    volumes:
      - ./rabbitmq/data/:/var/lib/rabbitmq/
      - ./rabbitmq/log/:/var/log/rabbitmq

And my Dockerfiles for php-fpm and nginx respectively:

FROM php:8.1-fpm

RUN mkdir -p /var/www/html

RUN docker-php-ext-install pdo pdo_mysql sockets
FROM nginx:1.23-alpine

CMD ["nginx", "-g", "daemon off;"]

EXPOSE 80 443

Finally, my nginx/conf.d/default.conf:

server {
    listen 80;
    server_name _;
    root /var/www/html;
    index index.php index.html;
    charset utf-8;

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

    location ~ \.php$ {
        try_files $uri = 404;
        fastcgi_split_path_info ^(. `.php)(/. )$;
        fastcgi_pass php:9000;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    error_log /var/log/nginx/error.log;
}

I'd be really grateful for any insights as to why this is always putting two messages into the queue when accessed via a browser?

For reference, the environment config can also be seen in the GitHub repo on the rabbitmq branch.

CodePudding user response:

In browser, check "Developer Tools", "Network". You'll find your browser is making two queries, probably "favicon" and the index page.

As favicon does not exist, you have that redirecting to index.php - that will trigger the second rabbit message you are seeing.

Solution: add a favicon, redirect to another "missing file handler" or check the query string. Or rename the test file as "test.php" and remove the rabbit code from index.php.

  • Related