リチャードのコードを完全なソリューションに拡張する私のソリューションを共有します。
URLに大文字が含まれている場合、キャッシュ機構に入る前にURLを正規化するのではなく、正しいURLにユーザーをリダイレクトします。これにより、検索エンジンが大文字と小文字の区別されたURLを小文字と区別して索引付けできなくなります
# Define a function that converts a string to lower-case in-place.
# http://stackoverflow.com/questions/6857445
C{
#include <ctype.h>
static void strtolower(char *c) {
for (; *c; c++) {
if (isupper(*c)) {
*c = tolower(*c);
}
}
}
}C
sub vcl_recv {
if (req.http.host ~ "[A-Z]" || req.url ~ "[A-Z]") {
# Convert host and path to lowercase in-place.
C{
strtolower(VRT_GetHdr(sp, HDR_REQ, "\005host:"));
strtolower((char *)VRT_r_req_url(sp));
}C
# Use req.http.location as a scratch register; any header will do.
set req.http.location = "http://" req.http.host req.url;
error 999 req.http.location;
}
# Fall-through to default
}
sub vcl_error {
# Check for redirects - redirects are performed using: error 999 "http://target-url/"
# Thus we piggyback the redirect target in the error response variable.
if (obj.status == 999) {
set obj.http.location = obj.response;
set obj.status = 301;
set obj.response = "Moved permanently";
return(deliver);
}
# Fall-through to default
}
-小文字にreq.url
を変換する際const char *
からchar *
に醜いキャストがあります...基本的に、我々はニスがないために私達に言ってもかかわらず、その場で文字列を変更しています。それは動作するようです。 :-) CブロックからURLを設定し、回避クラッシュ使用する
これはVarnish 4.0でしか利用できないと考えましたが、v3.0(https://www.varnish-cache.org/docs/3.0/reference/vmod_std.html#tolower)にあるようです。私はそれについて知らせたいと思っています。 –
興味深いことに、Enterキーを押すとすぐに、Firefox 44とChrome 48の両方でホストリクエストヘッダーが小文字になります。ホスト上のtolower()がもう必要ないことを完全には確信していません。私はそれが傷ついていないと思う。 –
Varnish docsは、ホストを小文字にするブラウザを確認します。 "Varnish.org"と "varnish.org/"を持つ理論では、ハッシュする前にホスト名またはURLを小文字にしないことを指摘しておく価値がありますが、ブラウザによって異なるキャッシュエントリが発生します。ホスト名 " - https://www.varnish-cache.org/docs/trunk/users-guide/vcl-hashing.html –