美文网首页
Day37nginx常用模块

Day37nginx常用模块

作者: a幕城 | 来源:发表于2019-12-09 18:11 被阅读0次

需要的服务器

角色 外网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 
    <h1> Mirrors Oldxu.com </h1>
    <ul><li><a href="http://mirror.oldxu.com/centos" target="_blank">centos系统</a></li> </ul>
    <ul><li><a href="http://mirror.oldxu.com/ubuntu" target="_blank">ubuntu系统</a></li> </ul>

访问控制:
基于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常用模块

    需要的服务器 角色外网ip内网ip主机名webeth0:10.0.0.7eth1:172.16.1.7web01 ...

  • Python常用模块

    Python常用模块之time模块 Python常用模块之os模块 Python常用模块之sys模块 Python...

  • python基础学习(三)

    常用模块 String模块 数学模块 随机模块 OS模块 os.path模块 re模块 常用函数及操作 列表操作 ...

  • nginx 2

    1 nginx 常用模块整理 nginx 常用模块整理1 http核心模块 ngx_http_core_modu...

  • Day13 作业

    1.常用的math模块中的方法 2.常用的calendar模块中的方法 3.常用的time模块中的方法 4.常用的...

  • Pytorch 常用语法

    常用模块以及设置 创建张量 张量操作 常用函数 模块类 损失函数与优化器 迭代 画图

  • 03-常用模块(三)-系统模块

    Ansible Ansible version : 2.6.2 常用模块(三) 系统模块 系统模块详细地址:htt...

  • 常用模块

    文件操作## 读取文件 可以使用with块缩短代码 分块读取 按行读取:如果要一次性读取所有行,只需调用readl...

  • 常用模块

    1、自定义模块 2、内置模块 3、开源模块 os 用于提供系统级别的操作 os.getcwd() 获取当前工作目录...

  • 常用模块

    functoolscollectionsitertools

网友评论

      本文标题:Day37nginx常用模块

      本文链接:https://www.haomeiwen.com/subject/vmssgctx.html