0

サブドメインが 6 文字未満の場合は 404 - を返すようにします。

たとえば、abcd.example.com404 を返す必要がありますがstackoverflow.example.com、index.html を返すには

私は次のことを試しました -

location ~ ^/[a-z0-9-_]{0,2}$
  return 404;
}

これは私にエラーを与えます -unknown directive "0,2}$"

これは可能ですか?

前もって感謝します

4

1 に答える 1

1

コードにいくつかの構文エラーを見つけることができます。

  1. Nginx は中かっこ{ }を使用して内部ディレクティブを指定するため、それを使用するときは{0,2}それをディレクティブとして読み取ろうとします。これを避けるには二重引用符が必要です。

  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の詳細については、ドキュメントを参照してください。

于 2013-06-11T09:28:48.083 に答える