working with header 從哪寫起呢,需求吧。
請求:https://header.test.ipinyou.com:7443/ ;
1. nginx 加入:
location / {
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:81;
}
則:後端機器能拿到請求來源ip,請求host,port資訊。
2. 擷取head頁面
<html> <body>
<?php
$arr = getallheaders();
foreach ($arr as $item => $content){
echo "<font color=\"blue\"><b>$item</b></font> => <font color=\"red\">\"$content\"</font></br>";
}
?>
</body> </html>
傳回結果:
Host => "header.test.ipinyou.com:7443"
X-Real-IP => "10.1.2.223"
X-Forwarded-For => "10.1.2.223"
Connection => "close"
User-Agent => "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0"
Accept => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
Accept-Language => "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"
Accept-Encoding => "gzip, deflate"
Cookie => "PYID=CBCC_81x6Ck; jsType=1; sessionId=D49Ard8ia4PR"
3. 設定header,host,server_port
proxy_set_header Host $host;
proxy_set_header port $server_port;
結果:
Host => "header.test.ipinyou.com"
port => "7443"
4. 自定義header
使用modify header add on 設定
True-Client-IP "testip"
User-Agent SonyEricsson e8000
Host "abcdef"
nginx中設定:
proxy_set_header Host $host;
proxy_set_header test2 'abc';
proxy_set_header test $http_true_client_ip;
結果:
Host => ""abcdef""
test2 => "abc"
test => "testip"
User-Agent => "SonyEricsson e8000"
True-Client-IP => "testip"
發現捕捉到了自定義的 true_client_ip
5.更多自定義header
當我們配置了
underscores_in_headers on; 【使用字段:http, server 是否允許在header的字段中帶下劃線】
之後,發現所有的自定義header都能被捕捉到。比如
請求加入header:any_para any_para_value
proxy_set_header any_para $http_any_para_value;
得到:
any_para => "any_para_value"
6. 寫入響應頭資訊:
header('myhost:phpserver');
=> myhost
phpserver
7. nginx全局變量
http://www.cnblogs.com/AloneSword/archive/2011/12/10/2283483.html