1. 程式人生 > >Nginx配置https和wss

Nginx配置https和wss

證書和私鑰的生成

注意:一般生成的目錄,應該放在nginx/conf/ssl目錄

1.建立伺服器證書金鑰檔案 server.key:

openssl genrsa -des3 -out server.key 1024

輸入密碼,確認密碼,自己隨便定義,但是要記住,後面會用到。

2.建立伺服器證書的申請檔案 server.csr

openssl req -new -key server.key -out server.csr

輸出內容為: Enter pass phrase for root.key: ← 輸入前面建立的密碼  Country Name (2 letter code) [AU]:CN ← 國家代號,中國輸入CN  State or Province Name (full name) [Some-State]:BeiJing ← 省的全名,拼音  Locality Name (eg, city) []:BeiJing ← 市的全名,拼音  Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名  Organizational Unit Name (eg, section) []: ← 可以不輸入  Common Name (eg, YOUR name) []: ← 此時不輸入  Email Address []:

[email protected] ← 電子郵箱,可隨意填 Please enter the following ‘extra’ attributes  to be sent with your certificate request  A challenge password []: ← 可以不輸入  An optional company name []: ← 可以不輸入

4.備份一份伺服器金鑰檔案

cp server.key server.key.org

5.去除檔案口令

openssl rsa -in server.key.org -out server.key

6.生成證書檔案server.crt

openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

配置檔案

server {     listen 443;     server_name localhost;     ssl on;     root html;     index index.html index.htm;         ssl_certificate      /usr/local/nginx/https.crt;         ssl_certificate_key  /usr/local/nginx/https.key;     ssl_session_timeout 5m;     ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;     ssl_protocols TLSv1 TLSv1.1 TLSv1.2;     ssl_prefer_server_ciphers on;     location / {         root html;         index index.html index.htm;     } }