Nginx請求限制和存取控制怎麼實現
一、nginx的请求限制
1. http协议的连接与请求
http协议版本与连接关系
http协议版本 | 连接关系 |
---|---|
http1.0 | tcp不能复用 |
http1.1 | 顺序性tcp复用 |
http2.0 | 多路复用tcp复用 |
http请求建立在一次tcp连接的基础上。
一次tcp连接至少可以产生一次http请求,http1.1版本以后,建立一次tcp连接可以发送多次http请求。
1. 连接频率限制
语法
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. 请求频率限制
语法
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; } } }
二、nginx的访问控制
1. 基于ip的访问控制
语法
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.1cidr
:例如: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
http_x_forwarded_for
http_x_forwarded_for = client ip, proxy1 ip, proxy2 ip, ...
remote_addr
获取的是直接和服务端建立连接的客户端ip。http_x_forwarded_for
可以记录客户端及所有中间代理的ip
2. 基于用户的登录认证
语法
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
输入正确的用户名和密码,即可正常访问。
ngx_http_auth_basic_module 的局限性
用户信息依赖文件方式
操作管理效率低下
以上是Nginx請求限制和存取控制怎麼實現的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

如何解決 Nginx 403 Forbidden 錯誤?檢查文件或目錄權限;2. 檢查 .htaccess 文件;3. 檢查 Nginx 配置文件;4. 重啟 Nginx。其他可能原因還包括防火牆規則、SELinux 設置或應用程序問題。

服務器無權訪問所請求的資源,導致 nginx 403 錯誤。解決方法包括:檢查文件權限。檢查 .htaccess 配置。檢查 nginx 配置。配置 SELinux 權限。檢查防火牆規則。排除其他原因,如瀏覽器問題、服務器故障或其他可能的錯誤。

解決 Nginx 跨域問題有兩種方法:修改跨域響應頭:添加指令以允許跨域請求,指定允許的方法和頭,以及設置緩存時間。使用 CORS 模塊:啟用模塊並配置 CORS 規則,允許跨域請求、方法、頭和設置緩存時間。

在 Linux 中啟動 Nginx 的步驟:檢查 Nginx 是否已安裝。使用 systemctl start nginx 啟動 Nginx 服務。使用 systemctl enable nginx 啟用在系統啟動時自動啟動 Nginx。使用 systemctl status nginx 驗證啟動是否成功。在 Web 瀏覽器中訪問 http://localhost 查看默認歡迎頁面。

問題的答案:304 Not Modified 錯誤表示瀏覽器已緩存客戶端請求的最新資源版本。解決方案:1. 清除瀏覽器緩存;2. 禁用瀏覽器緩存;3. 配置 Nginx 允許客戶端緩存;4. 檢查文件權限;5. 檢查文件哈希;6. 禁用 CDN 或反向代理緩存;7. 重啟 Nginx。

如何在 Windows 中配置 Nginx?安裝 Nginx 並創建虛擬主機配置。修改主配置文件並包含虛擬主機配置。啟動或重新加載 Nginx。測試配置並查看網站。選擇性啟用 SSL 並配置 SSL 證書。選擇性設置防火牆允許 80 和 443 端口流量。

確認 Nginx 是否啟動的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 檢查端口 80 是否開放;3. 查看系統日誌中 Nginx 啟動消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

查看 Nginx 運行狀態的方法有:使用 ps 命令查看進程狀態;查看 Nginx 配置文件 /etc/nginx/nginx.conf;使用 Nginx 狀態模塊啟用狀態端點;使用 Prometheus、Zabbix 或 Nagios 等監控工具。
