标签归档:X-Forwarded-For

laravel 获取真实ip的事项

# 这个逻辑可以放在中间件里,判断白名单(127.0.0.1或你的其他代理服务器ip)
$ip = $request->ip();
        if (in_array($ip, ['127.0.0.1']))
            $request->setTrustedProxies($request->getClientIps(), Request::HEADER_X_FORWARDED_ALL);

# nginx 配套配置,如果你是nginx转发,按照这个标准来
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;


# 获取IP
echo $request->ip();

X-Forwarded-For 相关连接:https://en.wikipedia.org/wiki/X-Forwarded-For

nginx: [emerg] unknown directive “proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for” in

nginx配置代理转发,部分配置如下

        location / {
            # index index.html index.htm index.php;
            # try_files $uri $uri/ /index.php$request_uri;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Real-Port $remote_port;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass https://10.29.185.61:8063;
        }

$proxy_add_x_forwarded_forthe “X-Forwarded-For” client request header field with the $remote_addr variable appended to it, separated by a comma. If the “X-Forwarded-For” field is not present in the client request header, the $proxy_add_x_forwarded_for variable is equal to the $remote_addr variable.

“ X-Forward-For”客户端请求头字段,后面附加 $remote _ addr 变量,中间用逗号分隔。如果客户端请求头中没有“ X-Forward-For”字段,则 $proxy_add_x_forwarded_for 变量等于 $remote_addr 变量。