# 修改的配置文件位置
该怎么改网上的教程已经很多,但是到底在哪个文件的什么地方修改,如何生效这才是根本问题。
我们需要修改是的 nginx.conf
根配置文件
使用 lnmp 的服务器一般配置文件在:
1 | /usr/local/nginx/conf/nginx.conf |
,宝塔的配置文件我发现有两个地方有 nginx.conf
配置文件:
1 | /www/server/nginx/conf/nginx.conf`,`/www/server/nginx/src/conf/nginx.conf |
。有效的配置文件是:
1 | /www/server/nginx/conf/nginx.conf |
在以下位置添加修改的配置:
<img src="https://image.wawacm.com/wawacdn/00663coEly1gip9keyiv7j30ke0hu41a.jpg" alt="img" style="zoom:200%;" />
其中需要注意的是 log_format main
,这里的 main
,需要在 vhost 中各站点的日志配置的地方要带上,表示使用这个配置,如图:
<img src="https://image.wawacm.com/wawacdn/00663coEly1gip9kn6bc6j30ji0dj0ts.jpg" alt="img" style="zoom:150%;" />
修改以上两个位置即可让配置生效。
# 参数配置
通过抓包可以看到,cdn 节点在溯源请求服务器地址的时候会将用户客户端的 ip 带上,以 X-Forwarded-For
, CF-Connecting-IP
两个参数的形式,如图:
<img src="https://image.wawacm.com/wawacdn/00663coEly1gip9kwruy3j30tx0c7dhf.jpg" alt="img" />
如果客户端加一个 x-forwarded-for
参数来尝试篡改,实际上该参数会有两个 ip,后面的一个 ip 永远是真实 ip,前面是伪造的:“8.8.8.8,163.160.73.253”
所以在配置日志参数时,可以将 X-Forwarded-For
参数的值放到第一位,nginx 模块中的变量为 $http_x_forwarded_for
,所以最终的格式为以下,其中保留了原始的 $remote_addr
,否则真实 ip 请求网站无法获取 ip。
1 | log_format main '"$http_x_forwarded_for"-$remote_addr - $remote_user [$time_local] "$request" ' |
综合以上的配置,最终日志格式完美记录真实 ip 与 cdn 节点的 ip:
<img src="https://image.wawacm.com/wawacdn/00663coEly1gip9l248uzj30lk07ijst.jpg" alt="img" style="zoom:150%;" />