寫在前面
- 分享一些 Nginx 用戶認證、SSL 加密配置的筆記
- 博文內容涉及 Nginx 用戶認證、SSL 加密配置 Demo通過 OpenSSL 生成使用 SSL 證書、私鑰和 CSR Demo
- 理解不足小伙伴幫忙指正
這世界的一面至始至終是表象,正如另一面至始至終是意志 -----《作為意志和表象的世界》(第一篇 世界作為表象初論)
用戶認證
安裝 nginx ,配置 nginx 的用戶認證
┌──[root@vms152.liruilongs.github.io]-[~]
└─$rpm -ql nginx || yum -y install nginx
安裝版本
┌──[root@vms.154.liruilongs.github.io]-[/etc/pki/nginx]
└─$nginx -v
nginx version: nginx/1.20.1
備份修改配置文件
┌──[root@vms.154.liruilongs.github.io]-[~]
└─$cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
┌──[root@vms.154.liruilongs.github.io]-[~]
└─$vim /etc/nginx/nginx.conf
認證需要 在配置文件 server 模塊下面添加對應的配置,auth_basic 為提示信息,auth_basic_user_file 為賬密文件位置
server {
...........
auth_basic "auth-liruilong";
auth_basic_user_file /etc/nginx/pass;
安裝壓測工具,http-tools 可以創建訪問網站的用戶名和密碼
┌──[root@vms.154.liruilongs.github.io]-[~]
└─$yum -y install httpd-tools
......
┌──[root@vms.154.liruilongs.github.io]-[~]
└─$htpasswd -c /etc/nginx/pass liruilong
New password:
Re-type new password:
Adding password for user liruilong
啟動服務,確認服務啟動
┌──[root@vms.154.liruilongs.github.io]-[~]
└─$systemctl start nginx
┌──[root@vms.154.liruilongs.github.io]-[~]
└─$systemctl is-active nginx
active
訪問測試
SSL 虛擬主機配置
修改配置文件,需要把注釋的部分放開,然后在配置文件的指定的位置創建 SSL 相關密鑰,證書
┌──[root@vms.154.liruilongs.github.io]-[/etc/pki/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.
include /usr/share/nginx/modules/*.conf;
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;
# Settings for a TLS enabled server.
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name _;
root /usr/share/nginx/html;
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 {
}
}
}
┌──[root@vms.154.liruilongs.github.io]-[/etc/pki/nginx]
└─$
創建 ssl 認證相關
┌──[root@vms.154.liruilongs.github.io]-[/etc/nginx/conf.d]
└─$mkdir -p /etc/pki/nginx/
┌──[root@vms.154.liruilongs.github.io]-[/etc/pki/nginx]
└─$mkdir private
┌──[root@vms.154.liruilongs.github.io]-[/etc/pki/nginx]
└─$cd private
生成 RSA 和 ECDSA 密鑰
生成生成 RSA 密鑰,服務器私鑰用于對報文進行解密
┌──[root@vms.154.liruilongs.github.io]-[/etc/pki/nginx/private]
└─$openssl genrsa -out server.key 2048
Generating RSA private key, 2048 bit long modulus
......+++
................+++
e is 65537 (0x10001)
┌──[root@vms.154.liruilongs.github.io]-[/etc/pki/nginx/private]
└─$cd ..
其他可選項
生成 RSA 密鑰:
openssl genrsa -out example.key [bits]
打印 RSA 密鑰的文本表示:
openssl rsa -in example.key -text -noout
生成新的 RSA 密鑰并使用基于 AES CBC 256 加密的密碼短語進行加密:
openssl genrsa -aes256 -out example.key [bits]
檢查您的私鑰。如果密鑰有密碼短語,系統會提示您輸入密碼:
openssl rsa -check -in example.key
從密鑰中刪除密碼:
openssl rsa -in example.key -out example.key
使用密碼短語加密現有私鑰:
openssl rsa -des3 -in example.key -out example_with_pass.key
生成 ECDSA 密鑰。curve 將替換為:prime256v1、secp384r1、secp521r1 或任何其他支持的
openssl ecparam -genkey -name [curve] | openssl ec -out example.ec.key
創建證書簽名請求(CRS)
從現有私鑰創建 CSR
┌──[root@vms.154.liruilongs.github.io]-[/etc/pki/nginx]
└─$ls
private
┌──[root@vms.154.liruilongs.github.io]-[/etc/pki/nginx]
└─$openssl req -new -key ./private/server.key -out server.csr -subj "/CN=192.168.26.1"
可選項
在單個命令中創建沒有密碼短語的 CSR 和私鑰:
openssl req -nodes -newkey rsa:[bits] -keyout example.key -out example.csr
在命令行上提供 CSR 主題信息,而不是通過交互式提示。
openssl req -nodes -newkey rsa:[bits] -keyout example.key -out example.csr -subj "/C=UA/ST=Kharkov/L=Kharkov/O=Super Secure Company/OU=IT Department/CN=example.com"
從現有證書和私鑰創建 CSR:
openssl x509 -x509toreq -in cert.pem -out example.csr -signkey example.key
通過提供 openssl 配置文件為多域 SAN 證書生成 CSR:
openssl req -new -key example.key -out example.csr -config req.conf
配置文件 req.conf:
[req]
prompt=no
default_md = sha256
distinguished_name = dn
req_extensions = req_ext
[dn]
CN=example.com
[req_ext]
subjectAltName= @alt_names
[alt_names]
DNS.1=example.com
DNS.2=www.example.com
DNS.3=ftp.example.com
創建 X.509 證書
生成證書,使用現有的 CSR 和私鑰創建自簽名證書:
┌──[root@vms.154.liruilongs.github.io]-[/etc/pki/nginx]
└─$openssl x509 -req -days 3650 -in server.csr -signkey ./private/server.key -out server.crt
Signature ok
subject=/CN=192.168.26.1
Getting Private key
┌──[root@vms.154.liruilongs.github.io]-[/etc/pki/nginx]
└─$ls
private server.crt server.csr
┌──[root@vms.154.liruilongs.github.io]-[/etc/pki/nginx]
└─$
可選項
這里也可以從頭開始創建自簽名證書和新私鑰:
openssl req -nodes -newkey rsa:2048 -keyout example.key -out example.crt -x509 -days 365
使用您自己的“CA”證書及其私鑰簽署子證書。如果您是一家 CA 公司,這將顯示一個關于如何頒發新證書的非常簡單的示例。
openssl x509 -req -in child.csr -days 365 -CA ca.crt -CAkey ca.key -set_serial 01 -out child.crt
打印證書的文本表示
openssl x509 -in server.crt -text -noout
將證書的指紋打印為 md5、sha1、sha256 摘要:
openssl x509 -in cert.pem -fingerprint -sha256 -noout
驗證 CSR 或證書
驗證 CSR 簽名:
openssl req -in example.csr -verify
驗證私鑰是否與證書和 CSR 匹配:
openssl rsa -noout -modulus -in example.key | openssl sha256
openssl x509 -noout -modulus -in example.crt | openssl sha256
openssl req -noout -modulus -in example.csr | openssl sha256
驗證證書,前提是您在計算機上將根證書和任何中間證書配置為受信任:
openssl verify example.crt
當您有中間證書鏈時,驗證證書。根證書不是捆綁包的一部分,應該在您的機器上配置為受信任的。
openssl verify -untrusted intermediate-ca-chain.pem example.crt
驗證證書,當您有中間證書鏈和根證書時,未配置為受信任的證書。
openssl verify -CAFile root.crt -untrusted intermediate-ca-chain.pem child.crt
驗證遠程服務器提供的證書是否涵蓋給定的主機名。有助于檢查您的多域證書是否正確涵蓋了所有主機名。
openssl s_client -verify_hostname www.example.com -connect example.com:443
啟動 nginx 服務測試
┌──[root@vms.154.liruilongs.github.io]-[/etc/pki/nginx]
└─$systemctl start nginx
訪問測試,自簽名的證書
博文參考
https://dynacont.NET/documentation/linux/openssl/
https://medium.com/free-code-camp/openssl-command-cheatsheet-b441be1e8c4a
https://www.sslshopper.com/article-most-common-openssl-commands.html
https://www.digitalocean.com/community/tutorials/openssl-essentials-working-with-ssl-certificates-private-keys-and-csrs
https://kubernetes.io/zh-cn/docs/tasks/administer-cluster/certificates/