安装环境:centos 7

  1. 下载 nginx 源码并解压

    1
    2
    3
    wget http://nginx.org/download/nginx-1.12.2.tar.gz
    tar -zxvf nginx-1.12.2.tar.gz
    cd nginx-1.12.2
  2. 准备编译环境

    1
    yum install -y gcc automake autoconf libtool make
  3. 安装依赖,pcre、openssl、zlib,也可手动编译安装

    1
    yum install -y pcre pcre-devel zlib zlib-devel openssl open-ssl
  4. 配置编译

    1
    2
    3
    4
    5
    ./configure --sbin-path=/usr/local/nginx/nginx \
    --conf-path=/usr/local/nginx/conf/nginx.conf \
    --pid-path=/usr/local/nginx/nginx.pid \
    --with-http_ssl_module \
    --with-http_gzip_static_module

常用编译选项说明

  • –prefix=PATH : 指定nginx的安装目录。默认 /usr/local/nginx
  • –conf-path=PATH : 设置nginx.conf配置文件的路径。nginx允许使用不同的配置文件启动,通过命令行中的-c选项。默认为prefix/conf/nginx.conf
  • –user=name: 设置nginx工作进程的用户。安装完成后,可以随时在nginx.conf配置文件更改user指令。默认的用户名是nobody。–group=name类似
  • –with-pcre : 设置PCRE库的源码路径,如果已通过yum方式安装,使用–with-pcre自动找到库文件。使用–with-pcre=PATH时,需要从PCRE网站下载pcre库的源码(版本4.4 – 8.30)并解压,剩下的就交给Nginx的./configure和make来完成。perl正则表达式使用在location指令和 ngx_http_rewrite_module模块中。
  • –with-zlib=PATH : 指定 zlib(版本1.1.3 – 1.2.5)的源码解压目录。在默认就启用的网络传输压缩模块ngx_http_gzip_module时需要使用zlib 。
  • –with-http_ssl_module : 使用https协议模块。默认情况下,该模块没有被构建。前提是openssl与openssl-devel已安装
  • –with-http_stub_status_module : 用来监控 Nginx 的当前状态
  • –with-http_realip_module : 通过这个模块允许我们改变客户端请求头中客户端IP地址值(例如X-Real-IP 或 X-Forwarded-For),意义在于能够使得后台服务器记录原始客户端的IP地址
  • –add-module=PATH : 添加第三方外部模块,如nginx-sticky-module-ng或缓存模块。每次添加新的模块都要重新编译(Tengine可以在新加入module时无需重新编译)
  1. 编译安装

    1
    make && make install
  2. 设置软链接

    1
    ln -s /usr/local/nginx/nginx /use/sbin/nginx

开始 nginx-upload-module 模块

  1. nginx 支持文件上传,先看 nginx wiki
  2. 按照wiki说明下载 nginx-upload-module 进行编译安装,此处有bug,详见 isuse

    1
    2
    3
    4
    5
    6
    7
    git https://github.com/fdintino/nginx-upload-module.git
    cd nginx-upload-module/
    git checkout 2.2
    git reset --hard
    git clean -f
    git pull
    git status
  3. 配置编译

    1
    2
    3
    4
    5
    6
    ./configure --sbin-path=/usr/local/nginx/nginx \
    --conf-path=/usr/local/nginx/conf/nginx.conf \
    --pid-path=/usr/local/nginx/nginx.pid \
    --with-http_ssl_module \
    --with-http_gzip_static_module \
    --add-module=/data/nginx/nginx-upload-module

附录

  • nginx-upload-module 配置说明
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
upload_pass 指明后续处理的地址。文件中的字段将被分离和取代,包含必要的信息处理上传文件。
upload_resumable 是否启动可恢复上传(分片断点续传)
upload_store 指定上传文件存放地址(目录)。目录可以散列,在这种情况下,在nginx启动前,所有的子目录必须存在。
upload_state_store 指定保存上传文件可恢复上传的文件状态信息目录。目录可以散列,在这种情况下,在nginx启动前,所有的子目录必须存在。
upload_store_access 上传文件的访问权限,user:r是指用户可读,user:rw指用户可读写
upload_pass_form_field 从表单原样转到后端的参数,可以正则表达式表示。:
$upload_field_name — 原始文件中的字段的名称
upload_pass_form_field “^submit$|^description$”;
意思是把submit,description这两个字段也原样通过upload_pass传递到后端处理。如果希望把所有的表单字段都传给后端可以用upload_pass_form_field “^.*$”;
upload_set_form_field 名称和值都可能包含以下特殊变量:
$upload_field_name 表单的name值
$upload_content_type 上传文件的类型
$upload_file_name 客户端上传的原始文件名称
$upload_tmp_path 文件上传后保存在服务端的位置
upload_aggregate_form_field 可以多使用的几个变量,文件接收完毕后生成的并传递到后端
$upload_file_md5 文件的MD5校验值
$upload_file_md5_uc 大写字母表示的MD5校验值
$upload_file_sha1 文件的SHA1校验值
$upload_file_sha1_uc 大写字母表示的SHA1校验值
$upload_file_crc32 16进制表示的文件CRC32值
$upload_file_size 文件大小
$upload_file_number 请求体中的文件序号
这些字段值是在文件成功上传后计算的。
upload_cleanup 如果出现400 404 499 500-505之类的错误,则删除上传的文件
upload_buffer_size 上传缓冲区大小
upload_max_part_header_len 指定头部分最大长度字节。
upload_max_file_size 指定上传文件最大大小,软限制。client_max_body_size硬限制。
upload_limit_rate 上传限速,如果设置为0则表示不限制。
upload_max_output_body_len 超过这个大小,将报403错(Request entity too large)。
upload_tame_arrays 指定文件字段名的方括号是否删除
upload_pass_args 是否转发参数。