Day37nginx常用模块

需要的服务器

角色 外网ip 内网ip 主机名
web eth0:10.0.0.7 eth1:172.16.1.7 web01

autoindex 目录索引

[root@web01 ~]# cat /etc/nginx/conf.d/mirror.oldxu.com.conf 
    server {
        listen 80;
        server_name mirror.oldxu.com;
        charset utf8;

        location / {
            root /code;
            index index.html;

            autoindex on;
            autoindex_exact_size off;
            autoindex_localtime on;
            autoindex_format html;
        }
    }
    
    [root@web01 ~]# mkdir /code/{centos,ubuntu}
    [root@web01 ~]# cat /code/index.html 
    

Mirrors Oldxu.com

访问控制:
基于IP实现访问控制
10.0.0.1 仅允许访问 /centos
10.0.0.100 拒绝访问 /ubuntu , 其他的IP都允许

root@web01 ~]# cat /etc/nginx/conf.d/mirror.oldxu.com.conf 
        server {
            listen 80;
            server_name mirror.oldxu.com;
            charset utf8;
            root /code;

            autoindex on;
            autoindex_exact_size off;
            autoindex_localtime on;
            autoindex_format html;

            location / {
                index index.html;
            }
            location /centos {
                allow 10.0.0.1/32;
                deny all;
            }
            location /ubuntu {
                deny 10.0.0.100/32;
                allow all;
            }
        }

基于用户名和密码的访问控制:
[root@web01 ~]# yum install httpd-tools -y
[root@web01 ~]# htpasswd -bc /etc/nginx/auth_conf oldxu 123

[root@web01 ~]# cat /etc/nginx/conf.d/mirror.oldxu.com.conf 
        server {
            listen 80;
            server_name mirror.oldxu.com;
            charset utf8;
            root /code;

            autoindex on;
            autoindex_exact_size off;
            autoindex_localtime on;
            autoindex_format html;

            location / {
                index index.html;
            }

            location /centos {
                allow 10.0.0.1/32;
                deny all;
            }
            location /ubuntu {
                auth_basic "Oldxu Site";
                auth_basic_user_file /etc/nginx/auth_conf;
            }
        }

限速
请求限制 limit_req
连接限制 limit_conn
下载限速:

[root@web01 ~]# cat /etc/nginx/conf.d/mirror.oldxu.com.conf 
    limit_req_zone $binary_remote_addr zone=req_one:10m rate=1r/s;
    limit_conn_zone $binary_remote_addr zone=addr:10m;

    server {
        listen 80;
        server_name mirror.oldxu.com;
        charset utf8;
        root /code;

        limit_req zone=req_one burst=5 nodelay;
        
        limit_conn addr 1;
        limit_conn_status 504;

        limit_rate 100k;
        limit_rate_after 200m;

        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
        autoindex_format html;

        location / {
            index index.html;
        }

        location /centos {
            allow 10.0.0.1/32;
            allow 10.0.0.7/32;
            deny all;
        }
    }

案列
问题:限制web服务器请求数处理为1秒一个,缓存值为5、限制用户仅可同时下载一个文件。当下载超过100M则限制下载速度为500k。如果同时下载超过2个视频,则返回提示 "请联系oldxu进行会员充值"。

[root@web01 ~]# cat /etc/nginx/conf.d/mirror.oldxu.com.conf
    limit_req_zone $binary_remote_addr zone=req_one:10m rate=1r/s;
    limit_conn_zone $binary_remote_addr zone=addr:10m;

    server {
        listen 80;
        server_name mirror.oldxu.com;
        charset utf8;
        root /code;

        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
        autoindex_format html;

        location / {
            index index.html;
        }

        location /download {
            limit_req zone=req_one burst=5 nodelay;
            limit_conn addr 2;
            limit_conn_status 504;

            limit_rate 300k;
            limit_rate_after 100m;
        }
        #接收抛出504的异常,交给内部 @error_504 处理
        error_page 504 @error_504;
        location @error_504 {
            #default_type text/html;
            #return 200 "请充值会员";

            #跳转到其他页面
            #return 302 https://www.waitsun.com/xpay-html;
        }
    }

nginx七中状态 stub_stauts

状态 含义
Active connections 当前活跃连接数,包括Waiting等待连接数。
accepts 已接收的总TCP连接数量。
handled 已处理的TCP连接数量。
requests 当前总http请求数量。
Reading 当前读取的请求头数量
Writing 当前响应的请求头数量
Waiting 当前等待请求的空闲客户端连接数

状态监控模块

location = /nginx_status {
    stub_status;
}

location匹配规则
location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }

    
    [root@web01 ~]# cat /etc/nginx/conf.d/location.oldxu.com.conf 
    server {
        listen 80;
        server_name location.oldxu.com;

        location = / {
            default_type text/html;
            return 200 'location = /';
        }

        location / {
            default_type text/html;
            return 200 'location /';
        }

        location /documents/ {
            default_type text/html;
            return 200 'location /documents/';
        }

        location ^~ /images/ {
            default_type text/html;
            return 200 'location ^~ /images/';


        }

        location ~* \.(gif|jpg|jpeg)$ {
            default_type text/html;
            return 200 'location ~* \.(gif|jpg|jpeg)';
        }
    }   

nginx 日志
访问日志 记录用户请求的信息
错误日志 记录所有错误信息,便于后期排查

server {
            access_log /var/log/nginx/vhost_name.log main;
            error_log /var/log/nginx/vhost_name_error.log;
        }
状态 含义
$remote_addr # 记录客户端IP地址 user --> web
$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地址和客户端请求的服务器

你可能感兴趣的:(Day37nginx常用模块)