【问题标题】:Laravel on nginx says 404 for all routes except indexnginx 上的 Laravel 对除索引以外的所有路由都表示 404
【发布时间】:2019-12-13 05:16:07
【问题描述】:

我已经在 Docker 上使用 nginx、php 7.4 fpm 和 mysql 8 设置了 Laravel。 我是 Docker 新手,我一直在学习几个教程,并设法在单独的容器中运行每个服务,并在不同的目录中运行代码。

只有索引页有效,其他路由无效。

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 没有太多经验,我一直在按照几个教程来设置它。

谢谢

【问题讨论】:

  • 试试 Laravel 推荐的配置,也许? laravel.com/docs/6.x/deployment#nginx
  • 谢谢,我修改了与建议相同的配置文件,但问题仍然相同。无法加载任何其他路线。是否有某种缓存,或查看路线是否已注册的方法?
  • 为什么要构建自己的图像而不使用即用型图像?
  • 我想学习并暂时单独设置它以尝试看看它是如何工作的。我知道有几个用php 7.4和nginx打包在一起的。

标签: laravel docker nginx


【解决方案1】:

我找到了解决办法。 我更改了根目录,它起作用了。

root /var/www/public

【讨论】:

  • 没有解决我的问题
【解决方案2】:

你的 nginx 配置是假的。 Nginx 需要将所有内容代理到 php-fpm 容器中。

你需要像这样设置你的 fastcgi_pass:

fastcgi_pass php-fpm:9000;

【讨论】:

    猜你喜欢
    • 2021-01-30
    • 2014-07-31
    • 2021-09-26
    • 2020-10-03
    • 2020-08-26
    • 2018-06-14
    • 1970-01-01
    • 2020-09-08
    • 2018-08-20
    相关资源
    最近更新 更多