起因
笔者最近搭建了一个12306验证码识别网站,但是发现返回Headers的信息特别多,这样会涉及到服务器的安全问题,于是笔者想屏蔽掉一些Response Headers。Nginx屏蔽Headers需要添加第三方模块,由于笔者当时直接apt install nginx,所以需要重新编译安装。故记录于此。
准备工具
- nginx 源码 http://nginx.org/download/nginx-1.15.8.tar.gz
- 第三方模块 https://github.com/openresty/headers-more-nginx-module
开始
下载Nginx源码
wget http://nginx.org/download/nginx-1.15.8.tar.gz
tar zxvf nginx-1.15.8.tar.gz
cd nginx-1.15.8
下载headers-more-nginx-module
wget https://github.com/openresty/headers-more-nginx-module/archive/v0.33.tar.gz
tar xzvf v0.33.tar.gz
如果已经安装Nginx请先卸载
apt remove nginx-mod*
配置参数
./configure --add-module=./headers-more-nginx-module-0.33/ --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --sbin-path=/usr/sbin/nginx --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 --prefix=/usr --modules-path=/usr/lib/nginx/modules/
然后报错
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
提示需要依赖 PCRE library 模块
apt-get install libpcre3-dev
继续报错
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.
提示需要依赖 OpenSSL 模块
apt-get install libssl-dev
编译and安装
make install
通过配置 Server
Server {
...
#清除 X-Powered-By
more_clear_headers "X-Powered-By:";
#设置 Server
more_set_headers "Server: Naonao High Performance Web Server V1.0";
...
}
然后重启Nginx
Good luck~

网友评论