Symfony2:如何强制整个应用程序的HTTPS?

时间:2013-09-16 11:49:18

标签: symfony https nginx

是否可以强制https用于整个应用程序,而无需为所有100条路由/防火墙规则定义它?

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

我阅读了配置文档,但没有找到任何相关内容。所有食谱条目也仅用于按路线/安全规则启用。

4 个答案:

答案 0 :(得分:0)

How to force HTTPS or HTTP for Different URLs

  

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

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

答案 1 :(得分:0)

我们似乎错误配置了我们的网络服务器。作为参考,这是现在正在运行的配置:

    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;
            }
    }

答案 2 :(得分:0)

关于security.yaml

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

答案 3 :(得分:0)

强制整个应用仅使用https://github.com/nelmio/NelmioSecurityBundle处理ssl请求

nelmio_security:
    forced_ssl: ~

默认情况下,使路由器生成https网址:

parameters:
    router.request_context.scheme: 'https'