HanG321 Blog

upload file on nginx – part 1: compile dynamic modules

Recently  I was assigned to a task about adding a file upload feature to a existing nginx server on CentOS. Nginx was chosen years ago because it simply handles some static files only. If the file upload was a requirement, using apache is simpler as computation power isn’t a constraint in our case.

Background

  1. CentOS 7
  2. nginx 1.10.2
  3. nginx-upload-module
  4. ngx_http_auth_pam_module

Compile Dynamic modules

Download upload and auth pam module source code. Then find out the current nginx version, download the source code and compile modules

## download module source code
$ wget https://github.com/sto/ngx_http_auth_pam_module/archive/master.zip -O ngx_http_auth_pam_module.zip
$ unzip ngx_http_auth_pam_module.zip
$ mv ngx_http_auth_pam_module-master ngx_http_auth_pam_module

$ wget https://github.com/vkholodkov/nginx-upload-module/archive/master.zip -O nginx-upload-module.zip
$ upzip nginx-upload-module.zip
$ mv nginx-upload-module-master nginx-upload-module
## modify the "config" file to support dynamic module,
## see https://github.com/vkholodkov/nginx-upload-module/issues/78

## install compile dependencies
$ sudo yum -y install pcre-devel zlib-devel pam-devel  libxslt-devel perl-ExtUtils-Embed GeoIP-devel \
 redhat-rpm-config openssl-devel gd-devel gperftools-devel gcc-c++

## find out the current version of nginx
$ nginx -v

## download source code
$ wget http://nginx.org/download/nginx-1.10.2.tar.gz

## list all compile parameters
$ nginx -V
## e.g.
configure arguments:
--prefix=/usr/share/nginx
--sbin-path=/usr/sbin/nginx
--modules-path=/usr/lib64/nginx/modules
--conf-path=/etc/nginx/nginx.conf
--error-log-path=/var/log/nginx/error.log
--http-log-path=/var/log/nginx/access.log
--http-client-body-temp-path=/var/lib/nginx/tmp/client_body
--http-proxy-temp-path=/var/lib/nginx/tmp/proxy
--http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi
--http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi
--http-scgi-temp-path=/var/lib/nginx/tmp/scgi
--pid-path=/run/nginx.pid
--lock-path=/run/lock/subsys/nginx
--user=nginx
--group=nginx
--with-file-aio
--with-ipv6
--with-http_ssl_module
--with-http_v2_module
--with-http_realip_module
--with-http_addition_module
--with-http_xslt_module=dynamic
--with-http_image_filter_module=dynamic
--with-http_geoip_module=dynamic
--with-http_sub_module
--with-http_dav_module
--with-http_flv_module
--with-http_mp4_module
--with-http_gunzip_module
--with-http_gzip_static_module
--with-http_random_index_module
--with-http_secure_link_module
--with-http_degradation_module
--with-http_slice_module
--with-http_stub_status_module
--with-http_perl_module=dynamic
--with-mail=dynamic
--with-mail_ssl_module
--with-pcre
--with-pcre-jit
--with-stream=dynamic
--with-stream_ssl_module
--with-google_perftools_module
--with-debug
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong
  --param=ssp-buffer-size=4 -grecord-gcc-switches
  -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic'
--with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'

## reuse the configs output from "nginx -V", now compile http auth pam module
$ tar zxvf nginx-1.10.2.tar.gz
$ cd nginx-1.10.2
$ ./configure --prefix=/usr/share/nginx ........... -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E' \
 --add-dynamic-module=../ngx_http_auth_pam_module
$ make modules
## upload to to server
$ scp objs/ngx_http_auth_pam_module.so user@host:/usr/lib64/nginx/modules/

## repeat for nginx-upload-module
$ ./configure --prefix=/usr/share/nginx ........... -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E' \
 --add-dynamic-module=../nginx-upload-module
$ make modules
$ scp objs/ngx_http_upload_module.so user@host:/usr/lib64/nginx/modules/

## create configs
echo 'load_module "/usr/lib64/nginx/modules/ngx_http_auth_pam_module.so";' >  /usr/share/nginx/modules/mod-http-auth-pam.conf
echo 'load_module "/usr/lib64/nginx/modules/ngx_http_upload_module.so";' >  /usr/share/nginx/modules/mod-http-upload.conf

## to validate config
$ nginx -tc /etc/nginx/nginx.conf

Exit mobile version