Nginx的常用模块

一、概述

Nginx 是一款高性能的开源 Web 服务器和反向代理服务器,它通过模块化架构提供了丰富的功能。

二、模块介绍

1、autoindex模块

1.1、位置:

http、server、location

1.2、开启autoindex

server {

     listen 80;

     server_name _;

  location / {

     root /code;

     index index.html;

     autoindex  on;                         #开启列表功能

    }

}

1.3、关闭autoindex 

server {

     listen 80;

     server_name _;

  location / {

     root /code;

     index index.html;

     autoindex off;                    #关闭列表功能(默认关闭)

     }

}

1.4、autoindex显示文件大小参数

1.4.1、关闭文件大小显示

server {

    listen 80;

    server_name _;

  location / {

    root /code;

    index index.html;

    autoindex on;

    autoindex_exact_size off;          #示出文件的大概大小,单位是kB或者是MB或者是GB

    }

}

1.4.2、打开文件大小显示

server {

    listen 80;

    server_name _;

  location / {

    root /code;

    index index.html;

    autoindex on;

    autoindex_exact_size on;   #显示出文件的确切大小,单位是bytes(默认方式)

    }

1.5、autoindex显示文件时间的参数

1.5.1、打开文件显示时间

server {

    listen 80;

    server_name _;

  location / {

    root /code;

    index index.html;

    autoindex on;

    autoindex_localtime    on;            #示的文件时间为在服务器创建时间

     }

}

1.5.2、关闭文件显示时间

server {

     listen 80;

     server_name _;

   location / {

      root /code;

      index index.html;

     autoindex on;

     autoindex_localtime   off;         #显示的时间为GMT时间(为默认方式)

     }

}

2、修改显示文字格式

server {

     listen 80;

     server_name _;

     charset utf-8,gbk;            #修改文字显示格式

   location / {

     root /code;

     index index.html;

     autoindex on;                 

      }

}

3、对下载资源进行限速参数 

3.1、单纯限制下载速度

server {

     listen 80;

     server_name _;

     limit_rate 1M;                               #限制每秒1M的速度下载

   location / {

     root /code;

     index index.html;

     }

3.2、下载N兆后开始限速 

server {

     listen 80;

     server_name _;

     limit_rate_after  50M;                #下载50M后开始限速

     limit_rate 1M;

   location / {

     root /code;

     index index.html;

    }

4、用户验证模块

4.1、生成密码

htpasswd -c -b /etc/nginx/auth.conf   lv  123   #   lv:验证用户名    123:验证密码   

4.2、服务配置 

 server {

      listen 80;

      server_name _;

      auth_basic  "lv";                                                        #用户提示字符

      auth_basic_user_file  /etc/nginx/auth.conf;              #指定用户名密码文件位置

    location / {

      root /code;

      index index.html;

      }

}

5、Nginx的状态模块 

5.1、位置作用 

作用:显示用户访问nginx的连接信息

位置:server、location 

5.2、配置nginx状态模块 

 server {

     listen 80;

     server_name _;

   location / {

     root /code;

     index index.html;

    }  

location /nginx {               #显示状态的名称可随意

      stub_status;              #显示状态                

    }

}

5.3、浏览器访问测试 

例如: 10.0.0.7/nginx

  Active connections: 6                #当前TCP的连接数
  server accepts handled requests
         32      32       233        #已经接受   #已经处理   #总请求
  Reading: 0 Writing: 1 Waiting: 5     #读头       #响应头     #等待连接

 6、IP访问限制模块

6.1、位置作用 

作用:允许特定用户进行访问

位置:http、server、location 

6.2、配置IP访问限制 

 server {

     listen 80;

     server_name _;

     allow 172.16.1.0/24;                     #允许这个网段的进行访问

     deny all;                                       #限制所有

   location / {

     root /code;

     index index.html;

     }

}

7、TCP连接数控制模块 

7.1、位置 

放在/etc/nginx/nginx.conf 

放在server中

7.2、限制IP数配置

 7.2.1、放在http中

 limit_conn_zone $remote_addr  zone=conn_zone:10m;

# limit_conn_zone : 限制模块名称

# $remote_addr  : 客户端IP

# zone=conn_zone:10m  : 开辟内存空间名称并设置大小为10M

7.2.2、放在server中 

server {

    listen 80;

    server_name _;

    limit_conn conn_zone 1;              #连接限制,限制同时最高1个TCP连接

7.3、请求数限制 

7.3.1、放在http中

 limit_req_zone $remote_addr zone=req_zone:10m rate=20r/s;  #限制每秒钟最多1个请求

7.3.2、放在server/location中 

 limit_req zone=req_zone burst=3 nodelay;           #延迟3个请求

 limit_req_status 478;                                             #返回状态码

 error_page 478 /err.html;                                      #重定错误状态码

三、 总结

Nginx 状态模块是一个简单但功能强大的工具,用于实时监控服务器的运行状态。通过合理配置和使用,可以有效帮助管理员优化服务器性能、排查问题并实现自动化监控。

你可能感兴趣的:(nginx,运维)