bodi87
bodi87

Reputation: 551

Laravel on nginx says 404 for all routes except index

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

Upvotes: 2

Views: 2631

Answers (2)

bodi87
bodi87

Reputation: 551

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

root /var/www/public

Upvotes: 1

leachim742
leachim742

Reputation: 341

Yout nginx config is false. Nginx need to proxy everything to the php-fpm container.

you need to set your fastcgi_pass like this:

fastcgi_pass php-fpm:9000;

Upvotes: 0

Related Questions