Nginx模块

Nginx虚拟主机

1、基于多IP的方式
[root@web01 conf.d]# cat game2.conf 
server {
    listen 80;
    server_name 192.168.15.7;
    location / {
	root /opt/Super_Marie;
        index index.html;
    }
}
server {
    listen 80;
    server_name 172.16.1.7;
    location / {
        root /opt/tank;
        index index.html;
    }
}

2、基于多端口的方式
[root@web01 conf.d]# cat game3.conf 
server {
    listen 80;
    server_name 192.168.15.7;
    location / {
        root /opt/Super_Marie;
        index index.html;
    }
}
server {
    listen 81;
    server_name 192.168.15.7;
    location / {
        root /opt/tank;
        index index.html;
    }
}

3、基于多域名的方式
[root@web01 conf.d]# cat game4.conf 
server {
    listen 80;
    server_name www.game.com;
    location / {
        root /opt/Super_Marie;
        index index.html;
    }
}
server {
    listen 80;
    server_name www.game1.com;
    location / {
        root /opt/tank;
        index index.html;
    }
}

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 # 请求花费的时间,单位为秒,精度毫秒
# 注:如果 Nginx 位于负载均衡器, nginx 反向代理之后, web 服务器无法直接获取到客 户端真实的 IP 地址。
# $remote_addr 获取的是反向代理的 IP 地址。 反向代理服务器在转发请求的 http 头信息中,
# 增加 X-Forwarded-For 信息,用来记录客户端 IP 地址和客户端请求的服务器地址。

nginx连环坑

1.如果是yum安装,启动关闭命令推荐使用systemctl。不要混着nginx -s这样用。

2.相同域名相同端口会报冲突,比如都是0.0.0.0:80

3.没有首页会报403而不是404

4.端口优先级高于域名

5.ip+端口的优先级是最高的

6.所有域名都匹配不上的时候,默认转发到根据ASCII码排序优先的配置文件

7.可以添加参数指定默认匹配的页面,这样就无需修改文件名了
server {
    listen       80 default_server;
    server_name  www.mysun.com;
    location / {
        root   /code/www;
        index  index.html index.htm;
    }
}

日志切割

[root@web01 /var/log/nginx]# cat /etc/logrotate.d/nginx
/var/log/nginx/*.log {
        daily
        missingok
        rotate 52
        compress
        delaycompress
	dateext
        notifempty
        create 640 nginx adm
        sharedscripts
        postrotate
                if [ -f /var/run/nginx.pid ]; then
                        kill -USR1 `cat /var/run/nginx.pid`
                fi
        endscript
}

Nginx模块

目录索引模块

1.1 应用场景

模块名:ngx_http_autoindex_module

可以使用nginx作为简易的文件下载服务器

1.2 参数说明

Syntax: autoindex on | off;
Default: autoindex off;
Context: http, server, location

# autoindex 常用参数
autoindex_exact_size off;
默认为 on, 显示出文件的确切大小,单位是 bytes。
修改为 off,显示出文件的大概大小,单位是 kB 或者 MB 或者 GB。

autoindex_localtime on;
默认为 off,显示的文件时间为 GMT 时间。
修改为 on, 显示的文件时间为文件的服务器时间。

charset utf-8,gbk;
默认中文目录乱码,添加上解决乱码

1.3 配置文件

[root@web01 /usr/share/nginx/html]# cat /etc/nginx/conf.d/download.conf 
server {
    listen 80;
    server_name download.xxx.com;
    location / {
        root /usr/share/nginx/html/download;
        charset utf-8,gbk;
        autoindex on;
        autoindex_localtime on;
        autoindex_exact_size off;
    }
}

[root@web01 /usr/share/nginx/html]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /usr/share/nginx/html]# systemctl restart nginx

1.4 创建测试数据

touch /usr/share/nginx/html/download/{1..10}.txt

1.5 访问页面

image

状态监控模块

2.1 状态字段解释

nginx状态模块:  --with-http_stub_status_module
Active connections # 当前活动的连接数
accepts # 当前的总连接数 TCP
handled # 成功的连接数 TCP
requests # 总的 http 请求数
Reading # 请求
Writing # 响应
Waiting # 等待的请求数,开启了 keepalive
# 注意, 一次 TCP 的连接,可以发起多次 http 的请求, 如下参数可配置进行验证
keepalive_timeout 0; # 类似于关闭长连接
keepalive_timeout 65; # 65s 没有活动则断开连接

2.2 配置文件

[root@web01 ~]# cat /etc/nginx/conf.d/status.conf 
server {
   listen 80;
   server_name  status.xxx.com;
   stub_status on;
   access_log off;
}

2.3 访问测试

[root@web01 ~]# curl status.xxx.com
Active connections: 1 
server accepts handled requests
 4 4 6 
Reading: 0 Writing: 1 Waiting: 0 

访问控制

3.1 基于IP的访问控制

3.1.1 配置语法
模块名:ngx_http_access_module

#允许配置语法
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
3.1.2 配置案例1:拒绝windwos访问www域名
[root@web01 ~]# cat /etc/nginx/conf.d/01-www.conf 
server   {
    listen       80;
    server_name  www.xxx.com;
    location / {
        root   /usr/share/nginx/html/www;
        index  index.html index.htm;
        deny 192.168.15.1;
        allow all;
    }
}
3.1.3 windows访问测试403

image

3.1.4 使用curl访问测试ok
[root@web01 ~]# curl www.xxx.com
www
3.1.5 配置案例2:只允许windows访问,其他全部拒绝
[root@web01 ~]# cat /etc/nginx/conf.d/01-www.conf    
server   {
    listen       80;
    server_name  www.xxx.com;
    location / {
        root   /usr/share/nginx/html/www;
        index  index.html index.htm;
        allow 192.168.15.1;
        deny all;
    }
}
3.1.6 windows访问测试ok

image-20220104154100267

3.1.7 curl访问测试403
[root@web01 ~]# curl www.xxx.com             
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.16.0</center>
</body>
</html>

3.2 基于用户认证的访问控制

3.2.1 配置语法
模块名:ngx_http_auth_basic_module

#访问提示字符串
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
3.2.2 配置文件
#1.需要安装 httpd-tools,该包中携带了 htpasswd 命令
[root@web01 ~]# yum install httpd-tools -y
#2.创建新的密码文件, -c 创建新文件 -b 允许命令行输入密码
[root@web01 ~]# htpasswd -b -c /etc/nginx/auth_conf xxx xxx
Adding password for user xxx
#3.nginx 配置调用
[root@web01 ~]# cat /etc/nginx/conf.d/01-www.conf 
server   {
    listen       80;
    server_name  www.xxx.com;
    location / {
        auth_basic "Auth access Blog Input your Passwd!";
        auth_basic_user_file auth_conf;
        root   /usr/share/nginx/html/www;
        index  index.html index.htm;
    }
}
3.2.3 访问测试

image

访问限制

经常会遇到这种情况,服务器流量异常,负载过大等等。对于大流量恶意的攻击访问, 会带来带宽的浪费,服务器压力,影响业务,往往考虑对同一个 ip 的连接数,请求数、进行限制。
ngx_http_limit_conn_module 模块可以根据定义的 key 来限制每个键值的连接数,如同一个 IP 来源的连接数。
limit_conn_module 连接频率限制
limit_req_module 请求频率限制

4.1请求限制

4.1.1 配置语法
#模块名 ngx_http_limit_req_module
# limit_conn_zone key zone=addr:10m;
Syntax: limit_req_zone key zone=name:size rate=rate;
Default: —
Context: http
Syntax: limit_conn zone number [burst=number] [nodelay];
Default: —
Context: http, server, location
4.1.2 配置文件
http {
      limit_req_zone $remote_addr zone=req_zone:10m rate=1r/s;
      # limit_conn_zone $remote_addr zone=addr:10m;
}


# 案例1:要求每秒只能有一个访问。
1、控制Nginx访问量

	1、连接池
		limit_req_zone $remote_addr zone=one:10m rate=1r/s;
		声明连接池       变量          名称  连接池的大小  速率
	2、限制数

[root@web01 ~]# cat /etc/nginx/conf.d/01-www.conf 
    limit_req_zone $remote_addr zone=req_zone:10m rate=1r/s;
server   {
    listen       80;
    server_name  www.xxx.com;
    limit_req zone=req_zone burst=3 nodelay;
    access_log  /var/log/nginx/www.access.log  main;
    location / {
        root   /usr/share/nginx/html/www;
        index  index.html index.htm;
    }
}
4.1.3 访问测试
1、控制Nginx连接数

	1、安装ab测试命令
	yum install httpd-tools -y 

	2、ab 参数
		-n : 总共需要访问多少次
		-c : 每次访问多少个

[root@web01 ~]# yum install httpd-tools -y
[root@web01 ~]# ab -n 20 -c 2 http://www.xxx.com/
4.1.4 查看访问日志
[root@web01 ~]# tail -f /var/log/nginx/www.access.log 
192.168.15.7 - - [30/Jul/2019:19:34:48 +0800] "GET / HTTP/1.0" 200 4 "-" "ApacheBench/2.3" "-"
192.168.15.7 - - [30/Jul/2019:19:34:48 +0800] "GET / HTTP/1.0" 200 4 "-" "ApacheBench/2.3" "-"
4.1.5 查看错误日志
[root@web01 ~]# tail -f /var/log/nginx/error.log
4.3 为什么限制请求的效果更好

我们先来回顾一下 http 协议的连接与请求,首先 HTTP 是建立在 TCP 基础之上, 在完成 HTTP 请求需要先建立TCP 三次握手(称为 TCP 连接) ,在连接的基础上在完成 HTTP 的请求。
所以多个 HTTP 请求可以建立在一次 TCP 连接之上, 那么我们对请求的精度限制,当然比对一个连接的限制会更加的有效,因为同一时刻只允许一个 TCP 连接进入, 但是同一时刻多个 HTTP 请求可以通过一个 TCP 连接进入。所以针对 HTTP 的请求限制才是比较优的解决方案。

location
使用 Nginx Location 可以控制访问网站的路径, 但一个 server 可以有多个 location 配置, 多个 location 的优先级该如何区分

5.1 location语法介绍

location [=|^~|~|~*|!~|!~*|/] /uri/ { ...
}

5.2 location语法优先级

image

5.3 配置location匹配规则实战

[root@web01 ~]# cat /etc/nginx/conf.d/01-www.conf 
server {
    listen       80;
    server_name  www.xxx.com;
    root   /usr/share/nginx/html/www;
    location / {
       return 200  "location / \n";
    }
    location = / {
        return 200 "location = \n";
    }

    location /documents/ {
        return 200 "location /documents/ \n";
    }
    location ^~ /images/ {
        return 200 "location ^~ /images/ \n";

    }
    location ~* \.(gif|jpg|jpeg)$ {
        return 200 "location ~* \.(gif|jpg|jpeg) \n";
    }
    access_log off;
}

5.4 测试location匹配规则

#精确匹配=/
[root@web01 ~]# curl www.xxx.com
location = 
#没有满足的请求,所以匹配了/
[root@web01 ~]# curl www.xxx.com/index.html
location / 
#匹配了/documents
[root@web01 ~]# curl www.xxx.com/documents/index.html
location /documents/ 
#没有满足的条件,匹配/
[root@web01 ~]# curl www.xxx.com/某个目录/documents/index.html
location / 
#正则匹配了文件名
[root@web01 ~]# curl www.xxx.com/1.jpg
location ~* \.(gif|jpg|jpeg) 
#~*匹配正则不区分大小写优先于/documents
[root@web01 ~]# curl www.xxx.com/documents/1.jpg
location ~* \.(gif|jpg|jpeg) 
#^~优先匹配于~*
[root@web01 ~]# curl www.xxx.com/images1.jpg   
location ^~ /images/ 
posted @ 2022-01-04 16:10  hai起奈  阅读(128)  评论(1编辑  收藏  举报