nginx--第三方模块安装&&上传下载服务

 第三方模块安装

准备

cd /usr/local/src/
yum install git -y
git clone https://github.com/openresty/echo-nginx-module.git
cd nginx-1.24.0
yum -y install perl-devel perl-ExtUtils-Embed zlib-devel gcc-c++ libtool openssl openssl-devel

编译安装

 ./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 \
--with-http_perl_module \
--add-module=/usr/local/src/echo-nginx-module-master 模块路径

make && make install

配置演示

[root@localhost nginx-1.24.0]# cat /apps/nginx/conf/conf.d/pc.conf 
server {
  listen 80;
  server_name www.fxq.com;
  location / {
     root /data/nginx/html/pc;
     index index.html;
  }
  location /main {
     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;
  }

}

传送变量

[root@localhost nginx-1.24.0]# cat /apps/nginx/conf/conf.d/pc.conf 
server {
  listen 80;
  server_name www.fxq.com;
  location / {
     root /data/nginx/html/pc;
     index index.html;
  }
  location /m78 {
     default_type text/html;
     echo $remote_addr;
 
  }

}

下载服务

[root@localhost ~]# cat /apps/nginx/conf/conf.d/pc.conf 
server {
  listen 80;
  server_name www.fxq.com;
  location / {
     root /data/nginx/html/pc;
     index index.html;
  }
  location /download {
     root /data/nginx/html/pc;  
     autoindex on;     #自动索引功能
     autoindex_exact_size on;#计算文件确切大小(单位bytes),off只显示大概大小(单位kb、mb、gb)
     autoindex_localtime on; #显示本机时间而非GMT(格林威治)时间
     limit_rate  20k; #限制响应给客户端的传输速率,单位是bytes/second,默认值0表示无限制限速与不不限速的对⽐:
  }

}

 上传服务

可配置http,server,location

[root@localhost ~]# cat /apps/nginx/conf/conf.d/pc.conf 
server {
  listen 80;
  server_name www.fxq.com;
  location / {
     root /data/nginx/html/pc;
     index index.html;
     client_max_body_size 100G;  #设置允许客户端上传单个文件的最大值,默认值为1m
     client_body_buffer_size 100k; #用于接收每个客户端请求报文的body部分的缓冲区大小;默认16k;超出此⼤小时,其将被暂存到磁盘上的由下面client_body_temp_path指令所定义的位置
     client_body_temp_path /apps/nginx/logs 1 2 3; #设定存储客户端请求报文的body部分的临时存路径及子目录结构和数量,目录名为16进制的数字,使用hash之后的值从后往前截取1位、2位、2位作为⽂文件名:
  }
}

其他配置

keepalive_disable none | browser ...;#对哪种浏览器禁用长连接

limit_except method ... { ... },仅用于location限制客户端用除了指定的请求方法之外的其它方法

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

[root@localhost ~]#  cat  /apps/nginx/conf/conf.d/pc.conf 
server {
  listen 80;
  server_name www.fxq.com;
  location / {
     root /data/nginx/html/pc;
     index index.html;
  }
  location /upload {
    root /data/nginx/html/pc;
    index index.html;
    limit_except GET{
    allow 192.168.33.179;
    deny all;
    }
  }
}

 允许上传

 不允许上传

 aio

aio on | off #是否启用asynchronous file I/O(AIO)功能,需要编译开启
linux 2.6以上内核提供以下几个系统调用来支持aio:
1、SYS_io_setup:建立aio 的context
2、SYS_io_submit: 提交I/O操作请求
3、SYS_io_getevents:获取已完成的I/O事件
4、SYS_io_cancel:取消I/O操作请求
5、SYS_io_destroy:毁销aio的context

 directio

directio size | off; #directio是直接磁盘IO,默认为关闭,directio的设计初衷,它具备sendfile的基本原理,只是不使用内核cache,而是直接使用DMA,当某个文件大于等于给定大小时会生效,小于此值会使用sendfile,例如directio 4m,直接I/O最大的优点就是减少操作系统缓冲区和用户地址空间的拷贝次数,降低了了CPU的开销和内存带宽,主要使用在磁盘上有较大文件的场合,比如视频、音频等

 DMA数据传输过程

DMA数据传输过程:
1.DMA请求
     CPU对DMA控制器初始化,并向I/O接口发出操作命令,I/O接口提出DMA请求。
2.DMA响应:
     DMA控制器通知I/O接口开始DMA传输。
3.DMA传输:
     DMA控制器获得总线控制权后,CPU不再进行数据拷贝,由DMA控制器输出读写命令,直接控制内存与I/O接口进行数据传输。
4.DMA结束:
     完成数据传输后,DMA控制器即释放总线控制权,并向I/O接口发出结束信号并通知CPU,CPU继续处理请求.
总结:
      DMA传输不需要CPU直接参与数据传输过程,也没有将CPU阻塞在数据拷⻉的过程中,而是通过硬件内存与I/O设备建立一条直接传送数据的通路,在数据传输过程中CPU基础处理其他请求,使CPU的工作效率大为提高。

 参数

directio_alignment 512; #设置directio的对齐方式,在大多数情况下,一个512字节的对⻬齐就足够了了,但是,在Linux下使用XFS时,需要将其增加到4K。

directio可以和sebdfile结合使用不冲突,在Linux上同时启用 AIO和sendfile时,AIO用于大于或等于directio指令中指定的大小的文件,而sendfile用于较小的文件或禁用directio的文
件,directio需要nginx开启aio,如:
location /video {
root /data/video;
index index.html;
sendfile on;
aio on;
directio 8m;
directio_alignment 512;
}

 线程

开启多线程,定义多线程读取和发送文件而不会阻塞

http://nginx.org/en/docs/http/ngx_http_core_module.html#aio

编译安装

./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 \
--with-http_perl_module \
--with-file-aio \
--add-module=/usr/local/src/echo-nginx-module-master \
--with-threads

 nginx配置

events {
    worker_connections  1024;
    use epoll; 
    accept_mutex on;
    multi_accept on;
}
thread_pool pool1 threads=16 max_queue=65535; 添加线程池
thread_pool pool2 threads=32 max_queue=65535;
[root@localhost conf]# cat /apps/nginx/conf/conf.d/pc.conf
server {
  listen 80;
  server_name www.fxq.com;
  location / {
     root /data/nginx/html/pc;
     index index.html;
  }
  location /video {
    root /data/nginx/html/pc;
    index index.html;
    sendfile on;
    aio threads=pool1;
    directio 8m;
    directio_alignment 4096;
  }
}

相关推荐

  1. python之模块

    2024-05-03 22:14:02       17 阅读
  2. Python安装库出错

    2024-05-03 22:14:02       38 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-03 22:14:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-03 22:14:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-03 22:14:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-03 22:14:02       20 阅读

热门阅读

  1. 【leetcode】优先队列题目总结

    2024-05-03 22:14:02       11 阅读
  2. P1496 火烧赤壁(离散化)

    2024-05-03 22:14:02       12 阅读
  3. 【MyBatisPlus 分页插件解析】

    2024-05-03 22:14:02       9 阅读
  4. 静态库和动态库

    2024-05-03 22:14:02       9 阅读
  5. 项目经理-简历描述

    2024-05-03 22:14:02       8 阅读
  6. python实现的堆排序

    2024-05-03 22:14:02       11 阅读
  7. 【Python快速上手(十一)】

    2024-05-03 22:14:02       11 阅读
  8. 牛客面试1

    2024-05-03 22:14:02       11 阅读
  9. QT-this关键字

    2024-05-03 22:14:02       10 阅读
  10. 设计模式:建造者模式

    2024-05-03 22:14:02       10 阅读
  11. Visual Studio C++ 的一个简单示例

    2024-05-03 22:14:02       11 阅读
  12. 模拟退火算法matlab代码

    2024-05-03 22:14:02       12 阅读