nginx--官方模块

news2024/9/20 20:28:55

目录

1.概述

2.Nginx的客户端状态

1.使用

2.目录中选择一个随机主页

3.http内容替换

​编辑

4.nginx请求限制

5.nginx访问控制

1.基于Ip的访问控制

1.1使用

1.2access_mod.conf

1.3只允许自己ip访问

1.4http_x_forwarded_for

1.5http_access_module局限性

2.基于用户的信任登录

2.1auth_mod.conf

2.2局限性


1.概述

nginx官方模块:默认下载,默认支持的模块
nginx第三方模块:自己开发的模块或者第三方开发的模块
--with-compat 
--with-file-aio 
--with-threads 
--with-http_addition_module 
--with-http_auth_request_module 
--with-http_dav_module 
--with-http_flv_module 
--with-http_gunzip_module 
--with-http_gzip_static_module 
--with-http_mp4_module 
--with-http_random_index_module 
--with-http_realip_module 
--with-http_secure_link_module 
--with-http_slice_module 
--with-http_ssl_module 
--with-http_stub_status_module 
--with-http_sub_module 
--with-http_v2_module 
--with-mail 
--with-mail_ssl_module 
--with-stream 
--with-stream_realip_module 
--with-stream_ssl_module 
--with-stream_ssl_preread_module 

2.Nginx的客户端状态

--with-http_stub_status_module
# 官方文档
https://nginx.org/en/docs/http/ngx_http_stub_status_module.html
配置语法
Syntax: stub_status; 配置语法
Default:-  默认没有配置
Context:server location 配置在server或者location下

1.使用

vi /etc/nginx/conf.d/server1.conf

# 检查配置文件语法是否正确
[root@localhost ~]# nginx -tc /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

# 重启nginx
nginx -s reload -c /etc/nginx/nginx.conf

1.nginx.cn/mystatus

server{
        # 默认访问路径配置
        location /mystatus {
            stub_status;
        }
    }


server {
    listen      80;
    server_name  1.nginx.cn;
     location /mystatus {
        stub_status;
    }location / {
        root   /opt/app/server1;
        index  server1.html;
    }
    error_page   500 502 503 504 /50x.html;
    error_page   404 /404x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location = /404x.html {
        root  /usr/share/nginx/html;
    }
   
}

2.目录中选择一个随机主页

--with-http_random_index_module 
# 官方文档
https://nginx.org/en/docs/http/ngx_http_random_index_module.html

Syntax: random_index on | off; 配置语法
Default:random_index off;  默认关闭
Context: location 配置在location下
# 复制文件
cp /opt/app/server2/server2.html /opt/app/server1/server2.html
# 修改配置文件
vi /etc/nginx/conf.d/server1.conf
# 检查配置文件语法是否正确
[root@localhost ~]# nginx -tc /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

# 重启nginx
nginx -s reload -c /etc/nginx/nginx.conf

# 测试 重复刷新即可
1.nginx.cn


# /opt/app/server1 目录下有两个html文件随机展示一个
server{
        listen     80;
        server_name 1.nginx.cn;
        # 默认访问路径配置
        location / {
            root  /opt/app/server1;
            random_index on;
        }
    }

3.http内容替换

--with-http_sub_module 
# 官方文档
https://nginx.org/en/docs/http/ngx_http_sub_module.html

Syntax: sub_filter string replacement; 配置语法
Default:-
Context:http server location 配置在http、server或者location下


Syntax: sub_filter_last_modified on | off; 配置语法
Default: sub_filter_last_modified off;
Context:http server location 配置在http、server或者location下


Syntax: sub_filter_once on | off; 配置语法
Default: sub_filter_once off;
Context:http server location 配置在http、server或者location下


# 修改配置文件
vi /etc/nginx/conf.d/server1.conf
# 检查配置文件语法是否正确
[root@localhost ~]# nginx -tc /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

# 重启nginx
nginx -s reload -c /etc/nginx/nginx.conf

# 测试
1.nginx.cn/server1.html


# /opt/app/server1
server{
        listen     80;
        server_name 1.nginx.cn;
        # 默认访问路径配置
        location / {
            root  /opt/app/server1;
            # 如果有多个内容一样的,默认只替换第一个,需要替换所有的使用sub_filter_once
           sub_filter 'service1' 'service1131231';
           sub_filter_once off;
        }
    }

4.nginx请求限制

连接频率限制 - limit_conn_module
# 官方文档
https://nginx.org/en/docs/http/ngx_http_limit_conn_module.html
# key nginx内置变量 , name 申请的空间名称 ,size申请空间的大小
Syntax: limit_conn_zone key zone=name:size;
Default:一
Context:http 定义在http下面

# zone 对应上面定义的空间名称name
Syntax: limit_conn zone number;
Default:-
Context:http, server, location


请求频率限制 - limit_req_module
#   key nginx内置变量 , name 申请的空间名称 ,size申请空间的大小 , rate 限制大小
Syntax: limit_req_zone key zone=namesize rate=rate;
Default:-
Context:http 定义在http下面

# name 对应上面定义的空间名称name,burst,nodelay默认不需要配置
Syntax: limit_reg zone=name [burst=number] [nodelay];
Default:-
Context:http, server, location


1.使用测试

# 修改配置文件
vi /etc/nginx/conf.d/server1.conf
# 检查配置文件语法是否正确
nginx -tc /etc/nginx/nginx.conf
# 重启nginx
nginx -s reload -c /etc/nginx/nginx.conf
# 测试 -n 发起的请求数 -c 并发的数量 ,可以看到请求错误49个 Non-2xx responses:      49
ab -n 50 -c 20 http://192.168.1.124/server1.html
# 查看日志
tail -f  /var/log/nginx/error.log

2023/04/12 16:34:05 [error] 1938#1938: *5255 limiting requests, excess: 0.911 by zone "req_zone", client: 192.168.1.124, server: localhost, request: "GET /server1.html HTTP/1.0", host: "192.168.1.124"



2.server1.conf

# 1m 1兆
limit_conn_zone $binary_remote_addr zone=conn_zone:1m;
limit_req_zone $binary_remote_addr zone=req_zone:2m rate=1r/s;
server {
    listen      80;
    server_name  1.nginx.cn;

    #access_log  /var/log/nginx/host.access.log  main;
     location /mystatus {
        stub_status;
    }
    # 默认访问路径配置
        location / {
            root  /opt/app/server1;
            # 同一时段只允许1个ip连接过来,一个连接可以发送多个请求
            #limit_conn conn_zone 1;
            # burst 超过指定数速率后遗留的三个到下一秒执行
            #  50此请求可以成功4个 Non-2xx responses:      46
            limit_req zone=req_zone burst=3 nodelay;
            #limit_req zone=req_zone burst=3;
            # 50此请求可以成功1个 Non-2xx responses:      49
            #limit_req zone=req_zone;
            index server1.html;
        }

    error_page   500 502 503 504 /50x.html;
    error_page   404 /404x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location = /404x.html {
        root  /usr/share/nginx/html;
    }
}



3.错误日志

# limit_req zone=req_zone burst=3 nodelay;
GET - /server1.html 192.168.1.124 - - [12/Apr/2023:16:41:17 +0800] "GET /server1.html HTTP/1.0" 200 251 "-" "ApacheBench/2.3" "-"
GET - /server1.html 192.168.1.124 - - [12/Apr/2023:16:41:17 +0800] "GET /server1.html HTTP/1.0" 200 251 "-" "ApacheBench/2.3" "-"
GET - /server1.html 192.168.1.124 - - [12/Apr/2023:16:41:17 +0800] "GET /server1.html HTTP/1.0" 200 251 "-" "ApacheBench/2.3" "-"
GET - /server1.html 192.168.1.124 - - [12/Apr/2023:16:41:17 +0800] "GET /server1.html HTTP/1.0" 200 251 "-" "ApacheBench/2.3" "-"
GET - /server1.html 192.168.1.124 - - [12/Apr/2023:16:41:17 +0800] "GET /server1.html HTTP/1.0" 503 497 "-" "ApacheBench/2.3" "-"
GET - /server1.html 192.168.1.124 - - [12/Apr/2023:16:41:17 +0800] "GET /server1.html HTTP/1.0" 503 497 "-" "ApacheBench/2.3" "-"
GET - /server1.html 192.168.1.124 - - [12/Apr/2023:16:41:17 +0800] "GET /server1.html HTTP/1.0" 503 497 "-" "ApacheBench/2.3" "-"
GET - /server1.html 192.168.1.124 - - [12/Apr/2023:16:41:17 +0800] "GET /server1.html HTTP/1.0" 503 497 "-" "ApacheBench/2.3" "-"
GET - /server1.html 192.168.1.124 - - [12/Apr/2023:16:41:17 +0800] "GET /server1.html HTTP/1.0" 

5.nginx访问控制

1.基于Ip的访问控制

http_access_module
#官方文档
https://nginx.org/en/docs/http/ngx_http_access_module.html
# 允许哪些条件访问
# address ip地址,CIDR网段,unix: socket方式访问; all:允许所有
Syntax: allow address | CIDR | unix: | all;
Default:一
Context:http, server, location, limit_except

# 不允许哪些条件访问
# address ip地址,CIDR网段,unix: socket方式访问; all:允许所有
Syntax: deny address | CIDR | unix: | all;
Default:一
Context:http, server, location, limit_except

1.1使用

# 删除之前配置
rm -rf /etc/nginx/conf.d/server1.conf /etc/nginx/conf.d/server2.conf
# 还原default配置文件
cp  /opt/backup/default.conf /etc/nginx/conf.d/default.conf
# 修改文件名称
mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/access_mod.conf
# 修改文件
vi /etc/nginx/conf.d/access_mod.conf
​
# 检查配置文件语法是否正确
nginx -tc /etc/nginx/nginx.conf
# 重启nginx
nginx -s reload -c /etc/nginx/nginx.conf
# 测试访问
http://192.168.1.124/
# 刷新页面查看错误日志
 tail -f  /var/log/nginx/error.log 
2023/04/12 17:21:16 [error] 2067#2067: *70856 access forbidden by rule, client: 192.168.1.118, server: localhost, request: "GET / HTTP/1.1", host: "192.168.1.124"

1.2access_mod.conf

server {
    listen       80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;
    location / {
        root   /opt/app/server1;
        # 自己pc的ip
        deny 192.168.1.118;
        allow all;
        index  server1.html server1.htm;
        
    }
    error_page   500 502 503 504 /50x.html;
    error_page   404 /404x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location = /404x.html {
        root  /usr/share/nginx/html;
    }
    
}

1.3只允许自己ip访问

location / {
        root   /opt/app/server1;
        # 自己pc的ip
        allow 192.168.1.118;
        index  server1.html server1.htm;
        
    }

1.4http_x_forwarded_for

1.5http_access_module局限性

方法一、采用别的HTTP头信息控制访问,如:HTTPX FORWARD FOR
方法二、结合geo模块作
方法三、通过HTTP自定义变量传递

2.基于用户的信任登录

http_auth_basic_module
#官方文档
https://nginx.org/en/docs/http/ngx_http_auth_basic_module.html
​
Syntax: auth_pasic 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
​
​
​
​
# 修改文件
mv access_mod.conf  auth_mod.conf
# htpasswd工具使用, 输入密码即可,用户名为test
htpasswd -c ./auth_conf test
​
[root@localhost conf.d]# htpasswd -c ./auth_conf test
New password: 
Re-type new password: 
Adding password for user test
​
# 编辑auth_mod.conf
vi auth_mod.conf
​
# 检查配置文件语法是否正确
nginx -tc /etc/nginx/nginx.conf
# 重启nginx
nginx -s reload -c /etc/nginx/nginx.conf
# 测试访问 输入密码即可
http://192.168.1.124/

2.1auth_mod.conf

server {
    listen       80;
    server_name  localhost;
​
    #access_log  /var/log/nginx/host.access.log  main;
​
    location / {
        root   /opt/app/server1;
        auth_basic  "auth access test! input you password";
        auth_basic_user_file /etc/nginx/conf.d/auth_conf;
        index  server1.html server1.htm;
        
    }
    error_page   500 502 503 504 /50x.html;
    error_page   404 /404x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    
    location = /404x.html {
        root  /usr/share/nginx/html;
    }
    
}

2.2局限性

1.用户信息依赖文件方式
2.操作管理机械,效率底下
3.nginx结合lua实现高效验证
4.nginx和LDAP打通,利用nginx-auth-ldap模块

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/417158.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【用python将文件夹下面的文件夹里面的文件全部提取出来,并且放到一个新的文件夹】

文件里面有多个文件,每个文件下面有很多jpg格式的照片,把所有照片提取出来并且放在一个新的文件夹下面。 可以使用Python的os和shutil库来完成这个任务。 比如说:我的faces95文件夹下面有95个文件,每个文件下面有十七到十八个照片…

深拷贝和浅拷贝

目录 一.Java的Cloneable和clone()方法 1.Object类中的clone() 2.实现Cloneable接口的类 3.通过clone()生成对象的特点 二.深拷贝和浅拷贝 1.浅拷贝 2.深拷贝 3.实现深拷贝的两种方法 1.一种是递归的进行拷贝 2.Json字符串的方式进行深拷贝 一.Java的Cloneable和clone…

No.037<软考>《(高项)备考大全》【第21章】项目组合管理

【第21章】项目组合管理1 考试相关2 项目组合管理2.1 项目组合管理、项目集管理、项目管理异同2.2 项目组合管理过程组3 练习题参考答案1 考试相关 选择1分必考 案例概率低,知识点看一遍即可 2 项目组合管理 1、项目组合是将项目、项目集,以及其他方面…

2023年MathorCup数学建模赛题浅析

MathorCup俗称妈杯,是除了美赛国赛外参赛人数首屈一指的比赛,而我们的妈杯今天也如期开赛。今年的妈杯难度,至少在我看来应该是2023年截至目前来讲最难的一场比赛。问题的设置、背景的选取等各个方面都吐露着我要难死你们的想法。难度是恒定的…

Servlet、SpringMVC、SpringBoot整合Thymeleaf汇总

介绍 模板引擎,与JSP、JSTL类似。 好处是:直接写在HTML文件中,服务器可以解析,浏览器也可以解析,实现了动静分离,并未破坏html结构,即使无网络、不通过后端渲染也能在浏览器成功打开&#xff…

kettle——数据清洗(数据表-->文本文件)

实验步骤: 1.数据表(图片加分析,创建表的过程和对应的字段及记录) ①选择数据库 ②创建表结构 ③插入数据 2.kettle连接模块(图片加分析,每个模块实现的功能) ①新建“转换”文件,”文件”——>“新建…

JAVA开发运维(Jenkins中踩的坑)

最近尝试通过Jenkins来自动化部署项目,没想到还踩了很多坑。Jenkins部署的基本原理: 通过Jenkins服务器拉取gitlab上的代码进行打包,推送到目标服务器上,并运行启动脚本。 那么Jenkins就要解决三个问题。 1.连接上目标服务器 …

电子行业应如何实施数字工厂管理系统

随着信息技术的快速发展,电子制造企业也正在逐步做好数字化转型,而数字工厂管理系统便是数字化管理中的一个重要系统。数字工厂系统可以帮助电子企业实现生产过程的自动化、智能化和可视化,提高生产效率,降低生产成本,…

2023香港国际创科展开幕,欧科云链受邀参展

4月12日,由香港特区政府、香港贸易发展局主办的首届香港国际创科展(InnoEX)在香港会展中心开幕,欧科云链(01499.HK)作为全球领先的Web3科技企业受邀参展。香港国际创科展现场图 此次创科展上,欧科云链向公众…

【RocketMQ】负载均衡源码分析

RocketMQ在集群模式下,同一个消费组内,一个消息队列同一时间只能分配给组内的某一个消费者,也就是一条消息只能被组内的一个消费者进行消费,为了合理的对消息队列进行分配,于是就有了负载均衡。 接下来以集群模式下的消…

智优ERP的升级版智优E3_ERP,可以自定义列,和自定义打印公司logo

新版的智优E3_ERP系统,新增了许多供自定义的列。 系统能够解决的企业管理问题: 一、日常的出入库管理、收付款管理、往来对账、移动加权平均成本核算、以及相关数据的查询分析; 二、订单的跟单管理(包括销售跟单、采购跟单、生产…

ElasticSearch常用查询操作

ES查询 一般我们使用ES最多的就是查询,今天就讲一下ES的查询。这里我是建了一个person的索引。 "person" : {"aliases" : { },"mappings" : {"properties" : {"address" : {"type" : "text"…

[LeetCode周赛复盘] 第 102 场双周赛20230415

[LeetCode周赛复盘] 第 102 场双周赛20230415 一、本周周赛总结二、 6333. 查询网格图中每一列的宽度1. 题目描述2. 思路分析3. 代码实现三、6334. 一个数组所有前缀的分数1. 题目描述2. 思路分析3. 代码实现四、6335. 二叉树的堂兄弟节点 II1. 题目描述2. 思路分析3. 代码实现…

English Learning - L2 第 15 次小组纠音 助动词弱读和重音节奏 2023.4.15 周六

English Learning - L2 第 15 次小组纠音 助动词弱读和重音节奏 2023.4.15 周六共性问题have has /hv/ /hz/ 弱读成 /həv/ /həz/fine left /faɪn/ /left/late changed train /leɪt/ /ʧeɪnʤd/ /treɪn/ 中的 eɪmoment problem time /ˈməʊmənt/ /ˈprɒbləm/ /taɪm…

4.10~4.11学习总结

ER图的学习: 学习了ER图相关知识,并绘制了项目大概的ER图 详细笔记博客:http://t.csdn.cn/YOJxq MySQL的学习: 函数 学习了字符串函数,数值函数,日期函数,流程函数。 约束 作用于表中字段的规则…

改善Instagram客户服务的6个技巧

Instagram仍然是全球前四大社交网络,按用户数量排名。它通过其创新的过滤器、内容创建工具、视频和卷轴选项继续增长并推动流量。这是一个平台,世界顶级名人和有影响力的人可以为全球用户提供有趣和令人印象深刻的内容。 但不仅仅是一个娱乐平台&#xf…

Nestjs实战干货-概况-异常过滤器-Exception filters

异常过滤器 Nest 带有一个内置的异常层,负责处理应用程序中所有未处理的异常。当应用程序代码未处理异常时,该层会捕获该异常,然后自动发送适当的用户友好响应。 开箱即用,此操作由内置的全局异常过滤器执行,该过滤器…

三、Locust任务(task)详解

当一个负载测试开始时,将为每个模拟用户创建一个用户类的实例,他们将在自己的绿色线程中开始运行。当这些用户运行时,他们会选择执行的任务,睡眠一段时间,然后选择一个新的任务,如此循环。 这些任务是正常…

二、Java 并发编程(4)

本章概要 Java 中的锁 乐观锁悲观锁自旋锁synchronizedReentrantLocksynchronized 与 ReentrantLock 对比SemaphoreAtomicInteger可重入锁公平锁和非公平锁读写锁共享锁和独占锁重量级锁和轻量级锁偏向锁分段锁同步锁和死锁如何进行锁优化 2.6 Java 中的锁 Java 中的锁主要…

【C语言进阶:动态内存管理】C/C++中程序内存区域的划分

⚡C/C中程序内存区域的划分 C/C程序内存分配的几个区域: 栈区(stack):在执行函数时,函数内局部变量的存储单元都可以在栈上创建,函数执行结 束时这些存储单元自动被释放。栈内存分配运算内置于处理器的指…