nginx ——请求和访问限制

Nginx 的请求限制

  • 连接频率限制 - limit_conn_module
  • 请求频率限制 - limit_req_module

Nginx 的访问控制

  • 基于 IP 的访问控制 - http_access_module
  • 基于用户的信任登录 - http_auth_basic_module

http_access_module

server {
        listen       80;
        server_name  localhost;
  
        location ~ ^/admin.html {
            root /app/nginx/html/access;
            # 禁止 172.27.254.48 IP访问
            deny 172.27.254.48;
            allow all;
            index admin.html admin.htm;
        }

        location ~ ^/admin.html {
            root /app/nginx/html/access;
            # 只允许 172.27.254.48 IP访问
            allow 172.27.254.48;
            deny all;
            index admin.html admin.htm;
        }

}

http_auth_basic_module

安装 httpd-tools

yum install httpd-tools -y

生成密钥

htpasswd -c ./auth_conf admin
image.png

配置 conf

server {
        listen       80;
        server_name  localhost;

        location / {
            root   /app/nginx/html/proxy;
            index  proxy.html index.htm;
        }
        location /access {
            alias   /app/nginx/html/access;
            # 配置参数
            auth_basic "Auth access test!input your passward!";
            auth_basic_user_file /app/nginx/auth_conf;
            index  admin.html;
        }

}

reload 之后页面访问

image.png
image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容