Here is a simple manual how to setup nginx to use self-signed SSL certificates with nginx web server.
Build nginx
First, we need to get nginx sources and additional modules (pcre, zlib, openssl).
cd /source $ wget http://sysoev.ru/nginx/nginx-0.6.36.tar.gz $ wget http://www.zlib.net/zlib-1.2.3.tar.gz $ wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.8.tar.gz $ wget http://www.openssl.org/source/openssl-0.9.8k.tar.gz
After, unpack all archives
$ tar -zxf nginx-0.6.36.tar.gz $ tar -zxf zlib-1.2.3.tar.gz $ tar -zxf pcre-7.8.tar.gz $ tar -zxf openssl-0.9.8k.tar.gz
Configure, make and install nginx with SSL, PCRE Regular expressions, Zlib support:
$ cd nginx-0.6.36 $ ./configure --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-zlib=../zlib-1.2.3 --with-pcre=../pcre-7.8 --with-openssl=../openssl-0.9.8k $ make && make install
Create SSL certificate
The following is an extremely simplified view of how SSL is implemented and what part the certificate plays in the entire process.
Generate a Private Key
The openssl toolkit is used to generate an RSA Private Key and CSR (Certificate Signing Request). It can also be used to generate self-signed certificates which can be used for testing purposes or internal usage.
The first step is to create your RSA Private Key. This key is a 1024 bit RSA key which is encrypted using Triple-DES and stored in a PEM format so that it is readable as ASCII text.
$ openssl genrsa -des3 -out server.key 1024 Generating RSA private key, 1024 bit long modulus .........................................................++++++ ........++++++ e is 65537 (0x10001) Enter PEM pass phrase: Verifying password - Enter PEM pass phrase:
Generate a CSR (Certificate Signing Request)
Once the private key is generated a Certificate Signing Request can be generated. The CSR is then used in one of two ways. Ideally, the CSR will be sent to a Certificate Authority, such as Thawte or Verisign who will verify the identity of the requestor and issue a signed certificate. The second option is to self-sign the CSR, which will be demonstrated in the next section.
During the generation of the CSR, you will be prompted for several pieces of information. These are the X.509 attributes of the certificate. One of the prompts will be for “Common Name (e.g., YOUR name)”. It is important that this field be filled in with the fully qualified domain name of the server to be protected by SSL. If the website to be protected will be https://domain.com, then enter domain.com at this prompt. The command to generate the CSR is as follows:
$ openssl req -new -key server.key -out server.csr Country Name (2 letter code) [GB]:US State or Province Name (full name) [Berkshire]:Illinois Locality Name (eg, city) [Newbury]:Chicago Organization Name (eg, company) [My Company Ltd]:Sosedoff Organizational Unit Name (eg, section) []:Information Technology Common Name (eg, your name or your server's hostname) []:domain.com Email Address []:YOUR.MAIL@HERE.com Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []:
Remove Passphrase from Key
One unfortunate side-effect of the pass-phrased private key is that nginx will ask for the pass-phrase each time the web server is started. Obviously this is not necessarily convenient as someone will not always be around to type in the pass-phrase, such as after a reboot or crash. It is possible to remove the Triple-DES encryption from the key, thereby no longer needing to type in a pass-phrase. If the private key is no longer encrypted, it is critical that this file only be readable by the root user! If your system is ever compromised and a third party obtains your unencrypted private key, the corresponding certificate will need to be revoked. With that being said, use the following command to remove the pass-phrase from the key:
$ cp server.key server.key.org $ openssl rsa -in server.key.org -out server.key The newly created server.key file has no more passphrase in it.
Generating a Self-Signed Certificate
At this point you will need to generate a self-signed certificate because you either don’t plan on having your certificate signed by a CA, or you wish to test your new SSL implementation while the CA is signing your certificate. This temporary certificate will generate an error in the client browser to the effect that the signing certificate authority is unknown and not trusted.
$ openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt Signature ok
Configure nginx to use SSL
Ok, now we have all necessary files to setup web server. Copy files server.key and server.crt to some folder where your configuration files located (by default, ‘/usr/local/nginx/conf’). And edit nginx.conf (or any other file that represents your site):
server {
server_name YOURNAME_HERE;
listen 443;
ssl on;
ssl_certificate path/to/certificate.crt;
ssl_certificate_key path/to/certificate.key;
}
And update server:
# /etc/init.d/nginx reloadTest the site
https://yourdomain.com/

