首页 > 解决方案 > nginx 上的 Laravel 对除索引以外的所有路由都表示 404

问题描述

我已经使用 nginx、php 7.4 fpm 和 mysql 8 在 Docker 上设置了 Laravel。我是 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']

站点文件夹中的默认配置

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;
    }
}

conf.d 文件夹中的默认配置


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

索引页面工作得很好,但是当我添加一个新路由时,它显示来自 nginx 的 404。我对 docker 没有太多经验,我一直在遵循几个教程来设置它。

谢谢

标签: laraveldockernginx

解决方案


我找到了解决办法。我改变了根,它工作。

root /var/www/public

推荐阅读