nginx常用模块

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介: nginx常用模块

1.配置nginx官方yum源
http://nginx.org/en/linux_packages.html#RHEL-CentOS
vim /etc/yum.repos.d/nginx.repo
添加:
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
保存退出

2.安装nginx
yum -y install nginx

3.查看nginx默认模块
nginx -V

4.nginx新版本的配置文件
全局配置文件:/etc/nginx/nginx.conf
虚拟主机配置:/etc/nginx/conf.d/*.conf
例子1:使用域名搭建一台虚拟主机
mkdir /www
复制网页代码到/www目录下
vim /etc/nginx/conf.d/www.conf
添加:
server {
listen 80;
server_name www.yundong.com;
location / {
root /www;
index index.html index.htm;
}

        }
        保存退出
        systemctl restart nginx 
        客户端修改/etc/hosts访问测试

5.nginx目录索引(autoindex自动索引模块) *
nginx默认不起用目录索引,更不允许列出网站目录提供下载。
Syntax: autoindex on | off; 索引功能的开或关
Default: autoindex off; 默认关闭
Context: http, server, location 场景:全局、某个虚拟主机、某个虚拟主机的目录

例子:在www网站下,创建download下载目录,索引显示
mkdir /www/download
复制文件到/www/download目录下
vim  /etc/nginx/conf.d/www.conf
在server字段中添加:
location /download {
    root   /www;
    autoindex on;                        启用索引显示
    charset utf-8,gbk;                    字符编码为中文
    autoindex_exact_size on;            显示文件大小        
    autoindex_localtime on;                显示文件创建时间
}
保存退出
systemctl reload nginx
客户端测试访问:http://www.www.com/download

6.nginx状态监控(status模块)*
Syntax: stub_status; 启用状态化追踪
Default: — 默认关闭
Context: server, location 场景:

例子:针对www网站,启用状态化追踪
vim  /etc/nginx/conf.d/www.conf
在server字段中添加:
location /status {
    stub_status;                        启用状态化追踪
    access_log off;                        关闭status的日志记录
}
保存退出
systemctl reload nginx
客户端访问:http://www.www.com/status
客户端显示结果如下:  ***
Active connections: 1                     当前活跃的连接数
server  accepts     19                 当前的总tcp连接数
    handled     19                 成功的连接数
    requests    486                总HTTP请求数

7.nginx基于ip的访问控制(access模块)
例子:仅允许内部网段或vpn访问status
vim /etc/nginx/conf.d/www.conf
修改为:
location /status {
stub_status;
access_log off;
allow 192.168.1.0/24; 仅允许1.0网段访问
deny all; 拒绝其他所有
}

8.nginx基于用户的访问控制(auth模块)
例子:设置访问/status,用户密码验证
yum -y install httpd-tools
htpasswd -b -c /etc/nginx/.auth_conf webadmin 123456
vim /etc/nginx/conf.d/www.conf
修改为:
location /status {
stub_status;
access_log off;
auth_basic "input your passwd:"; 用户验证启用描述
auth_basic_user_file /etc/nginx/.auth_conf; 用户验证文件路径
}

9.nginx的访问限制*
limit_conn_module 连接频率限制
例子:
http {
limit_conn_zone $binary_remote_addr zone=addr:10m; 创建zone区域名为addr,大小10m,保存客户端的二进制ip
server {
location /download/ {
limit_conn addr 1; 一个ip同一时间点只允许建立一个连接
}
}
}

limit_req_module        请求频率限制
例子:
http {
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;    创建zone区域名为one,大小10m,保存客户端的二进制ip,限制请求速率每秒1次
    server {
        location /download {
            limit_req zone=one burst=5;                        调用请求速率区域,另外设置额外突发5次
            }
        }
}

11.nginx日志格式:log_format
例子:
vim /etc/nginx/nginx.conf
http {
log_format main '$remote_addr - $remote_user [$time_iso8601] "$request" ' #定义日志输出格式main
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;                                    #调用日志格式main
}
#nginx日志格式的变量:
$remote_addr            #记录客户端的ip地址
$remote_user            #记录客户端的用户名
$time_local                #通用的时间格式
$time_iso8601            #iso8601时间格式
$request                #请求的方法和请求的HTTP协议
$status                    #请求状态码
$body_bytes_sent        #服务器回应的字节数,不包含头部大小
$bytes_sent                #服务器回应的总字节数
$msec                    #日志写入时间,单位为秒,精度为毫秒
$http_referer            #记录链接访问源地址
$http_user_agent        #记录客户端浏览器信息
$http_x_forwarded_for    #代理服务器ip
$request_length            #请求包的长度(请求头+请求正文)
$request_time            #请求花费的时间,单位为秒,精度为毫秒 ***

12.nginx的location *
语法详解
语法规则: location [=|~|~*|^~] /uri/ { … }
下列以优先级从高到低排序

= 开头表示精确匹配

^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。

~ 开头表示区分大小写的正则匹配

~* 开头表示不区分大小写的正则匹配

!~和!~*分别为区分大小写不匹配及不区分大小写不匹配的正则

/ 通用匹配,任何请求都会匹配到。

例子:测试匹配符的优先级
cd /etc/nginx/conf.d/
vim test.conf
添加:
server {
listen 80;
server_name test.benet.com;

    location / {
            default_type text/html;
            return 200 "location /";
    }
    location =/ {
            default_type text/html;
            return 200 "location =/";
    }
    location ~ / {
            default_type text/html;
            return 200 "location ~ /";
    }
    location ~* / {
            default_type text/html;
            return 200 "location ~* /";
    }

}
保存退出
客户端修改hosts文件,测试访问

真实企业场景配置:*

通用匹配,任何请求都会匹配到。

location / {

}

严格区分大小写,匹配.php结尾

location ~ .php$ {
fastcgi_pass http://127.0.0.1:9000;
}

严格区分大小写,匹配.jsp结尾

location ~ .jsp$ {
proxy_pass http://127.0.0.1:8080;
}

不区分大小写匹配

location ~* ".(sql|bak|tgz|tar.gz|.git)$ {
default_type text/html;
return 403 "启用访问控制";
}

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
目录
相关文章
|
1月前
|
应用服务中间件 nginx
Nginx安装nginx-rtmp-module模块
【2月更文挑战第4天】 nginx中的模块虽然就是类似插件的概念,但是它无法像VsCode那样轻松的安装扩展。 nginx要安装其它模块必须同时拿到nginx源代码和模块源代码,然后手动编译,将模块打到nginx中,最终生成一个名为nginx的可执行文件。
222 6
|
1月前
|
应用服务中间件 nginx
百度搜索:蓝易云【利用nginx内置ngx_http_mirror_module模块实现流量复制及流量放大】
以上就是使用Nginx内置 `ngx_http_mirror_module`模块实现流量复制和流量放大的简要示例。通过合理配置和利用该模块,可以实现更复杂的流量控制和调试需求。
78 1
|
29天前
|
应用服务中间件 数据库 nginx
nginx 第三方模块 与变量
nginx 第三方模块 与变量
|
1月前
|
Ubuntu 应用服务中间件 nginx
ubuntu编译安装nginx及安装nginx_upstream_check_module模块
以上是编译安装Nginx和安装 `nginx_upstream_check_module`模块的基本步骤。根据你的需求和环境,你可能需要进一步配置Nginx以满足特定的要求。
76 3
|
1月前
|
应用服务中间件 nginx Python
nginx-upload-module模块实现文件断点续传_nginx upload module 断点续传 进度(1)
nginx-upload-module模块实现文件断点续传_nginx upload module 断点续传 进度(1)
|
1月前
|
应用服务中间件 Linux PHP
Linux下安装php环境并且配置Nginx支持php-fpm模块
Linux下安装php环境并且配置Nginx支持php-fpm模块
102 0
|
1月前
|
消息中间件 关系型数据库 MySQL
使用Nginx的stream模块实现MySQL反向代理与RabbitMQ负载均衡
使用Nginx的stream模块实现MySQL反向代理与RabbitMQ负载均衡
177 0
|
7月前
|
应用服务中间件 nginx
nginx中handle模块的编写小案例
nginx中handle模块的编写小案例
nginx中handle模块的编写小案例
|
1月前
|
存储 应用服务中间件 nginx
Nginx模块开发:handler模块实现
Nginx模块开发:handler模块实现
38 0
|
1月前
|
存储 应用服务中间件 nginx
Nginx模块开发:模块结构的源码阅读以及过滤器(Filter)模块的实现
Nginx模块开发:模块结构的源码阅读以及过滤器(Filter)模块的实现
102 0