Home Operation and Maintenance Nginx How to implement Nginx request restriction and access control

How to implement Nginx request restriction and access control

May 17, 2023 pm 08:37 PM
nginx

一、nginx的请求限制

1. http协议的连接与请求

http协议版本与连接关系

http协议版本 连接关系
http1.0 tcp不能复用
http1.1 顺序性tcp复用
http2.0 多路复用tcp复用

http请求建立在一次tcp连接的基础上。

一次tcp连接至少可以产生一次http请求,http1.1版本以后,建立一次tcp连接可以发送多次http请求。

How to implement Nginx request restriction and access control

1. 连接频率限制

语法

syntax:    limit_conn_zone key zone=name:size;
default:  —
context:  http

syntax:    limit_conn zone number;
default:  —
context:  http, server, location
Copy after login

用法

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

http {
  # ...其它代码省略...
  # 开辟一个10m的连接空间,命名为addr
  limit_conn_zone $binary_remote_addr zone=addr:10m;
  server {
    ...
    location /download/ {
      # 服务器每次只允许一个ip地址连接
      limit_conn addr 1;
    }
  }
}
Copy after login

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
Copy after login

用法

在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;

    }
  }
}
Copy after login

二、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
Copy after login
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访问
  }
}
Copy after login

ngx_http_access_module 的局限性

当客户端通过代理访问时,nginx的remote_addr获取的是代理的ip

How to implement Nginx request restriction and access control

http_x_forwarded_for

http_x_forwarded_for = client ip, proxy1 ip, proxy2 ip, ...

remote_addr 获取的是直接和服务端建立连接的客户端ip。
http_x_forwarded_for 可以记录客户端及所有中间代理的ip

How to implement Nginx request restriction and access control

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
Copy after login

用法

要使用 htpasswd 命令,需要先安装httpd-tools

[root~]# yum -y install httpd-tools
Copy after login

使用 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
Copy after login

在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;
  }
}
Copy after login

修改后重新载入配置文件nginx -s reload

使用浏览器访问 http://192.168.33.88/index.html

How to implement Nginx request restriction and access control

输入正确的用户名和密码,即可正常访问。

ngx_http_auth_basic_module 的局限性

  • 用户信息依赖文件方式

  • 操作管理效率低下

The above is the detailed content of How to implement Nginx request restriction and access control. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What's wrong with nginx running for a while? What's wrong with nginx running for a while? Apr 14, 2025 am 07:18 AM

The reasons why nginx hangs up after running for a period of time: 1. Memory leak; 2. Configuration error; 3. Insufficient resources; 4. External factors. Solution: 1. Diagnose memory leaks; 2. Fix configuration errors; 3. Provide more resources; 4. Exclude external factors.

How to implement nginx load balancing How to implement nginx load balancing Apr 14, 2025 am 07:21 AM

Nginx load balancing defines backend servers through the upstream module and uses the location block to proxy the request to these servers. Supports load balancing strategies such as polling, minimum number of connections, response time weighting, and ip_hash. Configuration examples include defining an upstream group and pointing to it using the proxy_pass directive.

nginx restart command nginx restart command Apr 14, 2025 am 07:27 AM

nginx restart command: sudo systemctl restart nginx. Other related commands include: 1. Start: sudo systemctl start nginx; 2. Stop: sudo systemctl stop nginx; 3. Check status: sudo systemctl status nginx.

Apr 14, 2025 am 07:36 AM

Nginx Autoindex is a function of generating directory listing HTML pages, which is used to browse files and view file information when requesting directories instead of files. It can be customized with configuration options such as displaying the exact file size, local time, and custom page format. Advantages include easy browsing, easy configuration and providing file information. Disadvantages include security risks, performance impact, and the inability to customize the appearance of the page.

What is the reason for nginx403 What is the reason for nginx403 Apr 14, 2025 am 07:39 AM

nginx 403 error indicates that the client does not have permission to access the resource. Factors that cause this problem may include: permission settings, nginx configuration, CGI script errors, .htaccess files, or other reasons. Troubleshooting steps include: checking permission settings, reviewing nginx configuration, testing CGI scripts, checking .htaccess files, excluding firewalls or security software, and checking servers and file systems.

How to configure virtual hosts in nginx How to configure virtual hosts in nginx Apr 14, 2025 am 08:15 AM

Configuring nginx virtual host allows multiple websites to be hosted on a single server, each with a separate domain name and root directory. The specific configuration steps include: creating a virtual host configuration file to configure the server block, specifying the server listening port, virtual host domain name and document root directory to enable the virtual host, and linking the configuration file to the enabled directory to reload nginx

How to redirect in nginx How to redirect in nginx Apr 14, 2025 am 08:42 AM

Methods for redirecting through Nginx are 301 permanent redirects (update links or mobile pages) and 302 temporary redirects (handling errors or temporary changes). Configuring redirection involves using location directives in server blocks, advanced features include regular expression matching, proxy redirection, and condition-based redirection. Common uses of redirects include updating URLs, handling errors, redirecting HTTP to HTTPS, and guiding users to a specific country or language version.

The relationship between nginx and web server The relationship between nginx and web server Apr 14, 2025 am 07:09 AM

nginx is a lightweight, non-blocking web server and reverse proxy, commonly used for front-end proxy, load balancing, and caching. Its relationship with a web server is usually: Front-end proxy: nginx handles requests and forwards them to the back-end server. Load Balancer: nginx distributes requests to multiple backend servers. Caching: nginx caches frequently accessed files for performance.

See all articles