公司使用nginx作為負載均衡,有時候需要自定義header頭發送給后端的真實服務器. 想過去應該是非常的簡單的事情.
例子如下:
設置代理服務器ip頭
1
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
然后自己在自定義個header,remote_header_test,如下:
1
|
proxy_set_header remote_header_test "123123123";
|
接着后端真實服務器打開www.ttlsa.com/nginx_header.php
源代碼是簡單的phpinfo
1
2
3
4
5
|
<?php
phpinfo();
?>
|
在phpinfo結果頁面中搜索剛才設置的頭部,發現沒有找到,網上查找資料,才發現原來nginx會忽略掉下划線的頭部變量.於是改成如下:
1
|
proxy_set_header remoteheadertest "123123123";
|
再次打開www.ttlsa.com/nginx_header.php,搜索remoteheadertest,有內容. 看來果真不能用下划線. 然后改成'-',如下:
1
|
proxy_set_header remote-header-test "123123123";
|
打開頁面,搜索到的頭部是remote_header_test. 自動轉換成下划線了.
如果想要支持下划線的話,需要增加如下配置:
1
|
underscores_in_headers on;
|
可以加到http或者server中
語法:underscores_in_headers on|off
默認值:off
使用字段:http, server
是否允許在header的字段中帶下划線
實例配置:
server_name dev.fashio*****.cn; underscores_in_headers on; location /bi-api/ { proxy_pass http://192.168.0.155:8080/; proxy_set_header Host $host;