2013-03-01 30 views
2

因爲目前只有Chrome和Opera支持WebP,所以我想知道我是否可以定位這兩個特定的瀏覽器並將它們重定向到獲取我的網站的另一個版本,這樣我可以更快地優化我的網站下載速度?如果使用Chrome,請使用WebP

謝謝。

+0

偉大的問題,只是不正確的論壇(查看常見問題解答) – KevinDTimm 2013-03-01 19:26:34

+0

檢查用戶代理$ _SERVER,然後重定向到適當的頁面。 – Sturm 2013-03-01 19:27:25

+0

https://developers.google.com/speed/webp/在webP的正確設置上有很好的文檔。 – Mooseman 2013-03-01 19:27:59

回答

1

我解決了這個問題是這樣的:

  • 檢查客戶端發佈「圖像/ WEBP」在接受頭
  • 如果WebP的支持,檢查本地WebP的文件在磁盤上,並且 服務於它
  • 如果服務器配置爲代理,追加一個「WebP的:真正的」頁眉和 着後端
  • 追加「各不相同:接受」如果WebP的資產供應

在Nginx的:

location/{ 
    if ($http_accept ~* "webp") { set $webp "true"; } 
    # Use $webp variable to add correct image. 
} 

在我的情況,我用thumbor軟件將圖像轉換。 https://github.com/globocom/thumbor

pip install thumbor 

我的conf:

upstream thumbor { 
    server 127.0.0.1:9990; 
    server 127.0.0.1:9991; 
    server 127.0.0.1:9992; 
    server 127.0.0.1:9993; 
    server 127.0.0.1:9994; 
} 
location/{ 
    if ($http_accept ~* "webp") { 
     set $webp "T"; 
    } 
    if ($uri ~* "(jpg|jpeg)$") { 
     set $webp "${webp}T"; 
    } 
    proxy_cache_key $host$request_uri$webp; 

    if ($webp = "TT") { 
     rewrite ^(.*)$ "/unsafe/smart/filters:format(webp)/exemple.com$uri" break; 
     proxy_pass http://thumbor; 
     add_header Content-Disposition "inline; filename=image.webp"; 
    } 
    if ($webp != "TT") { 
     proxy_pass http://exemple.com; 
    } 
}