docker 403 forbidden php nginx

  docker, nginx, php

docker-compose.yml

version: "3"
networks:
  laravel:

services:
  nginx:
    image: nginx:stable-alpine
    container_name: nginx
    ports:
      - "8088:80"
    volumes: 
      - ./src:/usr/share/nginx/html
      - ./site.conf:/etc/nginx/conf.d/site.conf
    depends_on:
      - php
      - mysql

    networks:
    - laravel

  mysql:
    image: mysql:5.7.35
    container_name: mysql
    restart: unless-stopped
    tty: true
    ports: 
      - "4306:3306"
    volumes:
      - ./mysql:/var/lib/mysql
    environment:
      MYSQL_DATABASE: boilerplatelaravel
      MYSQL_USER: root
      MYSQL_ROOT_PASSWORD: Tobi12345678

    networks:
      - laravel


  php:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: php
    volumes:
      - ./src/public:/var/www/html
    ports:
      - "9000:9000"
    networks:
      - laravel

site.conf

server {
    listen 80;
    index index.php;
    server_name localhost;
    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/html/public;

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

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

My file tree

testdocker/
┣ mysql/
┣ nginx/
┣ src/
┃ ┗ index.php
┣ docker-compose.yml
┣ Dockerfile
┗ site.conf

When i run a index.html it works fine but when i run a index.php with phpinfo() in it is doesn’t work.

2021/09/23 09:02:10 [error] 32#32: *2 directory index of "/usr/share/nginx/html/" is forbidden, client: 172.26.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost:8088"

This is the error i get when i look at the logs.

Source: Ask PHP

LEAVE A COMMENT