在nginx中添加和使用头(HTTP)
我使用两个系统(都是Nginx负载均衡器,一个作为备份) 我想添加和使用一些HTTP自定义头 下面是我的两个代码在nginx中添加和使用头(HTTP),http,nginx,Http,Nginx,我使用两个系统(都是Nginx负载均衡器,一个作为备份) 我想添加和使用一些HTTP自定义头 下面是我的两个代码 upstream upstream0 { #list of upstream servers server backend:80; server backup_load_balancer:777 backup; #healthcheck } server { listen 80; #Add custom header about th
upstream upstream0 {
#list of upstream servers
server backend:80;
server backup_load_balancer:777 backup;
#healthcheck
}
server {
listen 80;
#Add custom header about the port and protocol (http or https)
server_name _;
location / {
# is included since links are not allowed in the post
proxy_pass "http://upstream0;"
}
}
备份系统
server {
listen 777;
server_name _;
#doing some other extra stuff
#use port and protocol to direct
}
如何实现这一点?要添加标题,只需将以下代码添加到要添加标题的位置块:
location some-location {
add_header X-my-header my-header-content;
}
显然,用您想要添加的内容替换x-my-header和my-header内容。这就是它的全部内容。您可以使用上游头(以$http\ux开头命名)和其他自定义头。例如:
add_header X-Upstream-01 $http_x_upstream_01;
add_header X-Hdr-01 txt01;
接下来,转到控制台并使用用户标题发出请求:
curl -H "X-Upstream-01: HEADER1" -I http://localhost:11443/
响应包含由服务器设置的X-Hdr-01和由客户端设置的X-上游-01:
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Mon, 30 Nov 2015 23:54:30 GMT
Content-Type: text/html;charset=UTF-8
Connection: keep-alive
X-Hdr-01: txt01
X-Upstream-01: HEADER1
谢谢如何在我的其他侦听器中读取它?$http\u头和$send\u http\u头变量允许访问nginx中头的内容。请参阅使用
代理传递时添加头
是否有效?这个问题似乎与此相反:@cobaco此配置将此头添加到响应头中。在将请求头发送到代理服务器时,是否有方法添加请求头?@IndraUprade发送到后端代理服务器的头使用proxy\u set\u头进行管理: