--- title: "How to Configure Mutual TLS (mTLS) with Nginx on Ubuntu" description: "Create a private CA with OpenSSL, issue server and client certificates, and require client certificates in Nginx on Ubuntu 24.04 to protect an internal API." category: "Proxy and Load Balancing" language: en source: https://cubepath.com/docs/proxy-and-load-balancing/mtls-mutual-tls-configuration updated: 2026-09-25 --- # How to Configure Mutual TLS (mTLS) with Nginx on Ubuntu With regular HTTPS only the server proves its identity. Mutual TLS (mTLS) adds the other direction: the client must also present a certificate signed by a CA the server trusts, or the TLS handshake is rejected before any request reaches your application. This makes mTLS a good fit for service-to-service APIs, admin endpoints and IoT devices. In this tutorial you will create a small private CA with OpenSSL, issue a server and a client certificate, configure Nginx on Ubuntu 24.04 to require client certificates, and handle revocation and renewal. ## Prerequisites To follow this tutorial you need: - A server running Ubuntu 24.04 LTS, for example a CubePath VPS, with Nginx installed (`sudo apt install nginx`). - A non-root user with `sudo` privileges. - A hostname for the protected service. This guide uses `api.your_domain`. It does not need a public DNS record for the local tests. - OpenSSL, which is preinstalled on Ubuntu 24.04 (version 3.0). ## Step 1 - Creating a private certificate authority The CA signs every certificate in this setup, and whoever holds its private key can issue certificates that your server will accept. Create it in a private working directory, and in production keep the CA key on a separate, well protected machine rather than on the web server. ```bash mkdir -p ~/mtls && cd ~/mtls chmod 700 ~/mtls ``` Generate a 4096-bit RSA key for the CA, protected by a passphrase. You will be asked for it every time you sign a certificate: ```bash openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:4096 -aes256 -out ca.key ``` Create the self-signed CA certificate, valid for 10 years: ```bash openssl req -x509 -new -key ca.key -sha256 -days 3650 -subj "/CN=Example Internal CA" -out ca.crt ``` Confirm that it is marked as a CA: ```bash openssl x509 -in ca.crt -noout -subject -ext basicConstraints ``` ```output subject=CN = Example Internal CA X509v3 Basic Constraints: critical CA:TRUE ``` ## Step 2 - Issuing the server certificate The server certificate proves the identity of `api.your_domain` to clients. Modern clients check the Subject Alternative Name (SAN), not the Common Name, so the SAN is required. Create an ECDSA key and a certificate signing request (CSR): ```bash openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out server.key openssl req -new -key server.key -subj "/CN=api.your_domain" -out server.csr ``` Create an extensions file that restricts the certificate to server authentication: ```bash nano server.ext ``` ```ini basicConstraints = CA:FALSE keyUsage = critical, digitalSignature extendedKeyUsage = serverAuth subjectAltName = DNS:api.your_domain ``` Sign the CSR with the CA: ```bash openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -days 397 -sha256 -extfile server.ext -out server.crt ``` Verify the certificate against the CA: ```bash openssl verify -CAfile ca.crt server.crt ``` ```output server.crt: OK ``` > Tip: If the service is also public, you can use a Let's Encrypt certificate as the server certificate and keep the private CA only for client certificates. Clients then do not need your CA to trust the server. ## Step 3 - Issuing a client certificate Each client (a service, a device or a person) gets its own key and certificate, so you can identify and revoke clients individually. Name the certificate after the client, here `billing-service`: ```bash openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out billing-service.key openssl req -new -key billing-service.key -subj "/CN=billing-service" -out billing-service.csr ``` Create the extensions file for client authentication: ```bash nano client.ext ``` ```ini basicConstraints = CA:FALSE keyUsage = critical, digitalSignature extendedKeyUsage = clientAuth ``` Sign it: ```bash openssl x509 -req -in billing-service.csr -CA ca.crt -CAkey ca.key -CAserial ca.srl -days 365 -sha256 -extfile client.ext -out billing-service.crt ``` Check the result and note the serial number, which you will need for revocation: ```bash openssl x509 -in billing-service.crt -noout -subject -serial -enddate ``` ```output subject=CN = billing-service serial=5C1A9E3F0B7D4A21C6E08F13A9B2D7640E1F3A58 notAfter=Sep 25 10:40:12 2027 GMT ``` Deliver `billing-service.crt` and `billing-service.key` to the client over a secure channel, together with `ca.crt` so it can verify the server. Never send the CA key. ## Step 4 - Installing the certificates on the server Copy the server certificate, its key and the CA certificate to a directory that only root can read: ```bash sudo install -d -m 0750 /etc/nginx/mtls sudo install -m 0644 ca.crt server.crt /etc/nginx/mtls/ sudo install -m 0600 server.key /etc/nginx/mtls/ ``` The CA private key (`ca.key`) stays in `~/mtls` or, better, on another machine. ## Step 5 - Requiring client certificates in Nginx Create a site for the API: ```bash sudo nano /etc/nginx/sites-available/api.conf ``` ```nginx server { listen 443 ssl; listen [::]:443 ssl; server_name api.your_domain; ssl_certificate /etc/nginx/mtls/server.crt; ssl_certificate_key /etc/nginx/mtls/server.key; ssl_protocols TLSv1.2 TLSv1.3; ssl_client_certificate /etc/nginx/mtls/ca.crt; ssl_verify_client on; ssl_verify_depth 2; location / { default_type text/plain; return 200 "Hello $ssl_client_s_dn\n"; } } ``` - `ssl_client_certificate` is the CA that client certificates must be signed by. - `ssl_verify_client on` rejects any request without a valid client certificate. Use `optional` if some locations must stay open, and check `$ssl_client_verify` in the protected ones. - `ssl_verify_depth 2` allows a client certificate signed directly by the root CA or by one intermediate. - `$ssl_client_s_dn` contains the client certificate subject. The `return` line is only for testing. Enable the site and reload Nginx: ```bash sudo ln -s /etc/nginx/sites-available/api.conf /etc/nginx/sites-enabled/ sudo nginx -t && sudo systemctl reload nginx ``` If UFW is active, allow HTTPS: ```bash sudo ufw allow 443/tcp ``` ## Step 6 - Testing the mTLS connection Run the tests from `~/mtls`. The `--resolve` option sends `api.your_domain` to the local server without needing DNS. Request the API with the client certificate: ```bash curl --cacert ca.crt --cert billing-service.crt --key billing-service.key --resolve api.your_domain:443:127.0.0.1 https://api.your_domain/ ``` ```output Hello CN=billing-service ``` Now try without a client certificate: ```bash curl --cacert ca.crt --resolve api.your_domain:443:127.0.0.1 https://api.your_domain/ ``` ```output