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

如何在Azure部署Laravel8+PHP8无公开端点App Service并移除/public路径

解决Azure App Service(Laravel 8 + PHP 8)URL /public段无法移除问题

常规方案无效的核心原因是Azure App Service默认站点根目录绑定为site\wwwroot,未适配Laravel的入口目录结构,可按以下优先级选择方案解决:


方案1:修改站点根目录映射(最推荐,无多余重定向开销)

  • 进入Azure门户对应App Service管理面板,打开左侧配置菜单
  • 切换到路径映射标签页
  • 找到虚拟路径为/的条目,将默认物理路径site\wwwroot修改为site\wwwroot\public
  • 保存配置后重启App Service即可,无需额外修改重定向规则,保留public目录下Laravel自带的.htaccess或web.config即可正常运行

方案2:根目录添加重写规则(适用于无法修改根目录映射的场景)

针对Windows托管的App Service

在项目根目录新建web.config文件,写入以下配置:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Laravel Public Rewrite" stopProcessing="true">
                    <match url="^(.*)$" ignoreCase="false" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="public/{R:1}" appendQueryString="true" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

针对Linux + Apache托管的App Service

在项目根目录新建.htaccess文件,写入以下配置:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^/public/
    RewriteRule ^(.*)$ /public/$1 [L,QSA]
</IfModule>

针对Linux + Nginx托管的App Service

Nginx默认不支持.htaccess规则,需要自定义配置:

  • 在项目根目录新建nginx.conf文件,写入以下配置:
server {
    listen 8080;
    root /home/site/wwwroot/public;
    index index.php index.html index.htm;
    server_name _;

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

    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php8.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }
}
  • 进入App Service配置 -> 应用程序设置,新增配置项:键为NGINX_CONF_PATH,值为/home/site/wwwroot/nginx.conf,保存后重启服务生效。

注意事项

  • 配置修改后清除浏览器缓存再测试,避免永久重定向缓存干扰结果
  • 不要删除public目录下Laravel自带的重定向配置文件

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 06:54:00