Laravel Apache 到 Nginx HTTPS 重定向

Laravel Apache to Nginx HTTPS redirect

我正在将一个 Laravel 4.2 站点从 Apache 迁移到 Nginx,并且在将资产 URL 更改为 HTTPS 时遇到问题。

这是我如何链接到 blade 中的 css 文件的示例:

{{ HTML::style('css/core.css') }}

在我的旧 Apache 设置中,这在输出中呈现为 HTTPS:

<link href="https://my.site.com/css/core.css" rel="stylesheet" type="text/css" media="all">

然而,在我的新 nginx 设置中,页面重定向到 HTTPS,但所有内部链接(表单、CSS、JS 等)都显示为 HTTP,这导致负载 "mixed content" 错误。

<link href="http://my.site.com/css/core.css" rel="stylesheet" type="text/css" media="all">

这是我的 nginx.conf:

server {
        listen 80;
        server_name my.site.com;
        return 301 https://$server_name$request_uri;
}

server {
        listen 443 ssl;
        ssl on;
        ssl_certificate /etc/ssl/my.site.com.chained.crt;
        ssl_certificate_key /etc/ssl/my.site.com.key;
        ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
        ssl_prefer_server_ciphers on;
        ssl_ciphers "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA RC4 !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS !RC4";

        server_name my.site.com;
        root /var/www/public;
        index index.php index.html index.htm;
        access_log /var/log/access.log;
        error_log /var/log/_error.log;

        try_files $uri $uri/ @rewrite;
        location @rewrite {
                rewrite ^/(.*)$ /index.php?_url=/;
        }
        location ~ \.php {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index /index.php;
                include /etc/nginx/fastcgi_params;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_param PATH_INFO $fastcgi_path_info;
                fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }

        location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
                root /var/www/public;
        }

        location ~ /\.ht {
                deny all;
        }
}

我想我可以强制 Laravel 生成安全 URL,在 HTML::style 方法上设置 $secure 属性,但是为此更改整个应用程序将是一项艰巨的工作。

Laravel 显然 "know" 如何在 Apache 而不是 nginx 中呈现 HTTPS 链接,是否有一种简单的方法可以在全球范围内强制使用它?

How does Laravel apparently "know" to render HTTPS links in Apache, but not nginx, and is there a simple way to force it globally?

Apache 将 $_SERVER['HTTPS'] 设置为 on,由 Symfony\Component\HttpFoundation\Request::isSecure() 选择。

在您的 nginx 配置中,fastcgi_param HTTPS on; 会做同样的事情。