首页 > 解决方案 > 在 Nginx 的访问日志中显示从 auth_request 检索到的远程用户

问题描述

我在 nginx 中有以下配置:

    location /api/ {
        auth_request /auth/request/;

        auth_request_set $user $upstream_http_remote_user;
        proxy_set_header Remote-User $user;
      

        proxy_pass http://...;
    }

我正在设置该Remote-User字段,因此我希望在访问日志中会出现该值,但只是-出现,表明该值不存在。在代理的服务器中,我可以访问 header Remote-User,所以我知道该值设置得很好,但无法在访问日志中显示它。

我想访问日志可能是在身份验证请求之前生成的。

有没有办法Remote-User通过这种配置获取访问日志的内部?

标签: nginxnginx-config

解决方案


在你的 nginx.conf 你应该有类似的东西

log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';

$remote_user用你的$user变量替换


推荐阅读