Nginx 反向代理后如何获取真实客户端 IP

记录以下

前后端分离项目需要获取真正的客户端IP 【需要在nginx 设置如何几个地方】

1 vhost 中的虚拟主机任意处
    location /api { 
            rewrite  ^/api/(.*)$ /api/$1 break;
            proxy_pass   https://***.*********.com;
    }
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

2 如果有报错
Starting nginx: nginx: [warn] could not build optimal proxy_headers_hash, you should increase either proxy_headers_hash_max_size: 512 or proxy_headers_hash_bucket_size: 64; ignoring proxy_headers_hash_bucket_size

    nginx.conf 下的http 模块下设置
    proxy_headers_hash_max_size 51200;
    proxy_headers_hash_bucket_size 6400;

3 代码里面获取
   $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
本作品采用《CC 协议》,转载必须注明作者和本文链接
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!