Carry の Blog Carry の Blog
首页
  • Nginx
  • Prometheus
  • Iptables
  • Systemd
  • Firewalld
  • Docker
  • Sshd
  • DBA工作笔记
  • MySQL
  • Redis
  • TiDB
  • Elasticsearch
  • Python
  • Shell
  • MySQL8-SOP手册
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Carry の Blog

好记性不如烂键盘
首页
  • Nginx
  • Prometheus
  • Iptables
  • Systemd
  • Firewalld
  • Docker
  • Sshd
  • DBA工作笔记
  • MySQL
  • Redis
  • TiDB
  • Elasticsearch
  • Python
  • Shell
  • MySQL8-SOP手册
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 工作笔记

  • K8S

  • Systemd

  • Nginx

    • Nginx添加用户认证
    • 利用nginx+sftp实现一个可供用户下载的服务
    • nginx配置文件及模块
    • 通过脚本按天切割nginx的日志
    • nginx通过四层代理实现端口转发
    • NGINX基于cookie针对同一域名进行分流转发
    • nginx利用内置模块配置限速限流
    • 利用NGINX内置模块mirror进行流量复制等操作
    • 使用$remote_user字段记录访问NGINX的用户
    • 从NGINX自身配置文件中定义访问日志按时间切割
    • NGINX配置单独代理百度的sitemap文件
    • nginx配置微信小程序校验及其他
    • nginx配置gzip压缩
    • 由Nginx集中代理分散的PHP集群的实践
    • http状态码详解
    • OpenResty-1-13-6-2-新增ldap模块儿
    • 排查NGINX的open_file_cache导致发布后访问404的问题
    • 制作OpenResty-1-19-9-1的RPM包
    • nginx-proxy-managar使用笔记
    • Nginx 的端口复用:提升服务器并发能力
  • Supervisord

  • OpenLdap

  • OpenVPN

  • GitLab

  • Sshd

  • WebDev

  • Docker

  • Prometheus

  • Rclone

  • Iptables

  • Firewalld

  • Linux笔记
  • Nginx
Carry の Blog
2019-11-16

使用$remote_user字段记录访问NGINX的用户收藏(二丫讲梵)备查

有时候我们会给一些没有集成权限认证系统的页面加一层认证,以提高安全性,但是针对一些涉及到线上生产的一些界面,如果使用 NGINX 统一认证的账号,那么如果某个时刻被改坏了,想要确认是谁都很难。

NGINX 在打印日志的时候,事实上是有 $remote_user这一参数的,只不过常规使用的时候,无法获取到值,需要在基于认证规则的时候,才能够生效,大多时候我们使用htpasswd来做的认证,这样会比较麻烦,所以优雅的方案应该是结合 ldap 认证的模块,来实现认证系统,然后在日志格式化中添加用户字段:

log_format json escape=json '{ "remote_addr": "$remote_addr",'
                           '"@timestamp":"$time_iso8601",'
                           '"request_uri": "$request_uri",'
                           '"remote_user": "$remote_user",'# 写入之后,就能记录访问的用户了
                           '"verb": "$request_method",'
                           '"httpversion": "$server_protocol",'
                           '"response": "$status", '
                           '"body_bytes_sent": "$body_bytes_sent", '
                           '"referrer": "$http_referer", '
                           '"user_agent": "$http_user_agent", '
                           '"server_name": "$host",'
                           '"request_time": "$request_time",'
                           '"upstream_response_time": "$upstream_response_time",'
                           '"realpath_root": "$realpath_root",'
                           '"cookie": "$http_cookie",'
                           '"request_body": "$request_body",'
                           '"nginx_version": "$nginx_version",'
                           '"scheme": "$scheme"}';
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

然后添加 ldap 的配置,这里不再赘述,参考这里:https://www.ranisa.cn/pages/3416.html#_6-nginx

img

最后模拟访问一下,看看日志中是否记录到了对应用户:

{ "remote_addr": "127.0.0.1","@timestamp":"2019-11-15T16:57:29+08:00","request_uri": "/","remote_user": "eryajf","verb": "GET","httpversion": "HTTP/1.1","response": "200", "body_bytes_sent": "3", "referrer": "", "user_agent": "ELinks/0.12pre6 (textmode; Linux; 89x44-2)", "server_name": "127.0.0.1","request_time": "0.002","upstream_response_time": "","realpath_root": "/usr/share/nginx/html","cookie": "","request_body": "","nginx_version": "1.14.0","scheme": "http"}
1

就能看到了在对应时间点中,eryajf 这个用户登陆了本系统,以后若有什么问题,就能更容易锁定目标了。

#nginx
上次更新: 4/24/2025

← 利用NGINX内置模块mirror进行流量复制等操作 从NGINX自身配置文件中定义访问日志按时间切割→

最近更新
01
tidb fast ddl
04-04
02
TiDB配置文件调优 原创
04-03
03
如何移除TiDB中的表分区 原创
04-03
更多文章>
Theme by Vdoing
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式