NGINX 上的全局变量

问题描述

我正在使用 Nginx 来提供静态文件、反向代理到 Django 并验证客户端证书。

我不想在 url root 请求证书,所以我在 Nginx.conf 上创建了另一个服务器来请求端口 8443 上的证书。这个服务器只是为了请求证书并重定向客户端回到 443 端口,这里发生了 Django 的反向代理。

这是我的 Nginx.conf:


events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

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

    sendfile        on;

    keepalive_timeout  65;

    gzip  on; 

    upstream app {
        server django:8000;
    }

    # Redirect from HTTP to HTTPS
    server {
        listen *:80;
        server_name localhost;
        return 301 https://localhost$request_uri;
    }

    server {
        
        listen *:443 ssl;
        server_name localhost;

        ssl_certificate      /etc/Nginx/ssl/certificate.pem;
        ssl_certificate_key  /etc/Nginx/ssl/certificate.key;
        ssl_password_file    /etc/Nginx/ssl/certificate.pass;

        ssl_verify_client off;
        ssl_client_certificate /etc/Nginx/ssl/chain.cer;
        ssl_verify_depth 3;

        ssl_protocols TLSv1.1 TLSv1.2;
        ssl_ciphers EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH;                
        ssl_prefer_server_ciphers  on;

        server_tokens off;
        underscores_in_headers on;

        location /static/ {
            autoindex off;
            alias /static_files/;
        }

        location / {
            try_files $uri $uri/ @app_web;
        }

        location @app_web {
            proxy_pass http://app;
            proxy_pass_request_headers on;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header CERTINFO $http_certinfo;
            proxy_redirect off;
        }
    }

    server {
        
        listen *:8443 ssl;
        server_name localhost;

        ssl_certificate      /etc/Nginx/ssl/certificate.pem;
        ssl_certificate_key  /etc/Nginx/ssl/certificate.key;
        ssl_password_file    /etc/Nginx/ssl/certificate.pass;

        ssl_verify_client on;
        ssl_client_certificate /etc/Nginx/ssl/certificate.cer
        ssl_verify_depth 3;

        ssl_protocols TLSv1.1 TLSv1.2;
        ssl_ciphers EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH;                
        ssl_prefer_server_ciphers  on;

        add_header CERTINFO $ssl_client_s_dn;

        return 301 https://$host$request_uri;
    
    }
}

当客户端在端口 8443 上进行身份验证并且我将他返回到端口 443 时,我需要转发他的证书信息,更具体地说是 $ssl_client_s_dn。为此,我尝试在端口 8443 (CERTINFO) 的服务器上使用 add_header 方法和在端口 443 的服务器上使用 $http_certinfo 捕获值的方法 proxy_set_header。但此解决方案不起作用。报头不会从 8443 端口转发到 443 端口服务器。

我的问题是:有没有办法做到这一点?我可以在 http 块上设置某种“全局”变量,更改其在端口 8443 上的值,然后使用端口 443 上的更新值将其转发给 Django?

非常感谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)