Nginx container returns 502 Bad Gateway

240 Views Asked by At

I have the following problem, I hope you can help me.

I have 3 docker containers, one with php-fpm, nginx and mysql. When entering my website, the browser responds with a 502 Gateaway error code. I am not understanding where I have the error. By the way, it is worth clarifying, the error varies between this error code but sometimes it also returns a 403 Forbiden.

docker-compose.yml:

version: '3'

services:
   app:
     build:
       context: .
     ports:
       - "9000:9000"
     volumes:
       - ./:/var/www/html
     depends_on:
       -db
     environment:
       - TZ=America/Argentina/Buenos_Aires
     networks:
       - stephvaez

   nginx:
       image: nginx:latest
       ports:
         - "9001:80"
         - "9002:443"
       volumes:
         - ./:/var/www/html
         - ./nginx-conf:/etc/nginx/conf.d
         - ./certificates:/etc/nginx/ssl
       depends_on:
         - app
       networks:
         - stephvaez

   DB:
     image: mysql:5.7
     environment:
       MYSQL_ROOT_PASSWORD: stephvaez
       MYSQL_DATABASE: stephvaez
     volumes:
       - mysql-data:/var/lib/mysql
     networks:
         - stephvaez

volumes:
   mysql-data: {}

networks:
   stephvaez:

Nginx configuration file:

server {
    listen 80;
    listen [::]:80;
        
    server_name storemanager.local;
        
    return 301 https://$host$request_uri;
        
}
    
server {
    listen 443 ssl;
    listen [::]:443 ssl;
    
    ssl_certificate /etc/nginx/ssl/docker/server.pem;
    ssl_certificate_key /etc/nginx/ssl/docker/server.key;
    
    server_name storemanager.local;
    
    root /var/www/html/public;
    index index.html index.php;
    
    
    location / {
        try_files $uri $uri/ /index.php?$is_args$args;
    }
    
    location ~ \.php$ {
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    
    location ~ /\.ht {
        deny there;
        return 404;
    }
}

Dockerfile:

    # Use a base image with PHP-FPM and Nginx
FROM php:8.1-fpm

# Install extensions necessary for Laravel and other dependencies
RUN docker-php-ext-install pdo pdo_mysql

# Install Nginx
RUN apt-get update && apt-get install -y nginx

# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Copy your Nginx configuration
COPY ./nginx-conf/nginx.conf /etc/nginx/conf.d

# Copy certificates
COPY certificates/ /etc/nginx/ssl/

# Configure Nginx to work with PHP-FPM
RUN echo "upstream php-upstream { server app:9000; }" > /etc/nginx/conf.d/upstream.conf

# Start both Nginx and PHP-FPM
CMD ["nginx", "-g", "daemon off;"]

Thank you so much!!!

1

There are 1 best solutions below

0
On BEST ANSWER

Your Docker Compose stack is set to use the nginx service as a front-end which proxies PHP requests through to the app service (php-fpm).

The app service should only be running php-fpm, not NGINX. By overriding the image's CMD, you're no longer running php-fpm which is why NGINX couldn't proxy requests through to it.

Remove all the NGINX specific parts from your Dockerfile...

FROM php:8.1-fpm

# Install extensions necessary for Laravel and other dependencies
RUN docker-php-ext-install pdo pdo_mysql

# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- \
    --install-dir=/usr/local/bin --filename=composer

and rebuild your app service image

docker compose build app