【问题标题】:Symfony2: How to force HTTPS for the whole app?Symfony2:如何为整个应用程序强制使用 HTTPS?
【发布时间】:2013-09-20 13:24:30
【问题描述】:

是否可以在不为所有 100 条路由/防火墙规则定义的情况下为整个应用程序强制使用 https?

我们尝试在网络服务器级别强制使用 https,但 symfony2 仍然尝试重定向到 http 并生成一些奇怪的链接 (http://[...]:443)。

我阅读了配置文档,但没有找到任何相关信息。所有说明书条目也仅用于根据路由/安全规则启用它。

【问题讨论】:

  • 你是如何在网络服务器级别强制它的?
  • 为什么要强制app做,就让nginx改成https
  • 请发布您的安全配置。

标签: symfony https nginx


【解决方案1】:

强制整个应用只处理带有https://github.com/nelmio/NelmioSecurityBundle的ssl请求

nelmio_security:
    forced_ssl: ~

让路由器默认生成https url:

parameters:
    router.request_context.scheme: 'https'

【讨论】:

    【解决方案2】:

    关于 security.yaml

    access_control:
        - { path: ^/, requires_channel: https, host: ^www\.domain\.com$ }
    

    【讨论】:

      【解决方案3】:

      看来我们的网络服务器配置错误。作为参考,这里是现在工作的配置:

          server {
                  listen x.x.x.x:80;
                  server_name domain.tld;
                  listen      80;
      
                  location / {
                          rewrite     ^(.*)   https://domain.tld$1 permanent;
                  }
          }
      
          server {
                  gzip                on;
                  gzip_types          text/plain text/css application/x-javascript text/xml application/xml application/rss+xml text/javascript image/x-icon;
                  gzip_min_length     1000;
                  gzip_comp_level     6;
                  gzip_http_version   1.0;
                  gzip_vary           on;
                  gzip_proxied        expired no-cache no-store private auth;
                  gzip_disable        msie6;
      
                  listen x.x.x.x:443;
      
                  ssl         on;
                  ssl_certificate     /etc/nginx/wildcard_ssl/cert.pem;
                  ssl_certificate_key /etc/nginx/wildcard_ssl/cert.key;
      
                  server_name domain.tld;
      
                  root /var/www/domain.tld/current/web/;
      
                  access_log /var/log/nginx/domain.tld/access.log main;
                  error_log /var/log/nginx/domain.tld/error.log;
      
                  rewrite ^/app\.php/?(.*)$ /$1 permanent;
      
                  location / {
                          index app.php;
                          try_files $uri @rewriteapp;
                  }
                  location @rewriteapp {
                          rewrite ^(.*)$ /app.php/$1 last;
                  }
      
                  location @long_time {
                          fastcgi_pass   tldpass;
                          fastcgi_split_path_info ^(.+\.php)(/.*)$;
                          include fastcgi_params;
                          fastcgi_param  SCRIPT_FILENAME    $document_root/app.php;
                          fastcgi_param  HTTPS              on;
      
                          fastcgi_read_timeout 300;
                  }
                  location ~ ^/app\.php(/|$) {
                          include fastcgi_params;
                          fastcgi_pass   tldpass;
                          fastcgi_split_path_info ^(.+\.php)(/.*)$;
                          fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
                          fastcgi_param  HTTPS              on;
                          fastcgi_read_timeout 600s;
      
                          access_log /var/log/nginx/domain.tld/php-only.log;
                  }
      
                  location ~* ^.+\.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|wav|bmp|rtf|htc)$ {
                          expires     31d;
                          add_header  Cache-Control private;
                  }
          }
      

      【讨论】:

        【解决方案4】:

        How to force HTTPS or HTTP for Different URLs

        Security 组件提供了一种通过 requires_channel 设置。这种替代方法更适合 保护您网站的“区域”(/admin 下的所有 URL)或何时 您想保护在第三方包中定义的 URL。

        access_control:
            - path: ^/secure
              roles: ROLE_ADMIN
              requires_channel: https
        

        【讨论】:

        • 正如我所说的,我不想对每条安全规则都这样做,而是对整个网站都这样做。
        猜你喜欢
        • 2013-10-27
        • 1970-01-01
        • 2014-09-27
        • 1970-01-01
        • 1970-01-01
        • 2020-06-23
        • 2016-12-24
        • 2015-01-08
        • 2017-08-30
        相关资源
        最近更新 更多