Symfony2开发环境正常,生产环境出现404错误。

24
我最近在我的计算机上成功安装了Symfony2。
我可以访问 http:/localhost/app_dev.php (开发环境)。
然而,当我尝试访问生产环境时: http:/localhost/app.php 我在浏览器中收到以下错误消息:
"Oops! An Error Occurred",服务器返回了 "404 Not Found"。某些地方出了问题。请发送电子邮件至[email]告诉我们您遇到此错误时正在做什么。我们将尽快修复。对于任何造成的不便,我们深感抱歉。
我已检查过显而易见的:文件app.php与app_dev.php位于同一文件夹中 - 所以我不知道是什么原因导致了这个问题。
有人有解决方法吗?
[[编辑]]
我已通过输入以下内容清除缓存:sudo php app/console cache:clear env=prod no-debug,建议这样做。现在我得到了一个空白屏幕。令人担忧的是,没有错误消息记录在app/logs/prod.log中,所以我对出了什么问题毫无头绪(生产环境仍然正常工作)。
我的app/config/routing.yml文件的内容:
### fos routing, remove later
fos_user_security:
    resource: "@FOSUserBundle/Resources/config/routing/security.xml"

fos_user_profile:
    resource: "@FOSUserBundle/Resources/config/routing/profile.xml"
    prefix: /profile

fos_user_register:
    resource: "@FOSUserBundle/Resources/config/routing/registration.xml"
    prefix: /register

fos_user_resetting:
    resource: "@FOSUserBundle/Resources/config/routing/resetting.xml"
    prefix: /resetting

fos_user_change_password:
    resource: "@FOSUserBundle/Resources/config/routing/change_password.xml"
    prefix: /profile

###


# Internal routing configuration to handle ESI
#_internal:
#   resource: "@FrameworkBundle/Resources/config/routing/internal.xml"
#   prefix:   /_internal

这是我的app/config/routing_dev.yml文件

_welcome:
    pattern:  /
    defaults: { _controller: AcmeDemoBundle:Welcome:index }

_demo_secured:
    resource: "@AcmeDemoBundle/Controller/SecuredController.php"
    type:     annotation

_demo:
    resource: "@AcmeDemoBundle/Controller/DemoController.php"
    type:     annotation
    prefix:   /demo

_assetic:
    resource: .
    type:     assetic

_wdt:
    resource: "@WebProfilerBundle/Resources/config/routing/wdt.xml"
    prefix:   /_wdt

_profiler:
    resource: "@WebProfilerBundle/Resources/config/routing/profiler.xml"
    prefix:   /_profiler

_configurator:
    resource: "@SensioDistributionBundle/Resources/config/routing/webconfigurator.xml"
    prefix:   /_configurator

_main:
    resource: routing.yml
我刚刚注意到我没有一个routing_prod.yml文件。
(警钟鸣响)Symfony2没有提供生产环境的路由配置文件吗?
下面是我的Apache配置文件内容:
NameVirtualHost *:80

<VirtualHost *:80>
    DocumentRoot /path/to/symfony/web
    ServerName localhost

    # Custom log file
    Loglevel warn
    ErrorLog  /path/localhost.error.log
    CustomLog /path/localhost.access.log combined

    <Directory /path/to/symfony/web>
        AllowOverride None
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f 
        RewriteRule ^(.*)$ app.php [QSA,L]
    </Directory>
</VirtualHost>

[[更多细节]]

app/logs/prod.log的内容

[2012-08-10 18:10:38] security.INFO: 使用匿名Token填充SecurityContext [] [] [2012-08-10 18:10:38] request.ERROR: Symfony\Component\HttpKernel\Exception\NotFoundHttpException: 找不到路由 "GET /" (未捕获异常) 在 /path/to/symfony/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/EventListener/RouterListener.php 的第83行 [] []


你能贴上你的app/config/routing.yml文件吗? - Vitalii Zurian
还有 routing_dev.ymlrouting_prod.yml。我认为您应该检查 Web 服务器的 access.logerror.log。另外,请附上您的 Web 服务器虚拟主机配置。 - Vitalii Zurian
如果没有 routing_prod.yml 也没关系。这意味着它会选择 routing.yml - Vitalii Zurian
4
你可以尝试手动删除缓存目录 (rm -rf app/cache/*),然后运行 app/console router:debug 命令来查看已注册的路由信息。 - kgilden
我也遇到了同样的问题,现在我和你一样只看到了一个空白页面,没有任何错误提示。你找到解决方法了吗? - DomingoSL
我通过创建一个routing_prod.yml文件,并在其中添加_main: resource: routing.yml的内容来解决了我的问题。 - Michał Tatarynowicz
10个回答

41

您是否已经启用了生产环境并清除了缓存? 运行控制台并执行以下操作:

app/console --env=prod cache:clear

如果您使用symfony生成项目,可以尝试使用bin/console而不是app/console - Luke
建议直接手动删除缓存目录: (在Symfony项目目录中)$ rm -rf app/cache - Benjamin

12

我认为我遇到了类似的问题。我相信预热缓存可以解决我的情况。

php app/console cache:warmup --env=prod --no-debug

2
+1 这帮助我解决了一个非常烦人的开发环境下,生产环境白屏的问题。 - Peter Wooster

4

最近我安装了Symfony 2.2,也遇到了这个问题,但实际上这是正常行为。这意味着,Symfony 2.x(截至本文写作时)默认情况下不会为生产环境提供任何路由/内容。

在您的情况下,看起来您安装了Symfony bundle,它在您的生产路线(routing.yml)中设置了一些路由,但乍一看,似乎没有一个路由针对您的生产环境根目录,即 http:/localhost/app.php/,所以404可能是预期的。虽然它正在导入路由,因此细节被隐藏了,所以我不能完全确定。检查路由的好方法是阅读Visualizing & Debugging Routes,您可以了解有关app/console router:debug CLI命令的信息。

routing.yml 是生产环境中路由的默认位置(意味着 routing_prod.yml 不存在)。你会注意到 routing_dev.yml 导入了 routing.yml。这意味着你在生产环境中放置的所有内容(开箱即用)都可以在开发环境中访问。你看到的演示内容是专属于开发环境的,所以你在生产环境中看不到它。你可以随意移动一些东西以适应你的需求,但通常建议开发环境导入生产环境而不是反过来。

每当你想要在生产环境中测试更改时,你需要像 @tolgap 建议的那样清除缓存。生产环境严重依赖预编译缓存,因此该命令将强制刷新生产环境缓存。开发环境始终在刷新其缓存。理解这一点对于使用 Symfony 至关重要。虽然 Symfony.org 上的教程是一个很好的入门点,但它并没有完全阐明有关缓存和工作流程的副标题之间的关系。我在意识到 routing.ymlrouting_dev.yml 以及开发环境的软缓存与生产环境的硬缓存之间的关系之前确实感到困惑了一会儿。


3

我尝试了@gilden在他的评论中提供的解决方案,它有效。

我运行了php bin/console cache:clear --env=prod而不是rm -rf app/cache/*


2

今天我遇到了同样的问题,并尝试了各种解决方案。直到我想起来启用mod_rewrite apache服务很重要。要做到这一点,请运行以下命令(在Ubuntu上)

sudo a2enmod rewrite

然后重新启动服务器

sudo service apache2 restart

我希望有人能提供帮助。祝好!


当我使用docker php7.0-apache镜像时,发生了这种情况!谢谢! - martintama

2
我很惊讶没有人告诉你: 在生产环境中,你没有 / 的路由。
请在 app/config/routing.yml 中编写此内容。
  _welcome:
 pattern:  /
 defaults: { _controller: AcmeDemoBundle:Welcome:index }

2

您需要在本地服务器上安装APC,这样就可以正常运行生产模式。还要尝试在parameters.yml中选择正确的数据库,因为dev会添加DATABASENAME_dev。


1

我的symfony缓存文件夹不可写。当我将缓存完整目录及其文件设置为可写时,它才能正常工作。

sudo chmod -R 777 .

0
当出现这个问题时,只需在您的公共目录中添加一个 .htaccess 文件。以下是一个示例:

# Use the front controller as index file. It serves as a fallback solution when
# every other rewrite/redirect fails (e.g. in an aliased environment without
# mod_rewrite). Additionally, this reduces the matching process for the
# start page (path "/") because otherwise Apache will apply the rewriting rules
# to each configured DirectoryIndex file (e.g. index.php, index.html, index.pl).
DirectoryIndex index.php

# By default, Apache does not evaluate symbolic links if you did not enable this
# feature in your server configuration. Uncomment the following line if you
# install assets as symlinks or if you experience problems related to symlinks
# when compiling LESS/Sass/CoffeScript assets.
# Options +FollowSymlinks

# Disabling MultiViews prevents unwanted negotiation, e.g. "/index" should not resolve
# to the front controller "/index.php" but be rewritten to "/index.php/index".
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Determine the RewriteBase automatically and set it as environment variable.
    # If you are using Apache aliases to do mass virtual hosting or installed the
    # project in a subdirectory, the base path will be prepended to allow proper
    # resolution of the index.php file and to redirect to the correct URI. It will
    # work in environments without path prefix as well, providing a safe, one-size
    # fits all solution. But as you do not need it in this case, you can comment
    # the following 2 lines to eliminate the overhead.
    RewriteCond %{REQUEST_URI}::$0 ^(/.+)/(.*)::\2$
    RewriteRule .* - [E=BASE:%1]

    # Sets the HTTP_AUTHORIZATION header removed by Apache
    RewriteCond %{HTTP:Authorization} .+
    RewriteRule ^ - [E=HTTP_AUTHORIZATION:%0]

    # Redirect to URI without front controller to prevent duplicate content
    # (with and without `/index.php`). Only do this redirect on the initial
    # rewrite by Apache and not on subsequent cycles. Otherwise we would get an
    # endless redirect loop (request -> rewrite to front controller ->
    # redirect -> request -> ...).
    # So in case you get a "too many redirects" error or you always get redirected
    # to the start page because your Apache does not expose the REDIRECT_STATUS
    # environment variable, you have 2 choices:
    # - disable this feature by commenting the following 2 lines or
    # - use Apache >= 2.3.9 and replace all L flags by END flags and remove the
    #   following RewriteCond (best solution)
    RewriteCond %{ENV:REDIRECT_STATUS} =""
    RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]

    # If the requested filename exists, simply serve it.
    # We only want to let Apache serve files and not directories.
    # Rewrite all other queries to the front controller.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ %{ENV:BASE}/index.php [L]
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        # When mod_rewrite is not available, we instruct a temporary redirect of
        # the start page to the front controller explicitly so that the website
        # and the generated links can still be used.
        RedirectMatch 307 ^/$ /index.php/
        # RedirectTemp cannot be used instead
    </IfModule>
</IfModule>


-2

(将以下内容添加到routing_dev.yml文件中)在routing_dev.yml中添加以下内容

_wellcome:
    pattern: /
    defaults: { _controller:AppBundle:Default:index }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接