Laravel asset URL ignoring https

梦想的初衷 提交于 2019-12-19 09:56:41

问题


I am using the following code in my template to load in my CSS file:

<link rel="stylesheet" href="{!! URL::asset('css/app.css') !!}">

If I view the page over https on my local machine then the link to the app.css file is also https, however on my live server this isn't happening. If you view the live site over https and view the source you can see that the link to the CSS file is still over normal http and so doesn't load. Does anyone know why this is happening?


回答1:


While your users are accessing via HTTPS, your CloudFlare configuration is passing those requests onto your server using HTTP. You have two options:

  1. Turn on CloudFlare's Full or Strict SSL. https://blog.cloudflare.com/origin-server-connection-security-with-universal-ssl/

  2. Set up your webserver to see the X-Forwarded-Proto header and tell PHP it's being accessed via HTTPS when that header's value is https. How to do this depends a bit on your webserver. In nginx, for example, I do this for Amazon's ELB (which sends the same headers):

map $http_x_forwarded_proto $is_https {
    default off;
    https on;
}

and then this in my FastCGI params file:

fastcgi_param  HTTPS              $is_https if_not_empty;

In Apache, I think you can do this if mod_setenvif is installed:

SetEnvIf x-forwarded-proto https HTTPS=on



回答2:


You may need to use secure_asset function instead.

<link rel="stylesheet" href="{!! secure_asset('css/app.css') !!}">


来源:https://stackoverflow.com/questions/34120976/laravel-asset-url-ignoring-https

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!