簡體   English   中英

Heroku 上的 Symfony:403 Forbidden 您無權訪問 / 在此服務器上

[英]Symfony on Heroku: 403 Forbidden You don't have permission to access / on this server

我已成功將 Symfony 2 應用程序部署到 Heroku,但現在,當我嘗試訪問它時,收到以下 403 錯誤:

禁止的

您無權訪問 / 在此服務器上。

這是 Heroku 的日志:

2015-07-29T14:31:41.827491+00:00 heroku[router]: at=info method=GET path="/" host=my-app.herokuapp.com request_id=557a70f4-ea11-4519-b8df-301b714f6ffa fwd="151.77.103.253" dyno=web.1 connect=0ms service=1ms status=403 bytes=387
2015-07-29T14:31:41.828428+00:00 app[web.1]: [Wed Jul 29 14:31:41.827438 2015] [autoindex:error] [pid 104:tid 140466989270784] [client 10.100.0.139:16096] AH01276: Cannot serve directory /app/: No matching DirectoryIndex (index.php,index.html,index.htm) found, and server-generated directory index forbidden by Options directive
2015-07-29T14:31:41.829009+00:00 app[web.1]: 10.100.0.139 - - [29/Jul/2015:14:31:41 +0000] "GET / HTTP/1.1" 403 209 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36

似乎 Symfony(或 Heroku?)正在嘗試為目錄/app/提供服務,但我認為這是不正確的,從日志來看:

2015-07-29T14:31:41.828428+00:00 app[web.1]: [7 月 29 日星期三 14:31:41.827438 2015] [autoindex:error] [pid 104:tid 140466984.20707.84.190707.84.160716 ] AH01276:無法提供目錄/app/ :找不到匹配的目錄索引(index.php、index.html、index.htm),並且選項指令禁止服務器生成的目錄索引

按照有關如何部署到 Heroku 的 Symfony 文檔教程,我創建了我的.procfile並放入了:

web: bin/heroku-php-apache2 web/

我還刪除了DemoBundle ,現在我的根 URL 以這種方式在DefaultController中配置:

<?php

// \AppBundle\Controller\DefaultController.php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller
{
    /**
     * @Route("/", name="Homepage")
     */
    public function indexAction()
    {
        return $this->render('default/index.html.twig');
    }
}

我認為,最后,我的.htaccess存在一些問題,即 Symfony 標准版附帶的問題:

# 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 app.php

<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 app.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}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]

    # Sets the HTTP_AUTHORIZATION header removed by apache
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect to URI without front controller to prevent duplicate content
    # (with and without `/app.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 ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]

    # If the requested filename exists, simply serve it.
    # We only want to let Apache serve files and not directories.
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]

    # Rewrite all other queries to the front controller.
    RewriteRule .? %{ENV:BASE}/app.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 302 ^/$ /app.php/
        # RedirectTemp cannot be used instead
    </IfModule>
</IfModule>

我的應用程序的另一部分可能是導致此問題的原因: security.yml ,目前是這樣的:

# you can read more about security in the related section of the documentation
# http://symfony.com/doc/current/book/security.html
security:
# http://symfony.com/doc/current/book/security.html#encoding-the-user-s-password
    encoders:
        FOS\UserBundle\Model\UserInterface: sha512

# http://symfony.com/doc/current/cookbook/security/acl.html#bootstrapping
acl:
    connection: default

# http://symfony.com/doc/current/book/security.html#hierarchical-roles
role_hierarchy:
    ROLE_ADMIN:       ROLE_USER
    # ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
    ROLE_SUPER_ADMIN: ROLE_ADMIN

# http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
providers:
    fos_userbundle:
        id: fos_user.user_provider.username_email

# the main part of the security, where you can set up firewalls
# for specific sections of your app
firewalls:
    main:
        pattern: ^/
        form_login:
            provider: fos_userbundle
            csrf_provider: security.csrf.token_manager
        logout:    true
        anonymous: true

    # disables authentication for assets and the profiler, adapt it according to your needs
    dev:
        pattern:  ^/(_(profiler|wdt)|css|images|js)/
        security: false

# with these settings you can restrict or allow access for different parts
# of your application based on roles, ip, host or methods
# http://symfony.com/doc/current/cookbook/security/access_control.html
access_control:
    #- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }
    - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }

但是,訪問http:// my-app.herokuapp.com/login (這似乎是“向世界開放”),無論如何我都會收到一個漂亮的 404 錯誤:

未找到

在此服務器上找不到請求的 URL /login。

那么,這可能是問題所在? 哪個設置阻止我在 Heroku 上訪問我的 Symfony 應用程序?

這不可能。 3個多小時才找到解決辦法。 在我在這里發布的所有代碼中都找不到的解決方案。

一個真正、簡單、愚蠢的小解決方案: procfile名稱。 你注意到了嗎? 我都是用小寫字母寫的。

解決方案? Procfile ,第一個字母大寫。

真是太糟糕了,但我終於讓我的應用程序啟動並運行了! :D

最簡單的方法是鍵入不帶引號的行。

例如

web: vendor/bin/heroku-php-apache2 public/

如果有人仍然遇到此問題,您可以嘗試用 Procfile 中的單引號替換雙引號

例子

"web: vendor/bin/heroku-php-apache2 public/"

應該

'web: vendor/bin/heroku-php-apache2 public/'

部署時檢查此通知:注意:沒有 Procfile,使用 'web: heroku-php-apache2'。 對我來說,在我直接在 Heroku 雲設置中編寫 web: heroku-php-apache2 public/ 之后沒有任何區別。 然后我找到了這個解決方案並且它有效。

如果您在將代碼部署到 Heroku 時不在本地 master 分支上,最終會出現錯誤。 您需要在本地 master 分支上推送到 Heroku master。 這是默認設置。

換句話說,新的 docroot 只能從 Procfile 設置,部署應該從本地master分支推送。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM