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

7

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

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

我阅读了配置文档,但没有找到任何相关内容。所有的教程都只是针对每个路由/安全规则启用它。


你是如何在Web服务器层面上强制执行它的? - Emii Khaos
1
为什么要强制应用程序去做这件事,只需让nginx修改成https即可。 - Mohammad AbuShady
请发布您的安全配置。 - jovobe
4个回答

0

如何强制使用HTTPS或HTTP来访问不同的URL

安全组件提供了一种通过requires_channel设置来强制使用HTTPS的方法。这种替代方法更适合保护您网站的“区域”(例如/admin下的所有URL)或者当您想要保护第三方包中定义的URL时。

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

2
就像我说的那样,我不想为每个安全规则都这样做,而是针对整个网站。 - eReiche

0

看起来我们配置了错误的Web服务器。 供参考,这是现在正在工作的配置:

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

0

0

关于security.yaml文件

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接