Symfony 生产网站不强制使用 https(使用 nginx)

JTG*_*JTG 1 https nginx symfony

在我的app/config/security.yml文件中有:

access_control:
    - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }
    - { path: ^/admin, roles: ROLE_ADMIN, requires_channel: https }
Run Code Online (Sandbox Code Playgroud)

在开发中(使用内置服务器的 symfony),如果我访问localhost/login它,它会将我重定向到https://localhost/login.

但是,在我的生产站点中,example.com/login我只看到登录页面。浏览器指示连接不安全。

我的直觉认为这可能与 nginx 配置文件有关。

server {
        listen       80;
        listen       443 ssl;
        server_name  example.com;
        root /var/www/symfony/web;

        client_max_body_size 500M;

        location / {
             # try to serve file directly, fallback to app.php
             index app.php;
             try_files $uri @rewriteapp;
        }

        location @rewriteapp {
             rewrite ^(.*)$ /app.php/$1 last;
        }

        location ~ ^/app\.php(/|$) {
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_split_path_info ^(.+\.php)(/.*)$;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param HTTPS on;
        }
        error_log /var/log/nginx/adam_error.log;
        access_log /var/log/nginx/adam_access.log;
    }
Run Code Online (Sandbox Code Playgroud)

我或多或少是从这里复制的

其他一些需要注意的事情:如果我被 symfony 内部重定向到登录页面(即,如果我尝试访问/admin受限路由或在不安全连接上登录失败,我确实会被路由到安全https://example.com/login网址,但显然这不是不够好,我不希望任何人进入example.com/login并且没有安全连接)。

谁能发现可能出了什么问题吗?

JTG*_*JTG 5

好吧,我明白了。在我的 nginx 配置中:

fastcgi_param HTTPS on;
Run Code Online (Sandbox Code Playgroud)

导致了问题。事实证明,将其设置为“on”时,如果初始端口为 80(我猜 symfony 认为端口 80 在此配置中为 https),则不会强制重定向到 https,并将其设置为“off” ,它会导致无限重定向(nginx 将 url 重定向到端口 80,symfony 将 url 重定向到端口 443)。

因此,解决方案是,如果是端口 80,则将其关闭;如果是端口 443(ssl 端口),则将其打开。

这就是我的 nginx.conf 现在的样子:

server {
        listen       80;
        listen       443 ssl;
        server_name  example.com;
        root /path/to/symfony_directory/web;

        #if your version of nginx is < 1.11.11, uncomment these next two lines
        #as $https will not be defined.
        #if ($server_port = 443) { set $https on; }
        #if ($server_port = 80) { set $https off; }

        location / {
             # try to serve file directly, fallback to app.php                                        
             # try_files $uri /app.php$is_args$args;                                                  
             index app.php;
             try_files $uri @rewriteapp;
        }

        location @rewriteapp {
             rewrite ^(.*)$ /app.php/$1 last;
        }

        location ~ ^/app\.php(/|$) {
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_split_path_info ^(.+\.php)(/.*)$;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

            #NOTE THAT "$https" is defined by nginx to be 
            # "on" if port 443 and off for port 80 (for version > 1.1.11)
            fastcgi_param HTTPS $https; 

        }
    }
Run Code Online (Sandbox Code Playgroud)