nodejs如何透传客户端ip地址

目前项目中用node做中间层,服务端获取的客户端ip地址都是node服务器的地址,请问node如何像nginx一样透传客户端ip地址?

阅读 7.2k
3 个回答

可以百度一下:“x-forwarded-for”这个http协议的header

这个header广泛用于代理服务器传递客户端真实IP给源服务器。

参见:http://baike.baidu.com/item/X...


       location / {

        ## TODO Gets the host name and port from the environment variable

        ## Build a nginx image with the lua module

        proxy_pass http://node:3030;

        ##set Host header

        proxy_set_header Host $host;


        ##set X-Real-IP header 可以设置下面的header

        proxy_set_header X-Real-IP $remote_addr;


        ##set Protocol header

        proxy_set_header Protocol $server_protocol; 


        ##set Port header

        proxy_set_header Port $server_port;


        ##disabele cache on development mode 

        proxy_cache off;

    } 

请求后端的时候,可以在headers里将用户ip放在x-forwarded-for属性里

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题