官方文档:http://nginx.org/en/docs/http/ngx_http_headers_module.html

1,查看当前nginx版本及确认安装目录

cd /home/oldboy/tools/
wget -q http://nginx.org/download/nginx-1.8.1.tar.gz
tar xf nginx-1.8.1.tar.gz
cd nginx-1.8.1
 

 版本及目前使用的模块信息

[root@web01 extra]# /application/nginx/sbin/nginx -V
nginx version: nginx/1.8.1
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013 TLS SNI support enabled configure arguments: --with-http_stub_status_module --with-http_ssl_module --prefix=/application/nginx-1.8.1/ ###注意一定要事先查看当前nginx下有哪些编译模块,记录,然后与要添加的模块一起编译
 

2,下载headers模块

cd /home/qiuyuetao/
wget https://codeload.github.com/openresty/headers-more-nginx-module/zip/master
unzip headers-more-nginx-module-master.zip
 

3,记录下当前访问页面的状态(也可以在web页面上查看 响应头信息)

[root@web01 qiuyuetao]# curl -I www.etiantian.org
HTTP/1.1 301 Moved Permanently
Server: nginx/1.2.9
Date: Wed, 02 Aug 2017 06:17:47 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: 
###这里可以修改内容很多,只是用到里面的一点点知识,其它内容,大家可以科普##
 

4,将header模块,编译到nginx中去

cd /home/qiuyuetao/tools/nginx-1.8.1    ##这个下载的安装文件目录
./configure --with-http_stub_status_module  --with-http_ssl_module --prefix=/application/nginx-1.8.1/ --add-module=/home/qiuyuetao/headers-more-nginx-module-master
###注意一定要事先查看当前nginx下有哪些编译模块,记录,然后与要添加的模块一起编译## make
 

(如果现在已经部署了nginx 就不能make install ,如果没有部署nginx 可以make install)

5,替换nginx启动文件   (yo)

cd  /application/nginx/sbin
mv nginx{,.bak}
cp nginx /application/nginx/sbin/ ##这个是将重新编译的nginx 配置文件,复制到安装目录使新的模块生效
 

6,重启服务,测试是否正常

/application/nginx/sbin/nginx -s stop
/application/nginx/sbin/nginx
/application/nginx/sbin/nginx -V
 

7,验证效果

[root@web01 qiuyuetao]# /application/nginx/sbin/nginx -V
nginx version: nginx/1.8.1
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013 TLS SNI support enabled configure arguments: --with-http_stub_status_module --with-http_ssl_module --prefix=/application/nginx-1.8.1/ --add-module=/home/qiuyuetao/headers-more-nginx-module-master
 

8,添加到nginx 指定站点配置文件中

server{
        listen  80;
        server_name www.etiantian.org;
        location / { ##将下面header信息写入location下 ## max-age设定缓存时间 nocashe 就是不缓存 add_header Cache-Control no-cache; add_header Cache-Control no-store; add_header Pragma no-cache; add_header Expires 0; }
 

9,重新加载

 /application/nginx/sbin/nginx -s stop
 /application/nginx/sbin/nginx
 

 ##web页面查看结果##

wKioL1mBlOmBnn5MAAAVRrLhyb8347.png