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

Laravel项目仅根目录指向public可用的Nginx多项目配置问题

Nginx多项目部署Laravel的配置解决方案

问题根源分析

你当前的子路径配置存在三个核心错误:

  1. location /project_name/public的root配置重复拼接路径,导致实际文件路径错误
  2. try_files未指定子路径前缀,请求被转发到根目录的index.php而非项目自身的入口文件
  3. PHP处理规则未针对子路径做适配,导致SCRIPT_FILENAME指向无效文件

方案一:子路径部署(适合单域名多项目场景)

以下是修正后的Nginx配置,假设你的项目路径为C:/nginx/html/project_folder,访问路径为localhost/project1:

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;
        root         C:/nginx/html;

        # 默认根路径(保留Nginx默认页面或其他项目)
        location / {
            index  index.php index.html index.htm;
            try_files $uri $uri/ /index.php$is_args$args;
        }

        # Laravel项目1:访问路径 localhost/project1
        location /project1 {
            # 使用alias替代root,避免路径重复拼接
            alias C:/nginx/html/project_folder/public;
            index index.php index.html index.htm;

            # 转发不存在的请求到项目自身的index.php
            try_files $uri $uri/ /project1/index.php$is_args$args;

            # 处理该子路径下的PHP文件
            location ~ \.php$ {
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                # 使用$request_filename获取alias解析后的正确路径
                fastcgi_param  SCRIPT_FILENAME  $request_filename;
                include        fastcgi_params;
            }
        }

        # 如需部署第二个项目,复制上述location块修改路径即可
        # location /project2 {
        #     alias C:/nginx/html/another_project/public;
        #     ...
        # }

        # 处理根路径下的PHP文件(可选)
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}

配套Laravel配置修改

  1. 打开项目根目录的.env文件,修改APP_URL为:
    APP_URL=http://localhost/project1
    
  2. 运行命令清除配置缓存:
    php artisan config:cache
    

方案二:虚拟主机部署(更推荐,无路由冲突)

通过子域名区分项目,配置更清晰,避免子路径的路由兼容问题:

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    # 项目1:访问地址 project1.localhost
    server {
        listen       80;
        server_name  project1.localhost;
        root         C:/nginx/html/project_folder/public;

        location / {
            index  index.php index.html index.htm;
            try_files $uri $uri/ /index.php$is_args$args;
        }

        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

    # 项目2:访问地址 project2.localhost
    server {
        listen       80;
        server_name  project2.localhost;
        root         C:/nginx/html/another_project/public;

        location / {
            index  index.php index.html index.htm;
            try_files $uri $uri/ /index.php$is_args$args;
        }

        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

    # 默认主机:访问localhost
    server {
        listen       80 default_server;
        server_name  localhost;
        root         C:/nginx/html;

        location / {
            index  index.php index.html index.htm;
            try_files $uri $uri/ /index.php$is_args$args;
        }

        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}

配套系统配置

修改Windows的hosts文件(路径:C:\Windows\System32\drivers\etc\hosts),添加:

127.0.0.1 project1.localhost
127.0.0.1 project2.localhost

验证步骤

  1. 保存配置后,运行nginx -s reload重新加载Nginx
  2. 访问对应路径/域名,检查主页、静态资源、路由是否正常
  3. 若仍有问题,确认PHP-FPM服务正常运行,且项目目录有读写权限

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 20:02:46