標籤:nginx、https、ssl
前提:
1、主機要先安裝openssl
2、編譯安裝nginx時,要加上--with-openssl和--with-http_ssl_module
1、產生自簽字認證
[[email protected] /]# openssl req -new -x509 -keyout /root/ca.key -out /root/ca.crtGenerating a 2048 bit RSA private key.............+++...................................+++writing new private key to ‘/root/ca.key‘Enter PEM pass phrase: #輸入密鑰保護密碼Verifying - Enter PEM pass phrase: #確認密碼保護密碼-----You are about to be asked to enter information that will be incorporatedinto your certificate request.What you are about to enter is what is called a Distinguished Name or a DN.There are quite a few fields but you can leave some blankFor some fields there will be a default value,If you enter ‘.‘, the field will be left blank.-----Country Name (2 letter code) [XX]:CNState or Province Name (full name) []:guangzhouLocality Name (eg, city) [Default City]:guangzhouOrganization Name (eg, company) [Default Company Ltd]:lzsOrganizational Unit Name (eg, section) []:itCommon Name (eg, your name or your server‘s hostname) []:101.lzs.comEmail Address []:[email protected]
2、修改設定檔openssl.cnf
vim /etc/pki/tls/openssl.cnf[ ca ]default_ca = CA_default # The default ca section####################################################################[ CA_default ]dir = /etc/pki/CA #認證的根目錄,要記住這個目錄certs = $dir/certs crl_dir = $dir/crl database = $dir/index.txt #unique_subject = no new_certs_dir = $dir/newcerts certificate = $dir/ca.crt # 修改這裡,表示簽名時使用的認證serial = $dir/serial crlnumber = $dir/crlnumber crl = $dir/crl.pem private_key = $dir/private/cakey.pemRANDFILE = $dir/private/.rand
3、複製認證到認證根目錄/etc/pki/CA下,並在該目錄下建立空檔案index.txt和serial,並向serial輸入”01“
cd /etc/pki/CAcp /root/ca.crt .touch index.txttouch serialecho "01" >serial
4、產生伺服器RSA私密金鑰/root/server.key
openssl genrsa -des3 -out /root/server.key 1024
5、為私密金鑰去除口令
openssl rsa -in /root/server.key -out /root/server_nopwd.key
5、產生認證請求檔案/root/server.csr
[[email protected] /]# openssl req -new -key /root/server.key -out /root/server.csrEnter pass phrase for /root/server.key: #輸入第4步產生的密鑰的保護密碼You are about to be asked to enter information that will be incorporatedinto your certificate request.What you are about to enter is what is called a Distinguished Name or a DN.There are quite a few fields but you can leave some blankFor some fields there will be a default value,If you enter ‘.‘, the field will be left blank.--------下面這部分應該和建立私人認證時填的一樣------------------------Country Name (2 letter code) [XX]:CNState or Province Name (full name) []:guangzhouLocality Name (eg, city) [Default City]:guangzhouOrganization Name (eg, company) [Default Company Ltd]:lzsOrganizational Unit Name (eg, section) []:itCommon Name (eg, your name or your server‘s hostname) []:101.lzs.com----------------------------------------------------------------Email Address []:[email protected]Please enter the following ‘extra‘ attributesto be sent with your certificate requestA challenge password []:www.lzs.comAn optional company name []:lzs
6、用私人認證給認證請求檔案/root/server.csr簽名
[[email protected] CA]# openssl ca -in /root/server.csr -out /root/server.crt -cert /root/ca.crt -keyfile /root/ca.key -config /etc/pki/tls/openssl.cnfUsing configuration from /etc/pki/tls/openssl.cnfEnter pass phrase for ca.key:Check that the request matches the signatureSignature okCertificate Details: Serial Number: 1 (0x1) Validity Not Before: Aug 31 14:09:15 2016 GMT Not After : Aug 31 14:09:15 2017 GMT Subject: countryName = CN stateOrProvinceName = guangzhou organizationName = lzs organizationalUnitName = it commonName = 101.lzs.com emailAddress = [email protected] X509v3 extensions: X509v3 Basic Constraints: CA:FALSE Netscape Comment: OpenSSL Generated Certificate X509v3 Subject Key Identifier: 18:80:30:B7:C6:11:61:AE:F3:62:9D:D0:33:D9:97:CB:45:5A:31:91 X509v3 Authority Key Identifier: keyid:DA:99:4B:9B:29:A8:D8:14:54:FA:52:4B:1E:C3:E0:81:C6:A6:EF:42Certificate is to be certified until Aug 31 14:09:15 2017 GMT (365 days)Sign the certificate? [y/n]:yes1 out of 1 certificate requests certified, commit? [y/n]yWrite out database with 1 new entriesData Base Updated
7、編輯nginx設定檔/etc/nginx/nginx.conf
-------在設定檔的特定地區加入/修改下面內容server { listen 443 ssl; #設定監聽的連接埠 server_name lzs; ssl on; ssl_certificate /root/server.crt; ssl_certificate_key /root/server_nopwd.key;
8、重啟服務
~~~~完成,在用戶端上輸入https://x.x.x.x即可訪問
本文出自 “90後” 部落格,請務必保留此出處http://lzs66.blog.51cto.com/9607068/1844977
nginx配置https網站