如何设置Nginx Secure Cookie?

Nginx
1.1 Secure属性的介绍

上一个章节整理了Cookie HttpOnly属性,详细如下,

如何设置Nginx HTTP-only Cookie?


HttpOnly属性可以确保Cookie被安全发送且不会被意外参与者或脚本截获,同样的,Secure属性也有同样的功能。

1.1 Secure属性的功能

– Secure的Cookie只能通过HTTPS协议加密请求发送给服务端(可防止man-in-the-middle攻击)
– Secure只能保护传输中的Cookie,但不能防止从客户端读取Cookie
注:从Chrome 52和Firefox 52开始,非https站点无法使用Cookie的Secure标记

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 构建nginx_cookie_flag_module

由于开源版本尚未包含该模块,所以需要执行构建,

如何附加Nginx Cookie标签模块?

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.4 配置后端PHP环境

2.4.1 安装PHP软件包

yum install -y php-fpm php

2.4.2 启动PHP服务

systemctl start php-fpm.service
systemctl enable php-fpm.service

2.4.3 配置PHP站点

vim /etc/nginx/conf.d/www.cmdschool.org_443.conf

加入如下配置,

server {
    listen 443;
    server_name www.cmdschool.org;
    ssl on;
    ssl_certificate 1_www.cmdschool.org_bundle.crt;
    ssl_certificate_key 2_www.cmdschool.org.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384;
    ssl_prefer_server_ciphers on;

    access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /var/www/www.cmdschool.org;
        index  index.html index.htm index.php;
    }

    location ~ \.php$ {
        root           /var/www/www.cmdschool.org;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        /etc/nginx/fastcgi_params;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

2.4.4 增加PHP测试代码

echo '<?php phpinfo(); ?>' > /var/www/www.cmdschool.org/index.php

2.4.5 配置PHP客户端(Windows)

notepad \Windows\System32\drivers\etc\hosts

加入如下配置,

10.168.0.154 www.cmdschool.org

2.4.6 测试PHP运行

http://www.cmdschool.org/phpinfo.php
如果显示如下图,则PHP能正常运行,

2.5 后端代码测试Secure Cookie

2.5.1 配置Cookie代码

vim /var/www/www.cmdschool.org/index.php

加入如下产生Cookie的代码,

<?php
// set the cookies
setcookie("cookie[three]", "cookiethree");
setcookie("cookie[two]", "cookietwo");
setcookie("cookie[one]", "cookieone");

// after the page reloads, print them out
if (isset($_COOKIE['cookie'])) {
    foreach ($_COOKIE['cookie'] as $name => $value) {
        $name = htmlspecialchars($name);
        $value = htmlspecialchars($value);
        echo "$name : $value <br />\n";
    }
}
?>

2.5.2 测试Cookie代码

http://www.cmdschool.org/index.php
访问以上网址,可见如下显示,

如上图所示,
按下【F12】->【Application】->【Cookies】->【http://www.cmdschool.org】
可注意到以上标红部分Secure没有被勾选,证明该标签没有生效。

2.5.3 增加Secure配置

vim /etc/nginx/conf.d/www.cmdschool.org_80.conf
server {
    #...
    set_cookie_flag * secure;
    #...
}

2.5.4 校验配置文件

nginx -t

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

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

2.5.5 重启服务使配置生效

systemctl reload nginx.service

2.5.6 再次测试Cookie代码

http://www.cmdschool.org/index.php
访问以上网址,可见如下显示,

如上图所示,
按下【F12】->【Application】->【Cookies】->【http://www.cmdschool.org】
可注意到以上标红部分Secure已被勾选,证明该标签已经生效。

2.6 前端代码测试Secure Cookie

2.6.1 增加前端代码

vim /var/www/www.cmdschool.org/index.html

加入如下产生Cookie的代码,

<script>
// set the cookies
document.cookie="three=cookiethree";
document.cookie="two=cookietwo";
document.cookie="one=cookieone";

// after the page reloads, print them out
var str = document.cookie;
var arr = str.split(";");
for(var i=0;i<arr.length;i++){
        var value = arr[i].split("=");
        document.write(value[0]+" : "+value[1]+"</br>");
}
</script>

注:此处默认之前已经设置Secure Cookie标签

2.6.2 测试前端代码

http://www.cmdschool.org/index.html
访问以上网址,可见如下显示,

如上图所示,
按下【F12】->【Application】->【Cookies】->【http://www.cmdschool.org】
可注意到以上标红部分Secure没有被勾选,证明该标签对前端代码无影响。

参阅文档
=================
https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Cookies
https://docs.nginx.com/nginx/admin-guide/dynamic-modules/cookie-flag/
https://github.com/AirisX/nginx_cookie_flag_module

没有评论

发表评论

Nginx
如何设置Nginx HTTP-only Cookie?

1 基础知识 1.1 HTTP cookies 1.1.1 HTTP cookies的概念 &#82 …

Nginx
如何附加Nginx Cookie标签模块?

1 基础知识 “nginx_cookie_flag_module”为上游响应标头中的cookie设置 …

Nginx
如何设置Nginx的HSTS?

1 基础知识 1.1 HTTPS的概念 – HTTPS是保护网站流量的重要措施(使用SS …