nginx实现https访问
- 创建服务器证书密钥文件 server.key:
[root@localhost ~]# openssl genrsa -des3 -out server.key 1024
#输入密码,确认密码
- 创建服务器证书的申请文件 server.csr
[root@localhost ~]# 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 []:admin@mycompany.com ← 电子邮箱,可随意填
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []: ← 可以不输入
An optional company name []: ← 可以不输入
- 备份一份服务器密钥文件
[root@localhost ~]# cp server.key server.key.org
- 去除文件口令
[root@localhost ~]# openssl rsa -in server.key.org -out server.key
- 生成证书文件server.crt
[root@localhost ~]# openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
- nginx配置文件(部分)
server {
listen 80;
server_name localhost;
listen 443 ssl;
ssl_certificate /usr/local/nginx/ssl/server.crt;
ssl_certificate_key /usr/local/nginx/ssl/server.key;
}
- nginx如果未开启SSL模块,配置https时提示错误
nginx[emerg]the"ssl"parameterrequiresngx_http_ssl_moule in/usr/local/nginx/conf/nginx.conf
nginx缺少http_ssl_module模块,编译安装的时候带上–with-http_ssl_module
- 服务器已经安装过nginx,但是未安装http_ssl_module模块
[root@localhost ~]# cd /root/nginx-1.14.0
[root@nginx-1.14.0 ~]# ./configure --with-http_stub_status_module --with-http_ssl_module
- 编译
[root@nginx-1.14.0 ~]# make
#不需要执行make install,否则就覆盖安装了
- 备份原有的nginx
[root@nginx-1.14.0 ~]# cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_bak
- 将刚刚编译好的nginx覆盖掉原有的nginx(nginx需要停止)
[root@nginx-1.14.0 ~]# cp ./objs/nginx /usr/local/nginx/sbin/
- 查看安装情况
[root@nginx-1.14.0 ~]# /usr/local/nginx/sbin/nginx -V
- 启动服务
[root@nginx-1.14.0 ~]# /usr/local/nginx/sbin/nginx
- 访问
版权声明:本文为weixin_45822470原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。