深入nginx之《获取用户的真实IP》
获取用户的真实IP
Nginx会将客户端的IP信息存放在$remote_addr变量里,但这并不意味着它就是客户端的IP,生产环境往往会充满各种代理,让IP的来龙去脉变得扑朔迷离。
目前互联网公司基本上都采用这种架构方式
用户的请求并不是直接和Nginx交互,而是通过了CDN加速平台。默认情况下,Nginx看到的$remote_addr是CDN的IP,这对日志的记录和分析,还有后端的业务逻辑都可能产生不良的影响,如果需要获取到用户的真实IP呢,这个时候realip模块就起到了作用。
此模块需要编译时开启:
--with-http_realip_module
在Nginx的HTTP块里面配置:
set_real_ip_from CDN_IP;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
set_real_ip_from:
设置哪些IP是可信任的,从这些IP进行获取请求头信息,这些IP就配置为CDN的IP白名单。
real_ip_header:
定义从哪个请求头获取IP信息,其值将用于替换客户端地址。一般都使用X-Forwarded-For。
real_ip_recursive:
如果启用递归搜索,将会对可信任的IP匹配,原始客户端地址将替换为在请求头域中发送的最后一个不可信地址。如果禁用递归搜索,则从白名单中由real_ip_header指令定义的请求头中的最后一个地址替换。
通过此方式就可以获取到CDN传递给Nginx的用户的IP。那么问题来了,$remote_addr被替换成了用户的IP,有些时候我们也希望获取到CDN 的节点IP,这对于排查一些和CDN有关的问题是由帮助的。可以使用$realip_remote_addr,它可以保留原始客户端地址。 (1.9.7版本新增的变量)
This is a <a href=https://diigo.com/0k39va>btc to eur</a> very good and trust site!
<a href="http://sftpharmacy.com/">cheapest pharmacy for prescriptions</a> <a href="http://cialisphm.com/">cheapest cialis us</a> <a href="http://bestnpills.com/">modafinil 200 mg coupon</a> <a href="http://iviagracialislevitra.com/">cheap cialis daily</a> <a href="http://ddviagra.com/">sildenafil 20 mg price</a>
<a href="http://viagraoctab.com/">suhagra 500 mg</a> <a href="http://tmviagra.com/">buying viagra nz</a> <a href="http://cialisdx.com/">cialis brand 10mg</a> <a href="http://viagraops.com/">how to get viagra</a> <a href="http://azzmeds.com/">tadalafil 20 mg soft tabs</a>
<a href="http://mpcialis.com/">rx tadalafil tablets</a>
<a href="http://drugstore555.com/">safe online pharmacies</a> <a href="http://ordcialis.com/">order cialis online in canada</a> <a href="http://ddviagra.com/">viagra buy uk online</a> <a href="http://viagracap.com/">sildenafil medicine</a> <a href="http://srxstore.com/">20mg levitra</a> <a href="http://cialispwr.com/">best generic tadalafil</a> <a href="http://iviagracialislevitra.com/">cost generic cialis</a> <a href="http://viagradist.com/">best price for generic viagra</a> <a href="http://viagra21.com/">viagra pills online canada</a> <a href="http://pharmacyvpro.com/">no prescription needed pharmacy</a>
<a href="https://nfshealth.com/">amoxicillin 5000 mg</a> <a href="https://viagracap.com/">us pharmacy viagra online</a> <a href="https://wheretocialis.com/">buy erectafil</a> <a href="https://apocialis.com/">cialis canada no prescription</a> <a href="https://hfspills.com/">buy modafinil mexico</a>
[url=https://moverslosangeles.city ]Movers Los Angeles [/url]
<a href="https://yestablets.com/">lexapro otc</a> <a href="https://pharmacyrp.com/">canadian pharmacy sildenafil</a>
<a href="http://hfspills.com/">where can i get modafinil</a> <a href="http://cialiseti.com/">dapoxetine otc us</a> <a href="http://cialisesale.com/">buy priligy in australia</a> <a href="http://cialisvm.com/">cialis discount pharmacy</a>
<a href="http://viagradist.com/">how much is sildenafil from canada</a> <a href="http://rxphealth.com/">sildenafil over the counter india</a> <a href="http://viagradx.com/">buy viagra online australia fast delivery</a> <a href="http://iviagracialislevitra.com/">canadian online pharmacy viagra</a>
<a href="http://bestnpills.com/">modafinil 200mg cost</a> <a href="http://xeonpharmacy.com/">cheapest pharmacy prescription drugs</a> <a href="http://phrmviagra.com/">buy sildenafil in canada</a> <a href="http://cialisdx.com/">order cialis no prescription</a> <a href="http://cialisbr.com/">canada cialis otc</a> <a href="http://srxstore.com/">generic levitra in canada</a> <a href="http://atcialis.com/">generic tadalafil 60 mg</a> <a href="http://cialiseff.com/">female cialis online</a> <a href="http://cialiseti.com/">tadalafil purchase online</a> <a href="http://viagraops.com/">online drugstore viagra</a>
<a href="https://priolending.com/">payday loans lenders</a>
<a href="http://viagraztab.com/">canadian pharmacy viagra 150 mg</a> <a href="http://phrmviagra.com/">by viagra</a> <a href="http://viagracap.com/">sildenafil 50 mg tablet price in india</a> <a href="http://drugstore555.com/">legitimate canadian mail order pharmacy</a> <a href="http://pharmacyvpro.com/">canadadrugpharmacy</a> <a href="http://cialisphm.com/">buy cialis canada online</a> <a href="http://npropills.com/">buy levitra online</a> <a href="http://ordcialis.com/">best dapoxetine tablet in india</a> <a href="http://cialisvm.com/">cialis 5 mg best price</a> <a href="http://azzmeds.com/">tadalafil 20 mg medication</a>
<a href="http://cialisesale.com/">75 mg tadalafil</a>