检查环境
- 确认nginx是否安装了SSL模块,如下的命令:
nginx -V
我已经安装ssl模块,所以有ssl的一些信息。
证书和私钥的生成
注意:一般生成的目录,应该放在nginx/ssl目录
- 创建服务器证书密钥文件 server.key:
openssl genrsa -des3 -out server.key 1024
输入密码,确认密码,自己随便定义,但是要记住,后面会用到。
- 创建服务器证书的申请文件 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]:Mskd Dofs ← 公司英文名
Organizational Unit Name (eg, section) []: ← 可以不输入
Common Name (eg, YOUR name) []: ← 此时不输入
Email Address []:admin@mycompany.com ← 电子邮箱,可随意填
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []: ← 可以不输入
An optional company name []: ← 可以不输入
- .备份一份服务器密钥文件
cp server.key server.key.org - .去除文件口令
openssl rsa -in server.key.org -out server.key - .生成证书文件server.crt
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
[root@csp-lab-gateway ssl]# ll
total 16
-rw-r--r-- 1 root root 863 Aug 4 11:42 server.crt
-rw-r--r-- 1 root root 655 Aug 4 11:41 server.csr
-rw-r--r-- 1 root root 891 Aug 4 11:42 server.key
-rw-r--r-- 1 root root 963 Aug 4 11:42 server.key.org
配置文件
下面为配置文件 /etc/nginx/15443.conf
[root@csp-lab-gateway conf.d]# cat 15443.conf
server {
listen 15443 ssl;
# 监听443端口, 开启ssl(必须)
#server_name domain.com;
client_max_body_size 501M;
# ssl on; # 不建议使用! 该指令与listen中ssl参数功能相同.
# 引用ssl证书(必须,如果放在nginx/conf/ssl下可以用相对路径,其他位置必须用绝对路径)
ssl_certificate ssl/server.crt;
ssl_certificate_key ssl/server.key;
# 协议优化(可选,优化https协议,增强安全性)
#ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
# 自动跳转到HTTPS
#if ($server_port = 80) {
# rewrite ^(.*)$ https://$host$1 permanent;
#}
location / {
proxy_pass http://127.0.0.1:15100;
proxy_set_header X-Forwarded-For $remote_addr;
# root html;
# index index.html index.htm;
}
}
[root@csp-lab-gateway conf.d]#
- 重启nginx
systemctl restart nginx
配置成功验证登录
- 应用绑定15100端口
- 在浏览器登录访问
注:页面访问的端口号要写15443