首页 > 解决方案 > Laravel 在使用 NginX 配置的所有路由上返回 404

问题描述

我一直在尝试解决这个问题一天。我检查了所有类似的问题并做了同样的事情,但这似乎不起作用。

我目前正在服务器上运行一个节点 JS 应用程序,现在我打算将 laravel 添加到它。但是所有路由都返回 laravel 404 错误页面。

这是配置文件。

server {

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

  root /var/www/html/dlabs.cloud-File-Service/public;

  index index.html index.htm index.php;


  server_name dlabs.com.ng;


  location /auth/ {

    proxy_pass http://localhost:3000;
    proxy_pass_header Server;
    proxy_redirect off;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Scheme $scheme;
    proxy_set_header REMOTE_ADDR $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }

  location /file-service/ {

    try_files $uri $uri/ /index.php?$query_string;

  }


  location / {

    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ =404;
  }


  location = /favicon.ico {

    access_log off; log_not_found off;
  }
  location = /robots.txt {

    access_log off; log_not_found off;
  }

  error_page 404 /index.php;

  location ~ \.php$ {

    fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
    include fastcgi_params;
  }


  location ~ /\.(?!well-known).* {

    deny all;
  }


  location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {

    expires 5d;
  }

  # deny access to . files, for security
  #
  location ~ /\. {

    log_not_found off;
    deny all;
  }
}

PHP 版本是 7.3 版。

/auth/ 完美运行。

标签: phplaravelnginx

解决方案


添加try_files $uri $uri/ /index.php$is_args$args; 到 nginx.conf

在 => /etc/nginx/nginx.conf


推荐阅读