---
title: "nginx 代理模式下，获取客户端真实IP"
author: "ssxxjjii"
date: "2011-11-30T09:42:59Z"
tags: []
categories: ["开源软件"]
slug: "nginx-ip"
draft: false
lastmod: "2012-09-13T11:25:28Z"
id: 1285126
url: "https://tdcq.iteye.com/blog/1285126"
---

最近做博友推荐，发现个小问题，用$\_SERVER\['REMOTE\_ADDR'];得到的都是服务器的地址192.168.96.52，搜索了一下，发现问题，改为$\_SERVER\['HTTP\_X\_REAL\_IP'];：

### [**nginx**]() 代理模式下，获取客户端真实IP

 

在nginx中设置:

proxy\_set\_header        Host            $host;  
proxy\_set\_header        X-Real-IP       $remote\_addr;  
proxy\_set\_header        X-Forwarded-For $proxy\_add\_x\_forwarded\_for;

后端程序可以从Server变量HTTP\_X\_REAL\_IP和HTTP\_X\_FORWARDED\_FOR中获取用户的ip地址。

HTTP\_X\_REAL\_IP的值是nginx得到的用户REMOTE\_ADDR

 

\--------------------------

[http://gong1208.iteye.com/blog/1559835](http://gong1208.iteye.com/blog/1559835)