强制https时,symfony 3 Too Many重定向太多

时间:2016-08-06 02:57:48

标签: php symfony

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

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

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

但是,在访问我网站的本地uri时,添加此项会导致“太多重定向”错误: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 

编辑: 这是响应标题:

> 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

2 个答案:

答案 0 :(得分:3)

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

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

你是一个清漆代理,负载均衡器吗?

对我来说,使用来自totas的this answer解决了这个问题:

false

我被迫这样做是因为AWS ELB具有动态IP。如果您的代理或负载均衡器具有修复IP,则可以使用truted_proxies var,如in symfony documentation所述。

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

我希望这会对你有所帮助。

答案 1 :(得分:1)

简单来说,Symfony配置不应该是您重定向流量的地方,原因有两个:

  1. Mantainability
  2. 开销
  3. 如果你有mod重写启用,我想你应该这样,你可以在Apache中配置这些设置:

    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{SERVER_NAME}/%$1 [R,L]
    
相关问题