Syntax: limit_conn_zone key zone=name:size;Default: —Context: httpSyntax: 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/ { # 服務(wù)器每次只允許一個IP地址連接 limit_conn addr 1;}2. 請求頻率限制ngx_http_limit_req_module語法
Syntax: limit_req_zone key zone=name:size rate=rate;Default: —Context: http用法
Default: —Context: http, server, location
在nginx配置文件中的 http 下配置
http { # ...其它代碼省略... # 開辟一個10m的請求空間,命名為one。同一個IP發(fā)送的請求,平均每秒只處理一次 limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; server { location /search/ { limit_req zone=one; # 當(dāng)客戶端請求超過指定次數(shù),最多寬限5次請求,并延遲處理,1秒1個請求 # limit_req zone=one burst=5; # 當(dāng)客戶端請求超過指定次數(shù),最多寬限5次請求,并立即處理。 # limit_req zone=one burst=5 nodelay;}二、Nginx的訪問控制1. 基于IP的訪問控制ngx_http_access_module語法
Syntax: allow address | CIDR | unix: | all;Default: —Context: http, server, location, limit_exceptSyntax: deny address | CIDR | unix: | all;Default: —Context: http, server, location, limit_exceptaddress:IP地址,例如:192.168.1.1
在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 的局限性
當(dāng)客戶端通過代理訪問時,nginx的remote_addr獲取的是代理的IP
http_x_forwarded_for
http_x_forwarded_for = Client IP, Proxy1 IP, Proxy2 IP, ...
remote_addr 獲取的是直接和服務(wù)端建立連接的客戶端IP。
http_x_forwarded_for 可以記錄客戶端及所有中間代理的IP
Syntax: auth_basic string | off;Default: auth_basic off;Context: http, server, location, limit_except用法
Default: —Context: http, server, location, limit_except
要使用 htpasswd 命令,需要先安裝httpd-tools
[root~]# yum -y install httpd-tools
使用 htpasswd 命令創(chuàng)建賬號密碼文件
[root/etc/nginx]# htpasswd -c ./auth_conf auth_rootNew 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_confauth_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
輸入正確的用戶名和密碼,即可正常訪問。
ngx_http_auth_basic_module 的局限性用戶信息依賴文件方式
操作管理效率低下
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,更多相關(guān)內(nèi)容請關(guān)注PHP !
相關(guān)推薦:
關(guān)于Nginx常用的官方模塊
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。
|
新聞熱點(diǎn)
疑難解答
圖片精選