在使用 NPS(内网穿透服务)连接的 NPC 网页服务器时,可以在 Apache 中通过 Proxy Protocol 获取访客的真实 IP,通过以下步骤进行配置:
### 1. 确保 Apache 版本支持 Proxy Protocol
Apache 2.4.30 及以上版本支持 Proxy Protocol。如果您的 Apache 版本低于 2.4.30,可以考虑升级到更高版本。
### 2. 启用 `mod_remoteip` 模块
`mod_remoteip` 模块用于处理代理协议,启用该模块,然后重启 Apache 服务以应用更改。
### 3. 配置 `VirtualHost`
在 Apache 的 `VirtualHost` 配置中,添加 `RemoteIPProxyProtocol On` 指令,以启用 Proxy Protocol 支持。例如:
<VirtualHost *:80> ServerName your-domain.com RemoteIPProxyProtocol On </VirtualHost> <VirtualHost *:443> ServerName your-domain.com RemoteIPProxyProtocol On </VirtualHost>
这样,Apache 就可以正确解析通过 Proxy Protocol 传递的真实 IP。
### 4. 配置日志格式
为了在日志中记录真实 IP,可以修改日志格式。在 `httpd.conf` 或 `VirtualHost` 配置中,添加以下内容:
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{X-Forwarded-For}i\" \"%{User-Agent}i\"" combined CustomLog logs/access_log combined
这样,日志中会记录 `X-Forwarded-For` 头部中的真实 IP。
### 5. 配置前端代理(如 HAProxy)
如果使用 HAProxy 作为前端代理,需要在 HAProxy 配置中启用 Proxy Protocol,并将请求转发到 Apache 服务器。例如:
frontend http bind *:80 mode http option forwardfor default_backend apache_backend backend apache_backend server apache1 127.0.0.1:80 send-proxy
这样,HAProxy 会将请求以 Proxy Protocol 格式发送到 Apache。
### 注意事项
- 确保所有连接到 Apache 的代理服务器都启用 Proxy Protocol,否则会导致连接失败。
- 如果使用的是 HTTPS 协议,同样需要在 `VirtualHost` 中启用 `RemoteIPProxyProtocol On`。
通过以上配置,Apache 服务器可以通过 Proxy Protocol 获取访客的真实 IP 地址。
- 本文固定链接: https://www.fengshen.cn/2025/04/显示通过nps内网穿透访问的真实ip/diyofwind/
- 转载请注明: fengshen 发表于 Dream of Wind | 一梦风神
