Symfony:生产时出现 404 错误

Symfony: 404 error on production

这是一个在开发环境中完美运行的页面示例,returns在生产环境中出现 404 错误

Not Found

The requested URL /app/reporter/ was not found on this server. Apache/2.4.7 (Ubuntu) Server at symfony.dev Port 80

不要被 /app/ 路由迷惑了,这个是真实的路由,与 app.php

无关

运行 php app/console router:debug --env=prod 确认路由器没有问题:

[router] Current routes
Name                                               Method Scheme Host   Path      
reporter                                           ANY    ANY    ANY  /app/reporter/      

当然,在发布此消息之前,我:

无论如何,我猜错误更多来自 Apache,所以这是我的配置文件:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName symfony.dev
    SetEnv SYMFONY__TENANT__ID "123"

    DocumentRoot /var/www/html/Symfony/web

#               <Directory />
#                       Options FollowSymLinks
#                       AllowOverride None
#               </Directory>

            <Directory /var/www/html/Symfony/web >
                    Options Indexes FollowSymLinks MultiViews
                    AllowOverride All
                    Order allow,deny
                    allow from all
            </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
            <Directory "/usr/lib/cgi-bin">
                    AllowOverride None
                    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                    Order allow,deny
                    Allow from all
            </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error-symfony.log

    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access-symfony.log combined

    Alias /doc/ "/usr/share/doc/"

            <Directory "/usr/share/doc/">
                    Options Indexes MultiViews FollowSymLinks
                    AllowOverride None
                    Order deny,allow
                    Deny from all
                    Allow from 127.0.0.0/255.0.0.0 ::1/128
            </Directory>

</VirtualHost>

/var/www/html/中,只有一个符号链接:

lrwxrwxrwx 1 root root    29 mai   13  2014 Symfony -> /home/me/path/to/symfony

而且我没有更改默认的 symfony /web/.htaccess 中的任何内容: 为了便于阅读,没有注释

DirectoryIndex app.php

<IfModule mod_rewrite.c>
RewriteEngine On


RewriteCond %{REQUEST_URI}:: ^(/.+)/(.*)::$
RewriteRule ^(.*) - [E=BASE:%1]


RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]


RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/ [R=301,L]


RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]


RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>

<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
    
    RedirectMatch 302 ^/$ /app.php/
   
</IfModule>
</IfModule>

Apache 错误日志确认错误来自那里:

[negotiation:error] [pid 1583] [client 127.0.0.1:44590] AH00687: Negotiation: discovered file(s) matching request: /var/www/html/Symfony/web/app (None could be negotiated).

由于我对 Apache 的理解非常有限,我 google 错误并找到了这个主题: http://www.bennadel.com/blog/2218-negotiation-discovered-file-s-matching-request-none-could-be-negotiated.htm

所以我最终简单地删除了我的 vhost conf 的 MultiViews 选项并解决了它。

正如上面给出的 link 提到的那样,

This goes to show you how bad it is to simply enable settings when you are not sure what they do.