Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dorqa95's avatar
Level 12

502 nginx response header error on mail sending through sail

Hi,

I use laravel sail for my project in localhost and for Illuminate\Support\Facades\Mail send in the browser I get 502 Bad Gateway. The nginx container says: [error] 38#38: *2 upstream prematurely closed connection while reading response header from upstream, client: 172.18.0.1, server: localhost, request: "POST /events/176/questions HTTP/1.1". The laravel log is empty, and the nginx log says no more. After this POST request the laravel container restarts.

I tried to read after this error in the internet and added nginx proxy configs for timeout but unfortunately it did not helped me. The dd() function before every Mail::to($user)->send(new <MailClass>()); shows up, but after it not, the laravel error handler did not runs, so basically I don't even know what is the exact problem. From notifications I got mails properly to my HELO app and tried also to change the MAIL_MAILER from smtp to log, but from the browser the nginx runs to the same problem. When I call the Mail facade from the tinker (in command line) with the same exact classes everything works like charm.

Could you please help me with this error. I use Laravel 10.28.0, sail 1.25.0 packages, and for nginx conf:

server {
    listen  80  default_server;
    server_name ${SERVER_NAME};

    return  301 https://$host:${SSL_PORT}$request_uri;
}

server {
    listen  443 ssl default_server;
    server_name ${SERVER_NAME};

    # Self signed certificates
    # Don't use them in a production server!
    ssl_certificate     /etc/nginx/certs/server.pem;
    ssl_certificate_key /etc/nginx/certs/server.key;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass	http://${APP_SERVICE};
    }
}

My docker-compose.yml file looks this way:

# For more information: https://laravel.com/docs/sail
version: '3'
services:
    nginx:
        image: 'nginx:latest'
        ports:
            - '${HTTP_PORT:-8000}:80'
            - '${SSL_PORT:-443}:443'
        environment:
            - SSL_PORT=${SSL_PORT:-443}
            - APP_SERVICE=${APP_SERVICE:-laravel.test}
            - SERVER_NAME=${SERVER_NAME:-localhost}
        volumes:
            - 'sail-nginx:/etc/nginx/certs'
            - './vendor/ryoluo/sail-ssl/nginx/templates:/etc/nginx/templates'
            - './vendor/ryoluo/sail-ssl/nginx/generate-ssl-cert.sh:/docker-entrypoint.d/99-generate-ssl-cert.sh'
        depends_on:
            - ${APP_SERVICE:-laravel.test}
        networks:
            - sail
    laravel.test:
        build:
            context: ./docker/8.2
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.2/app
        extra_hosts:
            - 'host.docker.internal:host-gateway'
        ports:
            - '${APP_PORT:-80}:80'
            - '${VITE_PORT:-5173}:${VITE_PORT:-5173}'
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
            XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
            XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
        volumes:
            - '.:/var/www/html'
        networks:
            - sail
        depends_on:
            - mysql
            - phpmyadmin
    mysql:
        image: 'mysql/mysql-server:8.0'
        ports:
            - '${FORWARD_DB_PORT:-3306}:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ROOT_HOST: "%"
            MYSQL_DATABASE: '${DB_DATABASE}'
            MYSQL_USER: '${DB_USERNAME}'
            MYSQL_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 1
        volumes:
            - 'sail-mysql:/var/lib/mysql'
            - './vendor/laravel/sail/database/mysql/create-testing-database.sh:/docker-entrypoint-initdb.d/10-create-testing-database.sh'
        networks:
            - sail
        healthcheck:
            test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
            retries: 3
            timeout: 5s
    phpmyadmin:
        image: 'phpmyadmin:latest'
        ports:
            - 8080:80
        networks:
            - sail
        environment:
            - PMA_ARBITRARY=1
            - UPLOAD_LIMIT=1000000000
networks:
    sail:
        driver: bridge
volumes:
    sail-nginx:
        driver: local
    sail-mysql:
        driver: local

0 likes
0 replies

Please or to participate in this conversation.