nginx client certificate – Get Client Certificate information with SSL, php-fpm and nginx

Hello guys,
i have stuck to get information for nginx client certificate in two way authentication with nginx and php-fpm.

When I use Apache, I can access the $_SERVER[‘SSL_*’] varaibles, but this is not available to nginx by default.

So, you have to make a little changes to the config files.

/etc/nginx/conf.d/default.conf

    location ~ \.php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
 
        fastcgi_param SSL_CLIENT_CERT       $ssl_client_cert;
        fastcgi_param SSL_CLIENT_RAW_CERT   $ssl_client_raw_cert;
        fastcgi_param SSL_CLIENT_S_DN       $ssl_client_s_dn;
        fastcgi_param SSL_CLIENT_I_DN       $ssl_client_i_dn;
        fastcgi_param SSL_CLIENT_SERIAL     $ssl_client_serial;
 
  }
    location ~ \.php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;

        fastcgi_param SSL_CLIENT_CERT       $ssl_client_cert;
        fastcgi_param SSL_CLIENT_RAW_CERT   $ssl_client_raw_cert;
        fastcgi_param SSL_CLIENT_S_DN       $ssl_client_s_dn;
        fastcgi_param SSL_CLIENT_I_DN       $ssl_client_i_dn;
        fastcgi_param SSL_CLIENT_SERIAL     $ssl_client_serial;

  }

nginx client certificate information

This is all you have to do (if you already setup the SSL connection in the configuration file). And now you have the following variables available to PHP:

$_SERVER[„SSL_CLIENT_CERT“] – the actual client certificate

$_SERVER[„SSL_CLIENT_RAW_CERT“] – the actual RAW client certificate

$_SERVER[„SSL_CLIENT_S_DN“] – Subject DN in client’s certificate

$_SERVER[„SSL_CLIENT_I_DN“] – Issuer DN of client’s certificate

I hope this will help you 🙂

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz