Nginx基礎篇(4)- Nginx請求限制和訪問控制

Nginx請求限制和訪問控制

1、Nginx的請求限制

1. HTTP協議的鏈接與請求

HTTP協議版本與鏈接關係

HTTP協議版本 鏈接關係
HTTP1.0 TCP不能複用
HTTP1.1 順序性TCP複用
HTTP2.0 多路複用TCP複用
HTTP請求創建在一次TCP鏈接的基礎上。
一次TCP鏈接至少能夠產生一次HTTP請求,HTTP1.1版本之後,創建一次TCP鏈接能夠發送屢次HTTP請求。

HTTP協議的鏈接與請求

1. 鏈接頻率限制

ngx_http_limit_conn_module

語法

Syntax:        limit_conn_zone key zone=name:size;
Default:    —
Context:    http

Syntax:        limit_conn zone number;
Default:    —
Context:    http, server, location

用法

  • 在nginx配置文件中的 http 下配置
http {
    # ...其它代碼省略...
    # 開闢一個10m的鏈接空間,命名爲addr
    limit_conn_zone $binary_remote_addr zone=addr:10m;
    server {
        ...
        location /download/ {
            # 服務器每次只容許一個IP地址鏈接
            limit_conn addr 1;
        }
    }
}

2. 請求頻率限制

ngx_http_limit_req_module

語法

Syntax:        limit_req_zone key zone=name:size rate=rate;
Default:    —
Context:    http


Syntax:        limit_req zone=name [burst=number] [nodelay];
Default:    —
Context:    http, server, location

用法

  • 在nginx配置文件中的 http 下配置
http {

    # ...其它代碼省略...
    
    # 開闢一個10m的請求空間,命名爲one。同一個IP發送的請求,平均每秒只處理一次
    limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
    
    server {
            ...

        location /search/ {
            limit_req zone=one;
            # 當客戶端請求超過指定次數,最多寬限5次請求,並延遲處理,1秒1個請求
            # limit_req zone=one burst=5;
            # 當客戶端請求超過指定次數,最多寬限5次請求,並當即處理。
            # limit_req zone=one burst=5 nodelay;

        }
    }
}

2、Nginx的訪問控制

1. 基於IP的訪問控制

ngx_http_access_module

語法

Syntax:        allow address | CIDR | unix: | all;
Default:    —
Context:    http, server, location, limit_except

Syntax:        deny address | CIDR | unix: | all;
Default:    —
Context:    http, server, location, limit_except
address:IP地址,例如:192.168.1.1
CIDR:例如:192.168.1.0/24;
unix:Socket方式
all:全部

用法

  • 在nginx配置文件中的 server 下配置
server {
    # ...其它代碼省略...
    location ~ ^/index_1.html {
        root   /usr/share/nginx/html;
        deny 151.19.57.60; # 拒絕這個IP訪問
        allow all; # 容許其餘全部IP訪問
    }

    location ~ ^/index_2.html {
        root   /usr/share/nginx/html;
        allow 151.19.57.0/24; # 容許IP 151.19.57.* 訪問
        deny all; # 拒絕其餘全部IP訪問
    }
}

ngx_http_access_module 的侷限性

  • 當客戶端經過代理訪問時,nginx的remote_addr獲取的是代理的IP

當客戶端經過代理訪問時,nginx的remote_addr獲取的是代理的IP

  • http_x_forwarded_for

    http_x_forwarded_for = Client IP, Proxy1 IP, Proxy2 IP, ...html

remote_addr 獲取的是直接和服務端創建鏈接的客戶端IP。
http_x_forwarded_for 能夠記錄客戶端及全部中間代理的IPnode

http_x_forwarded_for和remote_addr區別

2. 基於用戶的登陸認證

ngx_http_auth_basic_module

語法

Syntax:        auth_basic string | off;
Default:    auth_basic off;
Context:    http, server, location, limit_except


Syntax:        auth_basic_user_file file;
Default:    —
Context:    http, server, location, limit_except

用法

  • 要使用 htpasswd 命令,須要先安裝httpd-tools
[root~]# yum -y install httpd-tools
  • 使用 htpasswd 命令建立帳號密碼文件
[root/etc/nginx]# htpasswd -c ./auth_conf auth_root
New password:
Re-type new password:
Adding password for user auth_root

[root/etc/nginx]# ll auth_conf
-rw-r--r-- 1 root root 48 7月   9 11:38 auth_conf

[root/etc/nginx]# cat auth_conf
auth_root:$apr1$2v6gftlm$oo2LE8glGQWi68MCqtcN90
  • 在nginx配置文件中的 server 下配置
server {
    # ...其它代碼省略...
    
    location ~ ^/index.html {
        root   /usr/share/nginx/html;
        auth_basic "Auth access! Input your password!";
        auth_basic_user_file /etc/nginx/auth_conf;
    }
}
  • 修改後從新載入配置文件nginx -s reload
  • 使用瀏覽器訪問 http://192.168.33.88/index.html

基於用戶的登陸認證

輸入正確的用戶名和密碼,便可正常訪問。nginx

ngx_http_auth_basic_module 的侷限性

  • 用戶信息依賴文件方式
  • 操做管理效率低下
相關文章
相關標籤/搜索