查看谁在访问你的站点
外观
基于ubuntu22下apache2+frp,查看日志发现访问ip都是127.0.0.1,需要修改一下配置。
如果你是通过 HTTPS (443端口) 访问,且 SSL 证书是配置在 内网 Apache 服务器 上,情况会稍微复杂一点,因为 Apache 对 HTTPS 的 Proxy Protocol 支持主要依赖于 mod_remoteip 模块。
请按照以下步骤针对 HTTPS 进行配置:
1. 确认 frpc 配置
确保你的 frpc.ini 中,HTTPS 的代理部分开启了协议支持:
[web_https]
type = https # 或者如果是 tcp 转发 443
local_port = 443
custom_domains = your-wiki.com
proxy_protocol_version = v2
2. 配置 Apache 的 SSL 虚拟主机
你需要编辑处理 443 端口的配置文件(通常是 /etc/apache2/sites-enabled/default-ssl.conf 或类似的虚拟主机文件)。
在 <VirtualHost *:443> 块中加入以下指令:
Apache
<VirtualHost *:443>
ServerName your-wiki.com
# 1. 开启 Proxy Protocol 支持
RemoteIPProxyProtocol On
# 2. 声明信任来自本地 frp 转发的 IP
RemoteIPInternalProxy 127.0.0.1
# SSL 相关配置保持不变...
SSLEngine on
SSLCertificateFile /path/to/cert.pem
SSLCertificateKeyFile /path/to/key.pem
# 确保日志使用真实 IP
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
3. 修改全局日志格式 (非常重要)
如果这一步不做,日志里显示的依然会是 127.0.0.1。 编辑 /etc/apache2/apache2.conf,找到 LogFormat 定义行,将 %h 改为 %a:
Apache
# 找到这一行(通常叫 combined)
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
# 修改为(将 %h 改为 %a):
LogFormat "%a %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
4. 重启并验证
Bash
sudo a2enmod remoteip
sudo apache2ctl configtest
sudo systemctl restart apache2
⚠️ 特别提示:
- 版本要求:
RemoteIPProxyProtocol指令需要 Apache 2.4.31 及以上版本。如果你的系统版本较老(如 Ubuntu 16.04 以前),可能不支持这个指令。- 查看版本命令:
apache2 -v
- 查看版本命令:
- 连接断开问题:一旦你在
frpc开启了proxy_protocol_version = v2,Apache 的对应端口 必须 配置了RemoteIPProxyProtocol On才能正常通信。如果一处配了一处没配,会导致握手失败(SSL 错误或连接重置)。 - AWS 安全组:确保 AWS 服务端的 frp 监听端口(如 7000)和映射出的 443 端口都已经放行。
如果在重启时报错 Invalid command 'RemoteIPProxyProtocol': 这说明你的 Apache 版本太低,或者 remoteip 模块没加载成功。此时你可能需要升级 Apache,或者改用 Nginx 作为前置转发,因为 Nginx 对该协议的支持更早且更稳定。
使用过程中发现宿主机无法访问站点遂有接下来操作。