如何附加Nginx Cookie标签模块?

Nginx

1 基础知识

“nginx_cookie_flag_module”为上游响应标头中的cookie设置如下标志,
– HttpOnly
– SameSite
– secureSet-Cookie
注:关于以上标志提供的功能非本章重点,将由其他章节介绍。

2 最佳实践

2.1 环境配置

2.1.1 系统环境

IP Address = 10.168.0.154
Host Name = any.cmdschool.org
OS = CentOS 7.3 x86_64

2.1.2 yum源配置

yum -y install gcc gcc-c++ make expat-devel 
yum -y install rpm-build redhat-lsb
yum -y install vim wget

2.1.3 创建构建用户

useradd -u 1001 builder

2.1.4 关闭selinux

setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

2.2 构建rpm包部分

2.2.1 下载安装包

su - builder
cd ~
wget http://nginx.org/packages/mainline/centos/7/SRPMS/nginx-1.11.2-1.el7.ngx.src.rpm
wget -O nginx_cookie_flag_module-1.1.0.tar.gz https://github.com/AirisX/nginx_cookie_flag_module/archive/refs/tags/v1.1.0.tar.gz

另外,其他版本请从如下链接下载,
http://nginx.org/packages/
https://github.com/AirisX/nginx_cookie_flag_module/releases

2.2.2 解压并测试构建环境

rpm -ivh nginx-1.11.2-1.el7.ngx.src.rpm
rpmbuild -bb rpmbuild/SPECS/nginx.spec

如果看到类似提示,


error: Failed build dependencies:
        openssl-devel >= 1.0.1 is needed by nginx-1:1.11.2-1.el7.centos.ngx.x86_64
        perl-ExtUtils-Embed is needed by nginx-1:1.11.2-1.el7.centos.ngx.x86_64
        GeoIP-devel is needed by nginx-1:1.11.2-1.el7.centos.ngx.x86_64
        zlib-devel is needed by nginx-1:1.11.2-1.el7.centos.ngx.x86_64
        pcre-devel is needed by nginx-1:1.11.2-1.el7.centos.ngx.x86_64
        libxslt-devel is needed by nginx-1:1.11.2-1.el7.centos.ngx.x86_64
        gd-devel is needed by nginx-1:1.11.2-1.el7.centos.ngx.x86_64

请先解决包的依赖关系然后重试:

exit
yum -y install openssl-devel zlib-devel pcre-devel perl-ExtUtils-Embed GeoIP-devel libxslt-devel gd-devel

2.2.3 配置nginx_cookie_flag_modele模块

tar -xf nginx_cookie_flag_module-1.1.0.tar.gz
cp -a nginx_cookie_flag_module-1.1.0/ rpmbuild/BUILD/nginx_cookie_flag_module

2.2.4 修改构建文件

cd rpmbuild/SPECS
cp nginx.spec nginx.spec.orig

修改配置文件参数,

vim nginx.spec

修改包含如下关键字的行,

--with-ipv6 

增加参数后行如下,

--with-ipv6 \
--add-module=../nginx_cookie_flag_module \

2.2.5 重新构建rpm包

diff -uN nginx.spec.orig nginx.spec > nginx_cookie_flag_module.patch
patch -p0 < nginx_cookie_flag_module.patch

如果出现如下提示,按照以下向导处理,

patching file nginx.spec
Reversed (or previously applied) patch detected!  Assume -R? [n]
Apply anyway? [n] y
Hunk #1 FAILED at 120.
1 out of 1 hunk FAILED -- saving rejects to file nginx.spec.rej

继续完成重构

rpmbuild -bb nginx.spec
exit

2.3 配置Nginx服务

2.3.1 安装nginx包

cd /home/builder/rpmbuild/RPMS/x86_64/
yum -y install nginx-1.11.2-1.el7.ngx.x86_64.rpm

2.3.2 确认模块启用

nginx -V

显示如下:

nginx version: nginx/1.11.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/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 --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 --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --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_stub_status_module --with-http_auth_request_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_perl_module=dynamic --add-dynamic-module=njs-ef2b708510b1/nginx --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --add-module=../nginx_cookie_flag_module --with-http_v2_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic'

可见配置的参数后面有加载模块:

--add-module=../nginx_cookie_flag_module

2.3.3 启动服务并配置自启动

systemctl start nginx.service
systemctl enable nginx.service

2.3.4 配置防火墙

firewall-cmd --permanent --add-service http --add-service https
firewall-cmd --reload
firewall-cmd --list-all

2.3.5 增加带echo的配置

vim /etc/nginx/conf.d/default.conf

加入如下行:

server {
    [..]
    location / {
        [..]
        set_cookie_flag Secret HttpOnly secure SameSite;
        set_cookie_flag * HttpOnly;
        set_cookie_flag SessionID SameSite=Lax secure;
        set_cookie_flag SiteToken SameSite=Strict;
    }
    [..]
}

2.3.6 校验配置文件

nginx -t

如果看到以下提示则达到预期,

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

2.3.7 重启服务使配置生效

systemctl reload nginx.service

参阅文档
======================
https://github.com/AirisX/nginx_cookie_flag_module
https://docs.nginx.com/nginx/admin-guide/dynamic-modules/cookie-flag/

没有评论

发表回复

Nginx
如何解决https请求不安全http页被阻止问题?

1 前言 一个问题,一篇文章,一出故事。 笔者最近代理公司应用,发现https的页面有请求不安全的h …

Nginx
如何Nginx代理上游的子项目或文件夹?

1 前言 一个问题,一篇文章,一出故事。 笔者接到任务需要把Tomcat的其中一个应用号使用Ngin …

Nginx
如何安装部署RHEL 9 Nignx?

1 前言 一个问题,一篇文章,一出故事。 笔者需要基于RHEL 9部署Nginx环境,于是整理此文。 …