0

I have an app that has a server (Laravel) and a client (nodeJS) running on a nginx server with docker-compose. I get the "File not found" error when I go to "localhost/api" page. My nginx.conf:

upstream backend { 
    server backend:9000; 
}

upstream frontend { 
    server frontend:3000; 
}

server { 
    listen 80;
    
    location / {
        proxy_pass http://frontend/;
        
        root /usr/share/nginx/html;
        # try_files $uri /index.html;
        try_files $uri $uri/ =404;
    
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location /api {
        proxy_pass http://backend/;
    
        root /var/www/public;
        index index.php index.html;
        try_files $uri $uri/ /index.php?$query_string;
    
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
        add_header 'Access-Control-Allow-Headers' 'Origin, Content-Type, Accept, Authorization';
    
        if ($request_method = 'OPTIONS') {
            return 204;
        }
    }

    location ~ \.php$ {
        fastcgi_pass backend;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
} 

and my docker-compose.yml:

version: "3.8"

services:
    frontend:
        build:
            context: ./frontend
            dockerfile: Dockerfile
        container_name: frontend
        restart: unless-stopped
        ports:
            - "3000:80"
        volumes:
            - ./frontend:/usr/share/nginx/html
        networks:
            - app-network

    # Laravel Backend Service
    backend:
        build:
            context: ./backend
            dockerfile: Dockerfile
        container_name: backend
        working_dir: /var/www
        restart: unless-stopped
        environment:
            - APP_ENV=local
            - APP_DEBUG=true
            - DB_CONNECTION=pgsql
            - DB_HOST=db
            - DB_PORT=5432
            - DB_DATABASE=postgres
            - DB_USERNAME=postgres
            - DB_PASSWORD=postgres
        ports:
            - "9000:80"
        tty: true
        volumes:
            - ./backend:/var/www
        depends_on:
            - db
        networks:
            - app-network

    # Nginx Service
    webserver:
        image: nginx:alpine
        container_name: webserver
        restart: unless-stopped
        tty: true
        ports:
            - "80:80"
        volumes:
            - ./backend:/var/www
            - ./frontend:/usr/share/nginx/html
            - ./source/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
        depends_on:
            - frontend
            - backend
        networks:
            - app-network

    # Postgres DB
    db:
        image: postgres:14.2
        container_name: db
        environment:
            - POSTGRES_USER=postgres
            - POSTGRES_PASSWORD=postgres
            - POSTGRES_DB=postgres
        volumes:
            - ./source/postgres/postgresdata:/var/lib/postgresql/data
        ports:
            - "5432:5432"
        networks:
            - app-network

networks:
    app-network:
        driver: bridge

volumes:
    postgres-data:
        driver: local

My Dockerfile for backend:

# backend/Dockerfile

FROM php:8.1-fpm

# Install dependencies
USER root
RUN apt-get update && apt-get install -y \
    git \
    zip \
    curl \
    sudo \
    unzip \
    libzip-dev \
    libicu-dev \
    libbz2-dev \
    libpng-dev \
    libjpeg-dev \
    libmcrypt-dev \
    libreadline-dev \
    libfreetype6-dev \
    libpq-dev \
    g++ && docker-php-ext-install pdo pdo_pgsql

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

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

RUN groupadd -g 1000 www \
    && useradd -u 1000 -ms /bin/bash -g www www

COPY . /var/www
COPY --chown=www:www . /var/www

RUN chown -R www:www /var/www

USER www

WORKDIR /var/www

COPY php.ini /usr/local/etc/php/conf.d/

RUN composer install --no-interaction --optimize-autoloader --no-dev

EXPOSE 9000
CMD ["php-fpm"]

Is there a problem with the way I copy files inside Dockerfile? Or how I set the root page in nginx.conf?

0