###
1、創建https的ssl證書
1.1、創建秘鑰
mkdir test && cd test
openssl genrsa -des3 -out server.key 2048
注意:生成私鑰,需要提供一個至少4位,最多1023位的密碼。
1.2、生成CSR(證書簽名請求)
openssl req -new -key server.key -out server.csr 說明:需要依次輸入國家,地區,城市,組織,組織單位,Common Name和Email。其中Common Name,可以寫自己的名字或者域名,如果要支持https,Common Name應該與域名保持一致,否則會引起瀏覽器警告。 可以將證書發送給證書頒發機構(CA),CA驗證過請求者的身份之后,會出具簽名證書,需要花錢。另外,如果只是內部或者測試需求,也可以使用OpenSSL實現自簽名。
1.3、刪除秘鑰中的密碼
openssl rsa -in server.key -out server.key 說明:如果不刪除密碼,在應用加載的時候會出現輸入密碼進行驗證的情況,不方便自動化部署。
1.4、生成自簽名證書
內部或者測試使用,只要忽略證書提醒就可以了。 openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

1.5、生成pem格式的公鑰
有些服務,需要有pem格式的證書才能正常加載,可以用下面的命令: openssl x509 -in server.crt -out server.pem -outform PEM
# 最終生成證書的文件

1.6、總結
自簽名的證書,不被瀏覽器信任,適合內部或者測試使用。生產環境老老實實去買證書吧。當然了,不限成本的請隨意。
2、nginx配置ssl證書
2.1、將證書文件放到指定目錄中
mkdir /etc/nginx/nginx_ssl/ cp /test/server.key /etc/nginx/nginx_ssl/ cp /test/server.pem /etc/nginx/nginx_ssl/
2.2、配置nginx
cat /etc/nginx/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
location / {
root /usr/share/nginx/html;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# listen [::]:80;
# server_name _;
# root /usr/share/nginx/html;
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
# error_page 404 /404.html;
# location = /404.html {
# }
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
}
# Settings for a TLS enabled server.
server {
listen 443 ssl ;
root /usr/share/nginx/html;
# listen 443 ;
# ssl on; (此種寫法會有警告提示,詳情見下文第3、步)
ssl_certificate /etc/nginx/nginx_ssl/server.pem;
ssl_certificate_key /etc/nginx/nginx_ssl/server.key;
location / {
root /usr/share/nginx/html;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# listen 443 ssl http2;
# listen [::]:443 ssl http2;
# server_name _;
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
2.3、重啟nginx
nginx -s reload
2.4、訪問效果

3、nginx 提示the "ssl" directive is deprecated, use the "listen ... ssl" directive instead
該問題是由於新版nginx采用新的方式進行監聽https請求了解決方式如下: 在listen中改為 listen 443 ssl; 刪除ssl配置 # ssl on; 解決完成前后的配置如下 解決前: server {
...
listen 443 ;
ssl on;
...
}
解決后 server {
...
listen 443 ssl ;
...
}
###