Data protection
NGINX

Configure the NGINX server

9min
perform the following tasks to configure the nginx server 1 | set futurex pkcs #11 environment variables in a terminal, run the following commands to set the required fxpkcs11 environment variables export fxpkcs11 module=/path/to/libfxpkcs11 so; export fxpkcs11 cfg=/path/to/fxpkcs11 cfg; be sure to modify the file path to match the location where libfxpkcs11 so and fxpkcs11 cfg files are stored on your system 2 | create a key pair on the {{ch}} using pkcs11 tool in a terminal, run the following command to create a new ecc key pair on the {{ch}} using pkcs11 tool pkcs11 tool module $fxpkcs11 module login keypairgen key type rsa 2048 label "nginx rsa privatekey" id "123456" the above pkcs11 tool command prompts for the user pin enter the {{ch}} identity password configured inside the \<crypto opr pass> tag in the fxpkcs11 cfg file if successful, the command output will list the keys that pkcs11 tool created on the {{ch}} 3 | generate a csr by using the nginx private key in a terminal, run the following command to generate a csr from the private key created on the {{ch}} for nginx using pkcs11 tool openssl req new engine pkcs11 keyform engine key "pkcs11\ object=nginx rsa privatekey" out nginx cert req pem the common name of the nginx certificate should match the ip address or hostname of the virtual host it is configured for 4 | create a self signed root certificate authority (ca) here we are creating and using a self signed root certificate authority (ca) for demonstration purposes in a production environment, you should use a secure certificate authority, such as the kmes series 3, for all private key generation and certificate signing operations in a terminal, run the following commands to generate a root private key and self signed certificate we will use this certificate to sign the nginx certificate in the next step openssl genrsa out ssl ca privatekey pem 2048 openssl req new x509 key ssl ca privatekey pem out ssl ca cert pem days 365 5 | sign the nginx csr in a terminal, run the following command to issue a signed nginx certificate using the self signed root ca created in the previous step openssl x509 req in nginx cert req pem ca ssl ca cert pem cakey ssl ca privatekey pem cacreateserial days 365 out signed nginx cert pem extensions v3 leaf the common name must be the ip address or hostname of the nginx server 6 | combine the nginx and ca certificates into a single file in a terminal, run the following commands to combine the nginx and ca certificates into a single pem file cat signed nginx cert pem > combined pem cat ssl ca pem >> combined pem 7 | configure nginx to use the certificate and its associated private key stored in {{ch}} this section covers how to modify the configuration file for an nginx virtual host configuration of a virtual host is outside the scope of this guide please reference the following documentation https //www digitalocean com/community/tutorial collections/how to install apache specific to your operating system if you do not already have a virtual host configured before making any changes, stop your nginx server using the following commands sudo systemctl stop nginx sudo service nginx stop in a text editor, open the configuration file inside the conf d/ folder in the nginx directory for the virtual host you want to configure https for and modify it as shown below server { listen 443 ssl http2; server name my website com www my website com; \# ssl certificate and key paths ssl certificate /usr/local/bin/fxpkcs11/combined pem; ssl certificate key "engine\ pkcs11\ pkcs11\ token=futurex;object=nginx rsa privatekey"; \# ssl protocols and ciphers ssl protocols tlsv1 2 tlsv1 3; ssl ciphers 'tls aes 128 gcm sha256\ tls aes 256 gcm sha384\ ecdhe rsa aes128 gcm sha256\ ecdhe rsa aes256 gcm sha384'; \# other ssl settings ssl prefer server ciphers off; ssl session cache shared\ ssl 50m; ssl session timeout 1d; ssl session tickets off; \# security headers add header strict transport security "max age=31536000" always; \# logging access log /var/log/nginx/access log; error log /var/log/nginx/error log; \# document root root /var/www/html; \# index files index index html index htm; location / { try files $uri $uri/ =404; } } the location of the signed nginx certificate specified in the ssl certificate define needs to be modified according to where it is stored on your system the object name of the nginx private key specified in the ssl certificate key define must match the label you set in the pkcs11 tool command in a previous section restart your nginx server with the following command $ sudo nginx g 'daemon off;' the "daemon off" startup parameter is required for this integration do not close the window during operation if you receive an error message on startup, check to make sure there is not already a service running on port 443 8 | confirm nginx uses the new tls certificate and private key (stored on the {{ch}} ) for https connections you can complete the following steps with the firefox web browser there might be some differences in the actions when using a different browser, but the overall intent of the process is the same in firefox, select settings > privacy & security > certificates > view certificates select authorities > import and import the combined certificate (i e , combined pem) use the option trust this certificate to identity websites browse to the ip address of the nginx website that is running over https you should see a lock icon to the left of the web address view the certificate that the website served to the browser and confirm it is the certificate configured for the nginx server