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

Nginx部署Laravel应用时跨根目录检测/创建目录异常问题求助

Nginx部署Laravel应用时跨根目录检测/创建目录异常问题求助

各位大佬好!我遇到个头疼的问题:手里有个Laravel应用,用php artisan serve启动的时候完全正常,但换成Nginx做前端代理后,各种问题就来了,现在卡在一个跨目录检测的脚本上,怀疑是Nginx配置的锅,来请教大家!

先给大家看下服务器上的目录结构:

root@server:/var/www/html# tree -d -L 4
.
`-- app
    |-- C4
    |   `-- www
    |       |-- sites
    |       `-- site-assets
    `-- prod
        |-- app
        |-- bootstrap
        |-- config
        |-- database
        |-- public
        |-- resources
        |-- routes
        |-- storage
        `-- vendor

其中prod目录是Laravel应用的根目录,现在的问题是prod/public/js里有个脚本,需要检测C4/www/sites/目录是否存在(不存在的话就创建),但这个逻辑在Nginx部署后完全不工作。

下面是我当前用的Nginx配置:

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

    root /var/www/html/app/prod/public;
    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

补充下:我是运维工程师,不是这个应用的开发者,开发团队对Nginx完全没经验,所以只能自己摸索排查。

错误日志情况

我在/var/log/nginx/error.log里看到了这个错误:

2023/02/07 22:25:17 [error] 91535#91535: *23 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 192.168.x.x, server: , request: "GET /checksite/domain.com HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php8.1-fpm.sock:", host: "192.168.x.x"

后续更新

  • 更新1:我调整了Nginx的root为/var/www/html/app/prod/public,index设为index.php,现在主应用能正常打开了,但那个脚本又出了新问题:本该在C4/www/sites/下创建的目录,现在跑到Laravel应用的public目录里面去了,明显是路径识别错了。
  • 更新2:现在浏览器控制台还报了个jQuery错误:
Uncaught SyntaxError: Unexpected non-whitespace character after JSON at position 2

这个前端错误我打算明天找开发团队一起排查,但先想解决Nginx这边导致目录路径异常的问题,麻烦各位给支支招!

备注:内容来源于stack exchange,提问作者MasterOfNothing

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.23 14:38:12