nginx反向代理 清除X-Frame-Options iframe加载

@高效码农  July 29, 2019

准备环境

需要提前安装好nginx,并查看nginx已经安装的模块:

/opt/nginx-1.17.2/sbin/nginx -V

返回举例:

nginx version: nginx/1.17.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
built with OpenSSL 1.0.1j 15 Oct 2014
TLS SNI support enabled
configure arguments: --prefix=/opt/nginx-1.17.2 --with-pcre=../pcre-8.38 --with-openssl=../openssl-1.0.1j --with-zlib=../zlib-1.2.11 --with-http_ssl_module --add-module=../ngx_http_google_filter_module --add-module=../ngx_http_substitutions_filter_module --add-module=../headers-more-nginx-module-0.33
  • 需要安装的模块:headers_morengx_http_substitutions_filterngx_http_google_filteropensslzlib

修改ngingx.conf

在location中添加:

location / {
   more_clear_headers X-Frame-Options;
}

Nginx新增模块more_clear_headers

nginx默认没有安装more_clear_headers模块,会报错:nginx: [emerg] unknown directive “more_clear_headers” in

此时,我们需要追加more_clear_headers模块:

下载headers-more-nginx-module

wget https://github.com/openresty/headers-more-nginx-module/archive/v0.33.tar.gz

解压

tar -zxvf v0.33.tar.gz

生成 Makefile

/opt/nginx-1.17.2/sbin/nginx -V 
  • 换成自己的nginx 路径

返回举例:

nginx version: nginx/1.17.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
built with OpenSSL 1.0.1j 15 Oct 2014
TLS SNI support enabled
configure arguments: --prefix=/opt/nginx-1.17.2 --with-pcre=../pcre-8.38 --with-openssl=../openssl-1.0.1j --with-zlib=../zlib-1.2.11 --with-http_ssl_module --add-module=../ngx_http_google_filter_module --add-module=../ngx_http_substitutions_filter_module --add-module=../headers-more-nginx-module-0.33

拷贝返回中configure arguments后的内容,并在最前面添加./configure

举例:

./configure --prefix=/opt/nginx-1.17.2 --with-pcre=../pcre-8.38 --with-openssl=../openssl-1.0.1j --with-zlib=../zlib-1.2.11 --with-http_ssl_module --add-module=../ngx_http_google_filter_module --add-module=../ngx_http_substitutions_filter_module --add-module=../headers-more-nginx-module-0.33 

执行完成后,执行

  • 替换为自己的目录
# 编辑,切记没有make install
make
# 备份
cp /app/nginx112/sbin/nginx /app/nginx112/sbin/nginx.bak 
# 覆盖(覆盖提示输入y)
cp -f /app/nginx-1.12.2/objs/nginx /app/nginx112/sbin/nginx

修改ngingx.conf

在location中添加:

location / {
   more_clear_headers X-Frame-Options;
}

重启nginx

/opt/nginx-1.17.2/sbin/nginx -s reload


添加新评论