Ad

Laravel On Nginx Says 404 For All Routes Except Index

- 1 answer

I have setup Laravel on Docker with nginx, php 7.4 fpm and mysql 8. I am new to Docker and I have been following several tutorials and somehow managed to run each of the service in a separate container and the code in a different directory.

Only index page works but not any other route.

docker-compose.yml

version: '3'

services:
  nginx:
    build:
      context: ./nginx
    volumes:
      - ../laravelproject:/var/www
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/sites/:/etc/nginx/sites-available
      - ./nginx/conf.d/:/etc/nginx/conf.d
    depends_on:
      - php-fpm
    ports:
      - "80:80"
      - "443:443"

  php-fpm:
    build:
      context: ./php-fpm
    volumes:
      - ../laravelproject:/var/www
      - ../laravelproject/serve_config/custom.ini:/usr/local/etc/php/conf.d/custom.ini

  database:
    build:
        context: ./database
    environment:
      - MYSQL_DATABASE=mydb
      - MYSQL_USER=myuser
      - MYSQL_PASSWORD=secret
      - MYSQL_ROOT_PASSWORD=docker
    volumes:
      - ./database/data.sql:/docker-entrypoint-initdb.d/data.sql
    command: ['--default-authentication-plugin=mysql_native_password']

default-conf in sites folder

server {

    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    server_name localhost;
    root /var/www;
    index index.php index.html index.htm;

    location / {
         try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass php-fpm:9000;
        fastcgi_index index.php;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #fixes timeouts
        fastcgi_read_timeout 600;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }

    location /.well-known/acme-challenge/ {
        root /var/www/letsencrypt/;
        log_not_found off;
    }
}

default-conf in conf.d folder


upstream php-upstream {
    server php-fpm:9000;
}

The index page is working just fine, but when i add a new route, it says 404 from nginx. I have not much experience with docker and I have been following several tutorials to set it up.

Thanks

Ad

Answer

I found a fix. I changed the root and it worked.

root /var/www/public
Ad
source: stackoverflow.com
Ad