【问题标题】:Laravel Apache to Nginx HTTPS redirectLaravel Apache 到 Nginx HTTPS 重定向
【发布时间】:2015-02-28 17:12:26
【问题描述】:

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

这是我如何链接到刀片中的 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,这会导致加载“混合内容”错误。

<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=/$1;
        }
        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 在 HTML::style 方法上设置 $secure 属性来生成安全 URL,但是为此更改整个应用程序将是一项艰巨的工作。

Laravel 是如何“知道”在 Apache 中呈现 HTTPS 链接的,而不是在 nginx 中呈现 HTTPS 链接,有没有一种简单的方法可以在全局范围内强制它?

【问题讨论】:

    标签: laravel nginx


    【解决方案1】:

    Laravel 是如何“知道”在 Apache 中渲染 HTTPS 链接的,而不是在 nginx 中,有没有一种简单的方法可以在全局范围内强制它?

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

    在你的 nginx 配置中,fastcgi_param HTTPS on; 也会这样做。

    【讨论】:

    • 非常感谢 fastcgi_param HTTPS 开启;这就是我要找的东西
    猜你喜欢
    • 1970-01-01
    • 2011-08-18
    • 2020-09-11
    • 2017-09-28
    • 2020-03-21
    • 2016-03-10
    • 2015-08-16
    • 2015-02-14
    • 2019-06-04
    相关资源
    最近更新 更多