略微加速

略速 - 互联网笔记

用 Nginx 自动给 Cookie 增加 Secure 和 HttpOnly

2021-01-25 leiting (1222阅读)

标签 nginx 运维

前言

闲的蛋疼瞎折腾。。
通过Nginx保证全站HTTPS时小饼干的安全性 0.0

在 nginx 的 location 中配置


# 只支持 proxy 模式下设置,SameSite 不需要可删除,如果想更安全可以把 SameSite 设置为 Strict
proxy_cookie_path / "/; httponly; secure; SameSite=Lax";


示例


server {
    listen 443 ssl http2;
    server_name www.cat73.org;

    ssl_certificate /etc/letsencrypt/live/cat73.org/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/cat73.org/privkey.pem;

    ssl_trusted_certificate /etc/letsencrypt/live/cat73.org/chain.pem;

    add_header X-XSS-Protection "1; mode=block";
    add_header X-Frame-Options SAMEORIGIN;
    add_header Strict-Transport-Security "max-age=15768000";

    location / {
        root /var/www/html;
    }

    location /api {
        proxy_pass http://localhost:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        # 在这里设置
        proxy_cookie_path / "/; httponly; secure; SameSite=Lax";
    }
}


https://blog.cat73.org/20170802/2017080201.nginx-cookie-sucure/


北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3