2017-01-04 12 views
0

私は現在、haproxyを使用してシナリオを解決しようとしています。デフォルト HAproxy ACL。 Haproxyへのすべての接続をデフォルトでブロックし、特定のIPのみを許可する

  • によって

    • すべてブロックIP以下のようなシナリオは、任意の接続がwhilelist IPから来た場合、それは以上の10の同時接続を超えた場合
    • が、あれば拒否すべき特定のIPアドレスからの接続のみを許可します30秒

    私のサーバーへのAPI呼び出しの数を減らすには、これを実行します。誰も私にこれを手伝ってもらえますか?

    おかげ

  • +0

    あなたは、この時の試みを作り、あなたが動けなくなるところ私たちを表示する必要があります。 HAProxyの設定ファイルには、あるレベルの「プログラミング」が含まれている可能性がありますが、[Server Fault](http://serverfault.com)がHAProxyの質問に対するStack Overflowよりも適切な場所であると思われます。 –

    答えて

    2

    まず二つのことが簡単で、単に第三のみホワイトリストIP

    acl whitelist src 10.12.12.23 
    use_backend SOMESERVER if whitelist 
    

    許可 - スロットリング - stick-tablesを使用する必要が(多くのデータタイプがある - カウンタはconnの、SESのは、HTTP、レートカウンタとして速度...):

    #     max entries    count request in 60s periods 
    stick-table type ip size 200k expire 100s store http_req_rate(60s) 
    

    はあなたの隣には、tracking each requestなどにより、テーブルを埋めるために持っています。例えば

    # is there more than 5req/1min from IP 
    acl http_rate_abuse sc0_http_req_rate gt 5 
    
    # update use_backend condition 
    use_backend SOMESERVER if whitelisted !http_rate_abuse 
    

    カスタマイズされたエラーが発生しているいくつかの作業設定ファイル::

    global 
        log /dev/log local1 debug 
    
    defaults 
        log  global 
        mode http 
        option httplog 
        retries 3 
        option redispatch 
        maxconn 2000 
        contimeout  5000 
        clitimeout  50000 
        srvtimeout  50000 
    
    frontend http 
        bind *:8181 
    
        stick-table type ip size 200k expire 100s store http_req_rate(60s) 
        tcp-request content track-sc0 src 
    
        acl whitelist src 127.0.0.1 
        acl http_rate_abuse sc0_http_req_rate gt 5 
        use_backend error401 if !whitelist 
        use_backend error429 if http_rate_abuse 
        use_backend realone 
    
    backend realone 
        server local stackoverflow.com:80 
    
    # too many requests 
    backend error429 
        mode http 
        errorfile 503 /etc/haproxy/errors/429.http 
    
    # unauthenticated 
    backend error401 
        mode http 
        errorfile 503 /etc/haproxy/errors/401.http 
    

    注:エラーハンドリングは少しトリッキーですIP

    tcp-request content track-sc0 src 
    # more info at http://cbonte.github.io/haproxy-dconv/1.5/configuration.html#4.2-tcp-request%20connection 
    

    、最終的には、ACLで。上記のエラーバックエンドにはサーバーエントリがないため、haproxyはHTTP 503、errorfileをキャッチして異なるエラーを(異なるコードで)送信します。

    /etc/haproxy/errors/401.http内容:

    HTTP/1.0 401 Unauthenticated 
    Cache-Control: no-cache 
    Connection: close 
    Content-Type: text/html 
    
    <html><body><h1>401 Unauthenticated</h1> 
    </body></html> 
    

    /etc/haproxy/errors/429.http内容:

    HTTP/1.0 429 Too many requests 
    Cache-Control: no-cache 
    Connection: close 
    Content-Type: text/html 
    
    <html><body><h1>429 Too many requests</h1> 
    </body></html> 
    
    関連する問題