Home > Net >  Laravel docker-compose 404 not found Nginx
Laravel docker-compose 404 not found Nginx

Time:05-07

I'm facing a strange behavior when trying to run my Laravel app using docker-compose. After starting the containers, if I try to visit my website URL I get the following error:

404 Not Found from Nginx.

Here is the docker-compose.yml:

version: '3'
services:

  #Laravel App
  app:
    build:
      context: .         # path to the docker file
      dockerfile: Dockerfile    # name of the dockerfile
    # image: apperitivo_app       # name to assign to the image
    container_name: app         # name to assign to the container
    restart: unless-stopped     # always restart container except when stopped (manually or otherwise)
    tty: true                   # once started keeps container running (attaching a terminal to it, like using -t docker directive)
    environment:                # set environment variables
      SERVICE_NAME: app
      SERVICE_TAGS: dev
    working_dir: /var/www/html  # working directory of the running container
    volumes:
      - ./src/:/var/www/html    # map application directory in the /var/www/html directory of the container
    networks:
      - mynet                   # networks at which container is connected

  #Nginx Service
  nginx:
    image: nginx:alpine
    container_name: nginx
    restart: unless-stopped
    tty: true
    ports:                      # exposed ports 
      - "80:80"                 # http
      - "443:443"               # https
    volumes:
      - ./src/:/var/www/html
      - ./nginx/conf.d/:/etc/nginx/conf.d/    # nginx configuration
      - ./data/certbot/conf:/etc/letsencrypt  # letsencrypt certificates
      - ./data/certbot/www:/var/www/certbot   # certbot service
    networks:
      - mynet
    command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"

  # Certbot service to manage ssl certificates
  certbot:
    image: certbot/certbot
    volumes:
      - ./data/certbot/conf:/etc/letsencrypt
      - ./data/certbot/www:/var/www/certbot
    entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
    networks:
      - mynet

  #MySQL Service
  db:
    image: mysql/mysql-server #mysql:5.7
    container_name: db
    restart: unless-stopped
    tty: true
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: laraveldb
      MYSQL_USER: laraveluser
      MYSQL_PASSWORD: laravel_db_password
      MYSQL_ROOT_PASSWORD: mysql_root_password
    volumes:
      - mysqldata:/var/lib/mysql/
    networks:
      - mynet

#Docker Networks
networks:
  mynet:
    driver: bridge
#Volumes
volumes:
  mysqldata:
    driver: local

and this is the nginx config file:

server {
    listen 80;
    index index.php index.html;
    error_log  /var/log/nginx/error.log debug;
    access_log /var/log/nginx/access.log;
    root /var/www/public;
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(. \.php)(/. )$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
    location / {
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }
}

And finally this is the .env file of Laravel:

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:Vs71ZhnWfc9tvoSBTCn26CYCuP/lm2vI7AO7IOf0v5E=
APP_DEBUG=true
APP_URL=http://www.apperitivo.it

LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laraveldb
DB_USERNAME=laraveluser
DB_PASSWORD=laravel_db_password

BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

MEMCACHED_HOST=127.0.0.1

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="[email protected]"
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

Any suggestion?

CodePudding user response:

In your nginx you set root with root /var/www/public;.
In your docker-compose.yml you mount your source to /var/www/html.
Make sure they are the same.

  • Related