Nginx 常见配置 第三方模块echo 小节5

426 阅读1分钟

@[TOC](Nginx 常见配置 第三方模块echo 小节5)

nginx

[root@nginx ~]# cd /data/site1/
#创建一个100M文件
[root@nginx site1]# dd if=/dev/zero of=test.img bs=1M count=100

#启动服务
[root@nginx site1]# nginx

centos6

报错

[root@centos6 ~]$ wget http://192.168.37.7/test.img
--2022-08-07 11:56:39--  http://192.168.37.7/test.img
Connecting to 192.168.37.7:80... connected.
HTTP request sent, awaiting response... 404 Not Found
2022-08-07 11:56:39 ERROR 404: Not Found

nginx

查看错误日志

发现并没有跑到我们希望的目录去找

[root@nginx ~]# cat /var/log/nginx/error.log
...                                                                         并没有跑到我们希望的目录去找
2022/08/07 02:01:06 [error] 10944#10944: *1 open() "/usr/share/nginx/html/test.img" failed (2: No such file or directory), client: 192.168.37.6, server: _, request: "GET /test.img HTTP/1.0", host: "192.168.37.7"

centos6

成功

#不应该写IP、应该写域名
[root@centos6 ~]$ wget http://www.a.net/test.img

ngx_http_core_module (十)

  1. 对客户端进行限制的相关配置
  2. limit_rate rate;

限制响应给客户端的传输速率,单位是bytes/second 默认值0表示无限制

  1. limit_except method ... { ... },仅用于location.

限制客户端使用除了指定的请求方法之外的其它方法

method:GET, HEAD, POST, PUT, DELETE,MKCOL, COPY, MOVE,OPTIONS, PROPFIND, PROPPATCH, LOCK, UNLOCK, PATCH

limit_except GET {
    allow 192.168.1.0/24;
    deny all;
}   
除了GET和HEAD 之外其它方法仅允许192.168.1.0/24网段主机使用

示例10.1

nginx

限速

[root@nginx ~]# vim /etc/nginx/conf.d/test.conf 

charset utf-8;
server_tokens off;
server {
    server_name    www.a.net;
    root    /data/site1;
    limit_rate 100k;       <--限速100k
    location /about {
        root /opt/testdir;
        index test.html;
    }
    location /images {
        alias /data/images;
        try_files $uri /images/default.jpg;
    }
}

server {
    server_name    *.a.tech;
    root    /data/site2/;
}

[root@nginx ~]# nginx

centos6

再次下载发现限速

图片.png

示例10.2

nginx

[root@nginx ~]# vim /etc/nginx/conf.d/test.conf 

charset utf-8;
server_tokens off;
server {
    server_name    www.a.net;
    root    /data/site1;
    limit_rate 100k;
    location / {               <--
        limit_except GET {     <--除了GET允许
        allow 192.168.37.6;    <--允许主机
        deny all;              <--其他主机拒绝
        }                      <--
    }
    location /about {
        root /opt/testdir;
        index test.html;
    }
    location /images {
        alias /data/images;
        try_files $uri /images/default.jpg;
    }
}

server {
    server_name    *.a.tech;
    root    /data/site2/;
}

[root@nginx ~]# nginx

centos6

[root@centos6 ~]$ curl -X OPTIONS -I http://www.a.net/
HTTP/1.1 405 Not Allowed    <--
Server: nginx
Date: Sun, 07 Aug 2022 06:44:01 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 150
Connection: keep-alive
Keep-Alive: timeout=65

ngx_http_core_module (十一)

  1. 文件操作优化的配置
  2. aio on | off | threads[=pool];

是否启用aio功能,默认off

  1. directio size | off;

当文件大于等于给定大小时,同步(直接)写磁盘,而非写缓存,默认off

示例:
location /video/ {
   sendfile on;
   aio on;
   directio 8m; 
}
  1. open_file_cache off;
open_file_cache max=N [inactive=time];
   nginx可以缓存以下三种信息:
   (1) 文件元数据:文件的描述符、文件大小和最近一次的修改时间
   (2) 打开的目录结构
   (3) 没有找到的或者没有权限访问的文件的相关信息
   max=N:可缓存的缓存项上限;达到上限后会使用LRU算法实现管理
   inactive=time:缓存项的非活动时长,在此处指定的时长内未被命中的或命中的次数少于open_file_cache_min_uses指令所指定的次数的缓存项即为非活动项,将被删除
  1. open_file_cache_errors on | off;

是否缓存查找时发生错误的文件一类的信息,默认值为off

  1. open_file_cache_min_uses number;

open_file_cache指令的inactive参数指定的时长内,至少被命中此处指定的次数方可被归类为活动项,默认值为1

  1. open_file_cache_valid time;

缓存项有效性的检查频率,默认值为60s

ngx_http_access_module(访问控制)

  1. ngx_http_access_module模块

可实现基于ip的访问控制功能

  1. allow address | CIDR | unix: | all;
  2. deny address | CIDR | unix: | all;

http, server, location, limit_except 自上而下检查,一旦匹配,将生效,条件严格的置前 示例:

location /about {
   root /data/nginx/html/pc;
   index index.html;
   deny 192.168.1.1;
   allow 192.168.1.0/24;
   allow 10.1.1.0/16;
   allow 2001:0db8::/32;
   deny all;                            #先允许小部分,再拒绝大部分
}

nginx

访问控制

注意次序:如果deny在前、会把37.6也拒绝

[root@nginx ~]# vim /etc/nginx/conf.d/test.conf 
charset utf-8;
server_tokens off;
server {
    server_name    www.a.net;
    root    /data/site1;
    limit_rate 100k;
    location / {
        allow 192.168.37.6;      <--
        deny all;                   <--
    }
    location /about {
        root /opt/testdir;
        index test.html;
    }
    location /images {
        alias /data/images;
        try_files $uri /images/default.jpg;
    }
}

server {
    server_name    *.a.tech;
    root    /data/site2/;
}

[root@nginx ~]# nginx

centos6

[root@centos6 ~]$ curl -I http://www.a.net/
HTTP/1.1 200 OK        <--
Server: nginx
Date: Sun, 07 Aug 2022 07:05:42 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 23
Last-Modified: Sat, 06 Aug 2022 07:35:39 GMT
Connection: keep-alive
Keep-Alive: timeout=65
ETag: "62ee19cb-17"
Accept-Ranges: bytes

ngx_http_auth_basic_module

  1. ngx_http_auth_basic_module模块

实现基于用户的访问控制,使用basic机制进行用户认证

  1. auth_basic string | off;
  2. auth_basic_user_file file;
location /admin/ {
   auth_basic "Admin Area";
   auth_basic_user_file /etc/nginx/.ngxpasswd;
}
  • 用户口令文件:
  1. 明文文本:格式name:password:comment

  2. 加密文本:由htpasswd命令实现 httpd-tools所提供

nginx

[root@nginx ~]# yum install httpd-tools -y
#第一次创建文件加'-c'信息
[root@nginx ~]# htpasswd -b -c /etc/nginx/conf.d/.nginx_passwd alice 123.com
#注意!!!文件已经存在、往里写入不需要'-c'选项。如过加-c会被覆盖
[root@nginx ~]# htpasswd -b  /etc/nginx/conf.d/.nginx_passwd bob 123.com

[root@nginx ~]# vim /etc/nginx/conf.d/test.conf 
charset utf-8;
server_tokens off;
server {
    server_name    www.a.net;
    root    /data/site1;
    limit_rate 100k;
    location / {
    }
    location /admin {
        root /data;
        auth_basic "Admin Area";
        auth_basic_user_file /etc/nginx/conf.d/.nginx_passwd;
    }
}

server {
    server_name    *.a.tech;
    root    /data/site2/;
}

[root@nginx ~]# mkdir /data/admin
[root@nginx ~]# echo /data/admin/index.html > /data/admin/index.html
[root@nginx ~]# nginx

windows

修改hosts文件'192.168.37.7 www.a.net'

浏览器

图片.png

ngx_http_stub_status_module

  1. ngx_http_stub_status_module模块(状态页)、

用于输出nginx的基本状态信息,输出信息示例:

Active connections: 291
server accepts handled requests #下面三个数分别对应accepts,handled,requests
      16630948 16630948 31070465
Reading: 6 Writing: 179 Waiting: 106
Active connections:当前状态,活动状态的连接数
accepts:统计总值,已经接受的客户端请求的总数
handled:统计总值,已处理完成的客户端请求总数,一般和accepts相同,除非拒绝
requests:统计总值,客户端发来的总的请求数
Reading:当前状态,正在读取客户端请求报文首部的连接的连接数
Writing:当前状态,正在向客户端发送响应报文过程中的连接数
Waiting:当前状态,正在等待客户端发出请求的空闲连接数      
  1. stub_status;
示例:
location /nginx_status {
   stub_status;
   allow 127.0.0.1;
   allow 172.16.0.0/16;
   deny all;
}

nginx

[root@nginx ~]# vim /etc/nginx/conf.d/test.conf 

charset utf-8;
server_tokens off;
server {
    server_name    www.a.net;
    root    /data/site1;
    limit_rate 100k;
    location / {
    }
    location /nginx_status {       <--
        stub_status;               <--
        allow 127.0.0.1;              <--
        allow 192.168.37.0/24;                   <--
        deny all;                     <--
    }                                <--
    location /admin {
        root /data;
        auth_basic "Admin Area";
        auth_basic_user_file /etc/nginx/conf.d/.nginx_passwd;
    }
}

server {
    server_name    *.a.tech;
    root    /data/site2/;
}

windows

图片.png

nginx 第三方模块

  1. 第三模块是对nginx 的功能扩展,第三方模块需要在编译安装Nginx 的时候使用参数--add-module=PATH指定路径添加,有的模块是由公司的开发人员针对业务需求定制开发的,有的模块是开源爱好者开发好之后上传到github进行开源的模块,nginx支持第三方模块需要从源码重新编译支持
  2. 开源的echo模块,实现输出变量等信息

github.com/openresty/e…

  1. 示例:
yum install git -y
git clone https://github.com/openresty/echo-nginx-module.git
cd nginx-1.16.0/
useradd -r -s /sbin/nologin nginx
yum install gcc pcre-devel openssl-devel zlib-devel -y

nginx10

[root@nginx10 ~]# yum install git -y
[root@nginx10 ~]# cd /usr/local/src/
#克隆
[root@rip10 src]# git clone https://github.com/openresty/echo-nginx-module.git
[root@rip10 src]# ls
echo-nginx-module  nginx-1.16.1  nginx-1.16.1.tar.gz

#重新编译
[root@rip10 src]# cd nginx-1.16.1/
[root@rip10 nginx-1.16.1]# ./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --add-module=/usr/local/src/echo-nginx-module

[root@rip10 nginx-1.16.1]# make && make install

[root@rip10 nginx-1.16.1]# vim /apps/nginx/conf/nginx.conf
...
        #access_log  logs/host.access.log  main;
        location /echo {    <--
            default_type text/plain;    <--加上此项、可在浏览器直接打开、负责会默认下载页面
            echo hello;       <--屏幕上打印'hello'
            echo $remote_addr;    <--显示自己ip地址
        }                          <--

        location / {
...

#启动服务
[root@rip10 nginx-1.16.1]# nginx
#重新加载服务
[root@rip10 nginx-1.16.1]# nginx -s reload

centos6

修改hosts文件'192.168.37.10 www.test.com'

[root@centos6 ~]$ vim /etc/hosts
192.168.37.10 www.test.com

[root@centos6 ~]$ curl 192.168.37.10/echo
hello
192.168.37.6

[root@centos6 ~]$ curl www.test.com/echo
hello
192.168.37.6

windows 修改hosts文件'192.168.37.10 www.test.com'

图片.png

nginx10

测试返回路径花的时间

[root@rip10 nginx-1.16.1]# vim /apps/nginx/conf/nginx.conf
···
        location /test {
            index index.html;
            default_type text/html;
            echo "hello world,main-->";
            echo_reset_timer;
            echo_location /sub1;
            echo_location /sub2;
            echo "took $echo_timer_elapsed sec for total.";
        }
        location /sub1 {
            echo_sleep 1;
            echo sub1;
        }
        location /sub2 {
            echo_sleep 1;
            echo sub2;
        }
···

centos6

[root@centos6 ~]$ curl 192.168.37.10/test
hello world,main-->
sub1
sub2
took 2.003 sec for total.

windows

图片.png