惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Cloudflare Blog
L
LangChain Blog
WordPress大学
WordPress大学
V
V2EX
M
MIT News - Artificial intelligence
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Stack Overflow Blog
Stack Overflow Blog
J
Java Code Geeks
F
Fortinet All Blogs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
腾讯CDC
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
D
Docker
Recent Announcements
Recent Announcements
GbyAI
GbyAI
博客园 - 叶小钗
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
T
The Blog of Author Tim Ferriss
人人都是产品经理
人人都是产品经理
Engineering at Meta
Engineering at Meta
Y
Y Combinator Blog
雷峰网
雷峰网
The GitHub Blog
The GitHub Blog

博客园 - ABeen

[Database] Mongodb 分片集群 [Linux] vim状态栏配置 [Linux] Centos 添加开机启动脚本 [Linux] Docker 镜像及容器自动化思路 [Linux]Docker 容器时间与宿主机同步 [Linux] Tomcat java.lang.OutOfMemoryError: Java heap space [Linux] 非云环境的Ubuntu主机如何关闭Cloud-init [DataBase] MySql 查看实时日志 [Misc] ZSH 常用快捷键 [Database] MAC MySQL中文乱码问题 [Misc] python 开发vim 插件初步测试 [Tools] 多媒体视频处理工具FFmpeg [Linux] Ubuntu Server18 python3.7 虚拟环境 [Linux] 树莓派编译python3.7.4 [Linux] TMUX Python版本设置 Linux 批量杀进程的命令 arm树莓派Raspbian 下安装selenium+chrome 树莓派Raspbian系统密码 mac 终端查看端口命令
[Linux] Nginx 反向代理配置 http headers 带下划线fields转发
ABeen · 2020-10-25 · via 博客园 - ABeen

场景

应用中有个场景, web客户端提交数据时,在http header中加入某个字段. 如client_add_field.

在用ip访问时,字段可以正常获取. 改用域名访问时, 字段获取不到.

self.request.headers.get('access_token', '')

解决

Nginx在做反向代理时,一般会设置host和ip,如果你的请求的headers里有值,它是可以同时转发过去的,但是,默认情况下,并不是所有headers的fields它都会转发,fields里带有下划线(_)的,Nginx视为不合法,自动抛弃不发了。

文档:

Syntax: underscores_in_headers on | off;
Default: underscores_in_headers off;
Context: http, server

Enables or disables the use of underscores in client request header fields. When the use of underscores is disabled, request header fields whose names contain underscores are marked as invalid and become subject to the ignore_invalid_headers directive.

想要支持下划线(_)的headers fields,需要将underscores_in_headers设置为on。

但是,如果只是设置它,会发现,并没用,因为还要设置一项:

proxy_pass_request_headers on;

    underscores_in_headers on;  // 允许headers fields 里的下划线

    location / {
        proxy_pass_request_headers on; // 确定转化http headers

        proxy_pass_header Server;
        proxy_redirect off;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_connect_timeout 60s;
        proxy_read_timeout 5400s;
        proxy_send_timeout 5400s;
        proxy_pass http://pb_web;
    }