【问题标题】:Trouble with ProxyPass of REMOTE_USER from nginx to Django从 nginx 到 Django 的 REMOTE_USER 的 ProxyPass 出现问题
【发布时间】:2021-03-02 00:11:16
【问题描述】:

我有一个配置了客户端证书身份验证的 nginx Web 服务器。一旦通过身份验证,我将请求代理到 uWSGI Django 应用程序服务器。我已经将我的 Django 应用程序设置为使用 REMOTE_USER (https://docs.djangoproject.com/en/3.1/howto/auth-remote-user/) 执行身份验证,但是它似乎无法正常工作,因为我从 Django 收到“'AnonymousUser' 对象不可迭代”错误。

我假设我的 nginx 配置中缺少一些东西:

server {
  listen 80;
  server_name my.website.net;
  return 301 https://$server_name$request_uri;
}
server {
  listen 443 ssl;
  ssl_certificate /etc/letsencrypt/live/my.website.net/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/my.website.net/privkey.pem;
  ssl_verify_client on;
  ssl_verify_depth 2;
  ssl_client_certificate /etc/nginx/ssl/cas.pem;
  proxy_set_header X-SSL-Client-Serial $ssl_client_serial;
  proxy_set_header X-SSL-Client-Verify $ssl_client_verify;
  proxy_set_header X-SSL-Client-S-DN   $ssl_client_s_dn;
  proxy_set_header X-Remote-User $remote_user;
  proxy_set_header REMOTE_USER $remote_user;

  location / {
    root /var/www/my.website.net;
    index index.html index.htm;
  }

  location /django {
    proxy_set_header Host 10.101.10.228;
    proxy_http_version 1.1;
    proxy_set_header Connection "Keep-Alive";
    proxy_set_header Proxy-Connection "Keep-Alive";

    proxy_pass http://10.101.10.228:8000/webapp;
    proxy_redirect http://10.101.10.228/django/ https://my.website.net/django/;

  }

}

我也尝试过使用 nginx uwsgi_* 指令,但没有成功:

upstream django {
  server 10.101.10.228:8000;
}
location /django {
  include         uwsgi_params;
  uwsgi_pass django;
  uwsgi_param SCRIPT_NAME /webapp;
  uwsgi_param REMOTE_USER $remote_user;
  uwsgi_param X-REMOTE-USER $remote_user;
}

似乎缺少什么?

【问题讨论】:

  • 它绝对应该与 uwsgi_params 一起使用,而不是通过标题。但是,是否填充了 $remote_user,因为客户端证书身份验证与基本身份验证不同。如果不确定,可以使用 log_format 将变量记录到 nginx 日志。

标签: django nginx proxy reverse-proxy nginx-reverse-proxy


【解决方案1】:

$remote_user nginx 变量仅为 HTTP 基本身份验证填充。您可以依赖 $ssl_client_s_dn 变量来使用客户端 DN 作为用户名:

proxy_set_header Remote_User $ssl_client_s_dn;

或

uwsgi_param REMOTE_USER $ssl_client_s_dn;

您可以使用 map 块从客户端 DN 中仅检索某些特定字段,例如仅获取 CN(规范名称):

map $ssl_client_s_dn $remote_user_cn {
    ~,CN=(?<CN>[^,]+) $CN;
}

然后使用$remote_user_cn 变量作为用户名。

【讨论】:

  • 你说得对,$remote_user 没有自动设置,所以我使用了 $ssl_client_s_dn(使用 uwsgi_pass)。谢谢!
猜你喜欢
  • 2023-01-19
  • 2016-09-01
  • 2011-06-14
  • 1970-01-01
  • 2021-05-31
  • 1970-01-01
  • 2011-01-07
  • 2015-11-08
  • 1970-01-01
相关资源
最近更新 更多