php - symfony 4 公共文件无法访问
问题描述
部署到测试服务器时,我无法访问公用文件夹中的任何内容。我收到 Symfony 路由错误。好像它不识别公用文件夹一样。所有页面都正确路由。我的开发环境如下 c:\inetpub\wwroot\userdir\symfony-test 转换为这个 url。https://servername/userdir/symfony-test/test
尝试提取下面的测试图像或 twig 中的 css 或 js 文件,我收到 404 错误
<img src="{{ asset('build/images/test.jpg') }}" alt="test">
看到它没有获得完整路径,我更改了 framwork.yaml 以设置提供完整路径的 base_urls,并设置了 json manifest_path。虽然清单路径是正确的,并且我可以看到它提供了使用 base_urls 的完整路径,但对于公用文件夹中的任何内容,我仍然会得到 404。
assets:
json_manifest_path: '%kernel.project_dir%/public/build/manifest.json'
base_urls:
- 'https://servername/userdir/symfony-test/public/'
执行上述操作导致
GET https://servername/userdir/symfony-test/build/images/test.93d146a7.jpg 404 (Not Found)
我还更新了 composer.json 文件,如下所示,按照此处的链接说明在“extra”下添加公共目录。https://symfony.com/doc/current/configuration/override_dir_structure.html
"extra": {
"symfony": {
"allow-contrib": false,
"require": "4.2.*",
"public-dir": "userdir/symfony-test/public/"
}
文件使用 encore webpack 编译并按预期工作。
Encore
// directory where compiled assets will be stored
.setOutputPath('public/build/')
.enableVersioning()
// public path used by the web server to access the output path
.setPublicPath('/build')
.addEntry('app', './assets/js/app.js')
.addEntry('test','./assets/images/test.jpg')
这也可能是有用的信息。这是 /public/web.config 文件中的规则。
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" appendQueryString="true" />
</rule>
symfony-test/web.config 中的规则
<rewrite>
<rules>
<rule name="Symfony4" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<action type="Rewrite" url="public/" appendQueryString="true" />
</rule>
</rules>
</rewrite>
在 IIS Windows Server 2012 R2 上运行 Symfony 4。PHP 7.2.13,
Composer 版本 1.6.5,节点版本 8.11.4,yarn 1.9.4
如何让公用文件夹被识别而不被视为路由或接收 404。
解决方案
我发现有人在 github 上为 symfony 2 设置了 iis 环境。在考虑了目录结构之后,我将这些规则添加到了我的 web.config 文件中,现在它可以按预期工作了。希望这可以帮助其他任何人出于任何原因想要在 iis 上使用 symfony :) 。
<rules>
<rule name="TempRewriteToWeb" stopProcessing="false">
<match url="^(public/)?(.*)$" />
<action type="Rewrite" url="public/{R:2}" logRewrittenUrl="true" />
</rule>
<rule name="StaticFiles" stopProcessing="true">
<match url="^(.*)$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile"/>
</conditions>
<action type="Rewrite" url="{R:0}" logRewrittenUrl="true" />
</rule>
<rule name="RewriteRequestsToPublic" stopProcessing="true">
<match url="^(public/)(.*)$" />
<action type="Rewrite" url="public/index.php?{R:2}" logRewrittenUrl="true"/>
</rule>
</rules>
推荐阅读
- python - 有人可以给出这个函数应该做什么的示例解决方案吗?
- java - Java SSL 问题 - 无法解析的 CRLDistributionPoints 扩展
- python-3.x - TypeError:传递给“Pack”操作的“值”的列表中的张量具有不完全匹配的类型 [string, float32]。CSV 教程中的错误
- mongodb - mongodb比较两个数组并返回差异
- azure-sql-database - 满足特定条件时如何从 azure sql 发送电子邮件
- bash - 从 bash 脚本运行 Prolog 命令
- spring-boot - Thymeleaf 表单动作不要跳到
- android - 网络请求在生产应用程序中开始失败,WIFI 上没有有效的 NAT64 前缀错误,但适用于移动数据,仅限 Android
- if-statement - 折叠带有赋值的 if-else 语句
- python - 计算 Z 分数