支持版本列表

  • 1.17.x (last tested: 1.17.8)
  • 1.16.x
  • 1.15.x (last tested: 1.15.8)
  • 1.14.x
  • 1.13.x (last tested: 1.13.6)
  • 1.12.x
  • 1.11.x (last tested: 1.11.2)
  • 1.10.x
  • 1.9.x (last tested: 1.9.15)
  • 1.9.11(从这个版本开始不用重启nginx
  • 1.8.x
  • 1.7.x (last tested: 1.7.10)
  • 1.6.x (last tested: 1.6.2)
  • 1.5.x (last tested: 1.5.8)
  • 1.4.x (last tested: 1.4.4)
  • 1.3.x (last tested: 1.3.7)
  • 1.2.x (last tested: 1.2.9)
  • 1.1.x (last tested: 1.1.5)
  • 1.0.x (last tested: 1.0.11)
  • 0.9.x (last tested: 0.9.4)
  • 0.8.x (last tested: 0.8.54)
  • 0.7.x >= 0.7.44 (last tested: 0.7.68)

操作步骤

root@04d85a8c300f /var/www/html# nginx -v
nginx version: nginx/1.16.0

下载nginx源码(注意版本要对应):

wget 'http://nginx.org/download/nginx-1.16.0.tar.gz'
tar -xvf nginx-1.16.0.tar.gz

下载插件代码:

git clone https://github.com/openresty/headers-more-nginx-module.git

查看nginx编译选项(必要步骤):

root@04d85a8c300f /var/www/html# nginx -V
nginx version: nginx/1.16.0
built by gcc 8.2.0 (Alpine 8.2.0) 
built with OpenSSL 1.1.1b  26 Feb 2019
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --with-perl_modules_path=/usr/lib/perl5/vendor_perl --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-Os -fomit-frame-pointer' --with-ld-opt=-Wl,--as-needed

./configure --add-dynamic-module=../headers-more-nginx-module + 复制 configure arguments 后面所有的内容,开始编译插件:

cd nginx-1.16.0/
./configure  --add-dynamic-module=../headers-more-nginx-module --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --with-perl_modules_path=/usr/lib/perl5/vendor_perl --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-Os -fomit-frame-pointer' --with-ld-opt=-Wl,--as-needed

make -j 8
make install

此时,将objs/ngx_http_headers_more_filter_module.so文件复制到对应的nginx服务器上 /usr/lib/nginx/modules/ngx_http_headers_more_filter_module.so即可。

将以下配置添加到/etc/nginx/nginx.conf 前面。

load_module /usr/lib/nginx/modules/ngx_http_headers_more_filter_module.so;

热重载nginx。

nginx -s reload

万事具备了,试一下:

location /favicon.ico {
    more_set_headers "Content-Type: image/x-icon;base64";
    return 200 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABrUlEQVQ4T52SQXLTQBREu4UD7JB9AeIqlHVugLiBuIFZRlnYOYF1A5uFnKXtG5gTIJ8gYWtR5eQCMNsQ1XTqq2oSp5KUgdnN1O83/X9/4j9OfF6n9JqYlP+ij8+3h5G/HYMcBN1fA3plXQgYkoiDWNCPvYDWrjQH2KW4EPxHECRwKfDwRYDZp
}

来看看add_header对比: