Nginx請(qǐng)求限制和訪問控制怎么實(shí)現(xiàn)

一、nginx的請(qǐng)求限制

1. http協(xié)議的連接與請(qǐng)求

http協(xié)議版本與連接關(guān)系

http協(xié)議版本 連接關(guān)系
http1.0 tcp不能復(fù)用
http1.1 順序性tcp復(fù)用
http2.0 多路復(fù)用tcp復(fù)用

http請(qǐng)求建立在一次tcp連接的基礎(chǔ)上。

一次tcp連接至少可以產(chǎn)生一次http請(qǐng)求,http1.1版本以后,建立一次tcp連接可以發(fā)送多次http請(qǐng)求。

Nginx請(qǐng)求限制和訪問控制怎么實(shí)現(xiàn)

1. 連接頻率限制

語(yǔ)法

syntax:????limit_conn_zone?key?zone=name:size; default:??— context:??http  syntax:????limit_conn?zone?number; default:??— context:??http,?server,?location

用法

nginx配置文件中的 http 下配置

http?{ ??#?...其它代碼省略... ??#?開辟一個(gè)10m的連接空間,命名為addr ??limit_conn_zone?$binary_remote_addr?zone=addr:10m; ??server?{ ????... ????location?/download/?{ ??????#?服務(wù)器每次只允許一個(gè)ip地址連接 ??????limit_conn?addr?1; ????} ??} }

2. 請(qǐng)求頻率限制

語(yǔ)法

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?{  ??#?...其它代碼省略... ?? ??#?開辟一個(gè)10m的請(qǐng)求空間,命名為one。同一個(gè)ip發(fā)送的請(qǐng)求,平均每秒只處理一次 ??limit_req_zone?$binary_remote_addr?zone=one:10m?rate=1r/s; ?? ??server?{ ??????...  ????location?/search/?{ ??????limit_req?zone=one; ??????#?當(dāng)客戶端請(qǐng)求超過指定次數(shù),最多寬限5次請(qǐng)求,并延遲處理,1秒1個(gè)請(qǐng)求 ??????#?limit_req?zone=one?burst=5; ??????#?當(dāng)客戶端請(qǐng)求超過指定次數(shù),最多寬限5次請(qǐng)求,并立即處理。 ??????#?limit_req?zone=one?burst=5?nodelay;  ????} ??} }

二、nginx的訪問控制

1. 基于ip的訪問控制

語(yǔ)法

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;?#?拒絕這個(gè)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)客戶端通過代理訪問時(shí),nginx的remote_addr獲取的是代理的ip

Nginx請(qǐng)求限制和訪問控制怎么實(shí)現(xiàn)

http_x_forwarded_for

http_x_forwarded_for = client ip, proxy1 ip, proxy2 ip, …

remote_addr?獲取的是直接和服務(wù)端建立連接的客戶端ip。
http_x_forwarded_for?可以記錄客戶端及所有中間代理的ip

Nginx請(qǐng)求限制和訪問控制怎么實(shí)現(xiàn)

2. 基于用戶的登錄認(rèn)證

語(yǔ)法

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 命令創(chuàng)建賬號(hào)密碼文件

[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請(qǐng)求限制和訪問控制怎么實(shí)現(xiàn)

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

ngx_http_auth_basic_module 的局限性

  • 用戶信息依賴文件方式

  • 操作管理效率低下

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊6 分享
站長(zhǎng)的頭像-小浪學(xué)習(xí)網(wǎng)月度會(huì)員