サブドメインが 6 文字未満の場合は 404 - を返すようにします。
たとえば、abcd.example.com
404 を返す必要がありますがstackoverflow.example.com
、index.html を返すには
私は次のことを試しました -
location ~ ^/[a-z0-9-_]{0,2}$
return 404;
}
これは私にエラーを与えます -unknown directive "0,2}$"
これは可能ですか?
前もって感謝します
サブドメインが 6 文字未満の場合は 404 - を返すようにします。
たとえば、abcd.example.com
404 を返す必要がありますがstackoverflow.example.com
、index.html を返すには
私は次のことを試しました -
location ~ ^/[a-z0-9-_]{0,2}$
return 404;
}
これは私にエラーを与えます -unknown directive "0,2}$"
これは可能ですか?
前もって感謝します
コードにいくつかの構文エラーを見つけることができます。
Nginx は中かっこ{
}
を使用して内部ディレクティブを指定するため、それを使用するときは{0,2}
それをディレクティブとして読み取ろうとします。これを避けるには二重引用符が必要です。
その後、ステートメントのディレクティブを開く$
必要があります。{
location
ただし、最大の問題は、location
サブドメインに関連していないことです。探しているのはserver_name
上の段階にありlocation
ます。サーバー名の詳細については、ドキュメントを参照してください。
注: これはテストされていないコードです。
次のようなことを試してみます。
server {
listen 80;
# We require the expression in double quotes so the `{` and `}` aren't passed as directives.
# The `\w` matches an alphanumeric character and the `{7}` matches at least 7 occurrences
server_name "~^\w{7}\.example\.com";
location / {
# do_stuff...;
}
}
server {
listen 80;
# We require the expression in double quotes so the `{` and `}` aren't passed as directives.
# The `\w` matches an alphanumeric character and the `{1,6}` matches no more than 6 occurrences
server_name "~^\w{1,6}\.example\.com";
location / {
return 404;
}
}
私が言ったように、上記はテストされていませんが、先に進むための良い基礎を与えるはずです. PCRE
正規表現 nginx users とserver_namesの詳細については、ドキュメントを参照してください。