首页  >  文章  >  后端开发  >  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” http://domain.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