symfony 3强制https时重定向太多

Bla*_*ack 18 php symfony

我有一个类似于SO上发布的其他问题的问题,但这些解决方案都没有奏效.

我正在使用内置于OSX El Capitan Server中的Apache,并且当我不通过以下指令强制 http流量进入https 时,https工作正常:

    access_control:
    - { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https, host: mypc\.local$ }
Run Code Online (Sandbox Code Playgroud)

但是,当访问我的网站的本地uri时,添加此结果会导致Too Many Redirects错误: https://mypc.local/myproject/web/

security.yml:

security:
  access_control:
    - { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https, host: mypc\.local$ }

  providers:
    our_db_provider:
        entity:
            class: AppBundle:Users
            property: username

  encoders:
    AppBundle\Entity\Users: plaintext   

firewalls:
    # disable authentication for assets and the profiler 
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false

    main:
        pattern:    ^/
        http_basic: ~
        provider: our_db_provider

        anonymous: ~
        form_login:
            login_path: /
            check_path: login

        logout:
            path:   /logout
            target: /
            invalidate_session: true 
Run Code Online (Sandbox Code Playgroud)

编辑:这是响应标题:

> GET /myproject/web/ HTTP/1.1
> Host: mypc.local
> User-Agent: curl/7.43.0
> Accept: */*
> 
< HTTP/1.1 301 Moved Permanently
< Date: Tue, 09 Aug 2016 12:15:00 GMT
< Server: Apache
< X-Powered-By: PHP/5.5.31
< Cache-Control: no-cache
< Location: https://mypc.local/myproject/web/
< MS-Author-Via: DAV
< Content-Length: 396
< Content-Type: text/html; charset=UTF-8
< 
* Ignoring the response-body
* Connection #0 to host mypc.local left intact
* Issue another request to this URL: 'https://mypc.local/myproject/web/'
* Found bundle for host mypc.local: 0x7f89b2d01780
* Re-using existing connection! (#0) with host mypc.local
* Connected to mypc.local (fe80::ea06:88ff:fecf:61c6) port 443 (#0)
> GET /myproject/web/ HTTP/1.1
.... repeated 20 times
Run Code Online (Sandbox Code Playgroud)

小智 3

我在使用 AWS ELB 和 Beanstalk 的 Symfony 时遇到了同样的问题。所有由 UrlGenerator 生成的 url 都带有 http 方案。强制 https 会让我的 Symfony 感到困惑并运行无限重定向循环。

这与 trust_proxies 变量有关。我认为 symfony 正在做一个无限循环,因为对他来说,即使你使用 https,你的方案也是 http。

您是否使用清漆代理、负载均衡器?

对我来说,使用totas 的这个答案解决了这个问题:

Request::setTrustedProxies(array($request->server->get('REMOTE_ADDR')));

我被迫这样做是因为AWS ELB具有动态IP。如果您的代理或负载均衡器有固定 IP,您可以使用 truted_proxies var,如symfony 文档中所述所述。

如果有人在 AWS ELB 环境中有更好的解决方案,我很感兴趣。

我希望这能帮到您。