首頁  >  文章  >  後端開發  >  Nginx取得自訂頭部header的值

Nginx取得自訂頭部header的值

WBOY
WBOY原創
2016-07-28 08:26:485765瀏覽

Nginx讀取自訂header

在參考了資料:
http://stackoverflow.com/questions/8393772/how-to-get-non-standard-http-headers-on -nginx
http://nginx.org/en/docs/http/ngx_http_core_module.html#underscores_in_headers
http://serverfault.com/questions/297225/nginx-passing-back-custom-header
https://easyengine.io/tutorials/nginx/forwarding-visitors-real-ip/
http://www.ttlsa.com/nginx/nginx-proxy_set_header/
後面得到如下:
1.nginx是支援讀取非nginx標準的使用者自訂header的,但需要在http或server下開啟header的下劃線支援:

  • underscores_in_headers on;

2、例如我們自訂header為X-Real-IP,透過第二個nginx取得該header時需要這樣:

  • $http_x_real_ip; (一律採用小寫,而且前面多了個http_)

3、如果需要把自訂header傳遞到下一個nginx:

  • 如果是在nginx中自訂採用proxy_set_header X_CUSTOM_HEADER $http_host;

  • 如果是在使用者要求時自訂的header,例如curl –head -H “X_CUSTOM_HEADER: foo” .com/api/test,則需要透過proxy_pass_header X_CUSTOM_HEADER來傳遞

範例:

<span>http</span><span>{</span><span>
    upstream myServer </span><span>{</span><span>        server </span><span>127.0</span><span>.</span><span>0.1</span><span>:</span><span>8082</span><span>;</span><span>}</span><span>
    underscores_in_headers on</span><span>;</span><span>
    server </span><span>{</span><span>
        listen       </span><span>80</span><span>;</span><span>
        server_name  localhost</span><span>;</span><span>        location  </span><span>/</span><span>{</span><span>
  	    proxy_set_header </span><span>Some</span><span>-</span><span>Thing</span><span> $http_x_custom_header</span><span>;;</span><span>
  	    proxy_set_header X</span><span>-</span><span>Forwarded</span><span>-</span><span>For</span><span> $proxy_add_x_forwarded_for</span><span>;</span><span>
  	    proxy_pass http</span><span>://</span><span>myServer</span><span>;</span><span>}</span><span>}</span><span>}</span>

以上就介紹了 Nginx獲取自訂頭部header的值,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn