目次概述1. 为什么必要获取客户端的真实 IP 地址?在利用 Nginx 作为反向代理服务器时,默认环境下,后端服务器只能看到 Nginx 的IP地址。为了记载日记、限制访问或举行其他基于 IP 地址的操作,获取客户端的真实 IP 地址非常紧张。 2. Nginx 中用于获取真实 IP 地址的模块Nginx 提供了两个紧张模块来处理这一需求:
这里紧张先容 [code]HttpRealipModule[/code]。 3. 设置示例和步骤3.1 安装和启用模块大多数环境下,Nginx 已经包含了 [code]HttpRealipModule[/code]。可以通过以下命令检查: [code]nginx -V 2>&1 | grep -o with-http_realip_module [/code]如果没有启用该模块,则必要重新编译 Nginx 或安装包含该模块的 Nginx 版本。 3.2 设置 Nginx编辑你的 Nginx 设置文件(通常位于 [code]/etc/nginx/nginx.conf[/code] 或 [code]/etc/nginx/conf.d/[/code] 中的某个文件),添加以下设置: [code]http { ... set_real_ip_from 0.0.0.0/0; # 答应所有 IP 地址的代理 real_ip_header X-Forwarded-For; real_ip_recursive on; ... server { ... location / { ... # 如果必要日记中记载真实 IP log_format custom '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log custom; ... } } } [/code]3.3 设置阐明
4. 潜在的陷阱和调试方法4.1 潜在的陷阱
4.2 调试方法
实操http 节点 添加$http_x_forwarded_for日记格式设置文件中必要添加$http_x_forwarded_for日记格式, 焦点内容如下 [code]http { include mime.types; default_type application/octet-stream; log_format main ' $remote_addr | $http_x_forwarded_for | $remote_user | $time_local | $request | $http_host |' ' $status | $upstream_status | $body_bytes_sent | $http_referer ' ' $http_user_agent | $upstream_addr | $request_time | $upstream_response_time'; [/code]http节点 日记格式中必要添加$http_x_forwarded_for [code]log_format[/code] 指令用于界说 Nginx 的日记格式。它指定了在日记文件中记载哪些信息以及如何格式化这些信息。每个字段利用一个变量表现,变量之间可以用分隔符分开,如空格、竖线([code]|[/code])等。界说的日记格式可以应用于 [code]access_log[/code] 指令,以便记载客户端请求的详细信息。 以下是 [code]log_format[/code] 指令中各个变量的含义: [code]log_format main ' $remote_addr | $http_x_forwarded_for | $remote_user | $time_local | $request | $http_host |' ' $status | $upstream_status | $body_bytes_sent | $http_referer ' ' $http_user_agent | $upstream_addr | $request_time | $upstream_response_time'; [/code]
server节点 设置[code]server { listen 80; server_name localhost; access_log logs/access.log main; #必要添加日记引用 proxy_set_header X-Real-IP $remote_addr; #添加透传设置 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for location / { root html; } } [/code]https://www.jb51.net/server/3262802e1.htm 利用 proxy_set_header 指令设置透传头部。确保代理服务器(如 Nginx)在转发请求时保存原始客户端的 IP 地址 验证方式一访问Nginx页面 访问日记 [code]192.168.0.6 | 168.138.171.206 | - | 19/May/2024:10:57:24 +0800 | GET / HTTP/1.1 | nginx.frps.fun | 200 | - | 615 | - Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 | - | 0.000 | -[/code]
含义:直接连接到 Nginx 服务器的客户端的 IP 地址。在这个例子中,这可能是一个内网的 IP 地址。
含义:通过 [code]X-Forwarded-For[/code] 头部获取的客户端的真实 IP 地址。在经过代理或负载均衡器时,这个头部会记载原始客户端的 IP 地址。
含义:客户端的用户名。在请求必要 HTTP 根本认证时记载用户名。这里没有举行认证,以是表现为 [code]-[/code]。
含义:请求到达服务器的本地时间,格式为 [code]day/month/year:hour:minute:second timezone[/code]。这个例子中表现 2024 年 5 月 19 日上午 10:57:24,时区为 +0800。
含义:客户端的请求行,包含请求的方法(GET)、请求的资源路径(/),以及利用的 HTTP 协议版本(HTTP/1.1)。
含义:请求中的 [code]Host[/code] 头部,表现客户端请求访问的主机名。
含义:HTTP 相应状态码,表现请求乐成。[code]200[/code] 代表乐成。
含义:上游服务器的相应状态码。在没有上游服务器时,这里表现为 [code]-[/code]。
含义:传送给客户端的相应主体内容的字节数,不包罗相应头的大小。
含义:请求的引用页面(Referer)。如果没有引用页面则表现为 [code]-[/code]。
含义:客户端利用的欣赏器或其他客户端的信息(User-Agent)。在这个例子中,表现客户端利用的是 Chrome 欣赏器,运行在 macOS 上。
含义:上游服务器的地址。在没有上游服务器时,这里表现为 [code]-[/code]。
含义:处理请求的总时间,从接收到客户端请求到完整发送相应的时间,单元为秒。
含义:从上游服务器读取相应的时间。在没有上游服务器时,这里表现为 [code]-[/code]。 方式二[code]如果我们前面没有apisix或者其它lb,也可以利用curl命令来模仿X-Forwarded-For日记[/code] [code]curl http://127.0.0.1/ -H 'X-Forwarded-For: 1.1.1.1' -v * About to connect() to 127.0.0.1 port 80 (#0) * Trying 127.0.0.1... * Connected to 127.0.0.1 (127.0.0.1) port 80 (#0) > GET / HTTP/1.1 > User-Agent: curl/7.29.0 > Host: 127.0.0.1 > Accept: */* > X-Forwarded-For: 1.1.1.1 #-v参数可以看到这里参数已经传入 > < HTTP/1.1 200 OK < Server: nginx < Date: Sun, 19 May 2024 03:09:05 GMT < Content-Type: text/html; charset=utf-8 < Content-Length: 615 < Last-Modified: Mon, 09 Oct 2023 06:03:57 GMT < Connection: keep-alive < ETag: "652397cd-267" < Accept-Ranges: bytes < <!DOCTYPE html> #http:127.0.0.1 就是Nginx服务器 [/code]
日记 [code]127.0.0.1 | 1.1.1.1 | - | 19/May/2024:10:57:24 +0800 | GET / HTTP/1.1 | 127.0.0.1 | 200 | - | 615 | - | curl/7.29.0 | - | 0.000 | -[/code]从日记文件 logs/access.log 中,可以验证 X-Forwarded-For 头部信息是否正确记载, 此日记记载表现,X-Forwarded-For 头部中通报的 1.1.1.1 已正确记载。 到此这篇关于Nginx中透传客户端真实IP的本领的文章就先容到这了,更多相关Nginx 透传IP内容请搜索脚本之家以前的文章或继续欣赏下面的相关文章盼望各人以后多多支持脚本之家! 来源:https://www.jb51.net/server/326278c9h.htm 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
|手机版|小黑屋|梦想之都-俊月星空
( 粤ICP备18056059号 )|网站地图
GMT+8, 2025-7-1 18:14 , Processed in 0.038715 second(s), 19 queries .
Powered by Mxzdjyxk! X3.5
© 2001-2025 Discuz! Team.