You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

本地Nginx根路径正常,非根路由返回404错误排查求助

Nginx配置导致Laravel路由404的问题修复

问题分析

你的配置存在两个关键错误,直接导致非根路径的Laravel路由无法被正常处理:

  • location /的规则未转发请求到单入口文件:当前try_files $uri $uri/ =404会在请求的文件/目录不存在时直接返回404,没有把请求交给Laravel的核心入口index.php——而Laravel所有路由都依赖这个文件解析。
  • PHP处理规则仅匹配.php后缀:location ~ \.php$只处理带.php的请求,像/test这类无后缀的路由请求根本不会进入这个处理块,自然无法被Laravel解析。

修复后的完整Nginx配置

替换原server块中的对应部分即可:

server {
    listen 80;
    listen [::]:80 ;

    root /var/www/userauthapi.local/public;

    index index.php index.html index.htm index.nginx-debian.html;

    server_name userauthapi.local www.userauthapi.local;

    # 修改try_files规则,将非真实文件请求转发到index.php
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root/index.php;
    }

    location ~ /\.ht {
        deny all;
    }
}

验证步骤

  1. 保存修改后的配置文件
  2. 执行nginx -t检查配置语法是否合法
  3. 重启Nginx服务:systemctl restart nginx
  4. 访问http://userauthapi.local/test验证路由是否正常返回内容

内容的提问来源于stack exchange,提问作者cookie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 05:50:08