无法访问 symfony 4 public 个文件
symfony 4 public files not accesible
部署到测试服务器时,我无法访问 public 文件夹中的任何内容。我收到 Symfony 路由错误。好像它不认识 public 文件夹。所有页面路由正确。我的开发环境是下面的c:\inetpub\wwroot\userdir\symfony-test
这转化为 url。 https://servername/userdir/symfony-test/test
尝试拉出下面的测试图像或 css 或 twig 中的 js 文件我收到 404 错误
<img src="{{ asset('build/images/test.jpg') }}" alt="test">
看到它没有获得完整路径,我更改了 framwork.yaml 以设置 base_urls,它提供了完整路径,我设置了 json manifest_path。虽然清单路径是正确的,而且我可以看到它给出了使用 base_urls 的完整路径,但对于 public 文件夹中的任何内容,我仍然会得到 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 文件,按照此处的 link 说明在 "extra" 下添加 public-dir。 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 上的 Symfony 4 Windows Server 2012 R2。
Php 7.2.13,
作曲家版本 1.6.5,
节点版本 8.11.4,
纱线 1.9.4
如何让 public 文件夹被识别而不被视为路由或收到 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>
部署到测试服务器时,我无法访问 public 文件夹中的任何内容。我收到 Symfony 路由错误。好像它不认识 public 文件夹。所有页面路由正确。我的开发环境是下面的c:\inetpub\wwroot\userdir\symfony-test 这转化为 url。 https://servername/userdir/symfony-test/test
尝试拉出下面的测试图像或 css 或 twig 中的 js 文件我收到 404 错误
<img src="{{ asset('build/images/test.jpg') }}" alt="test">
看到它没有获得完整路径,我更改了 framwork.yaml 以设置 base_urls,它提供了完整路径,我设置了 json manifest_path。虽然清单路径是正确的,而且我可以看到它给出了使用 base_urls 的完整路径,但对于 public 文件夹中的任何内容,我仍然会得到 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 文件,按照此处的 link 说明在 "extra" 下添加 public-dir。 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 上的 Symfony 4 Windows Server 2012 R2。
Php 7.2.13,
作曲家版本 1.6.5,
节点版本 8.11.4,
纱线 1.9.4
如何让 public 文件夹被识别而不被视为路由或收到 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>