nginx使用ssl模块配置支持HTTPS访问【解决ssl错误】
2023-09-11 14:19:50 时间
默认情况下ssl模块并未被安装,如果要使用该模块则需要在编译nginx时指定–with-http_ssl_module参数.
需求:
做一个网站域名为 www.localhost.cn 要求通过https://www.localhost.cn进行访问.
10.10.100.8 www.localhost.cn
实验步骤:
1.首先确保机器上安装了openssl和openssl-devel
1
2
|
#yum install openssl #yum install openssl-devel |
2.创建服务器私钥,命令会让你输入一个口令:
1
2
|
openssl genrsa -des3 - out server.key 1024<br> //生成私钥 #因为以后要给nginx使用。每次reload nginx配置时候都要你验证这个PAM密码的.由于生成时候必须输入密码,你可以输入后 再删掉。 |
3.创建签名请求的证书(CSR):
1
|
openssl req - new -key server.key - out server.csr //生成证书颁发机构,用于颁发公钥 |
4.在加载SSL支持的Nginx并使用上述私钥时除去必须的口令:
1
2
|
cp server.key server.key.org openssl rsa - in server.key.org - out server.key //除去密码以便reload询问时不需要密码 |
5.配置nginx
最后标记证书使用上述私钥和CSR:
1
|
openssl x509 -req -days 365 - in server.csr -signkey server.key - out server.crt |
6.修改Nginx配置文件,让其包含新标记的证书和私钥:
1
2
3
4
5
|
#vim /usr/local/nginx/conf/nginx.conf http { include server/*.cn; } |
7.修改Nginx配置文件,让其包含新标记的证书和私钥:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#vim /usr/local/nginx/server/www.localhost.cn server { listen 443; //监听端口为443 server_name www.localhost.cn; ssl on ; //开启ssl ssl_certificate /etc/pki/tls/certs/server.crt; //证书位置 ssl_certificate_key /etc/pki/tls/certs/server.key; //私钥位置 ssl_session_timeout 5m; ssl_protocols SSLv2 SSLv3 TLSv1; //指定密码为openssl支持的格式 ssl_ciphers HIGH:!aNULL:!MD5; //密码加密方式 ssl_prefer_server_ciphers on ; //依赖SSLv3和TLSv1协议的服务器密码将优先于客户端密码 location / { root html; //根目录的相对位置 index index.html index.htm; } } |
8.启动nginx服务器.
1
|
#/usr/local/nginx/sbin/nginx -s reload //如果环境允许的话直接杀掉进程在启动nginx |
如果出现“[emerg] 10464#0: unknown directive "ssl" in /usr/local/nginx-0.6.32/conf/nginx.conf:74”则说明没有将ssl模块编译进nginx,在configure的时候加上“--with-http_ssl_module”即可
1
|
如:[root@localhost nginx-1.4.4]# ./configure --prefix=/usr/local/nginx --user=www -- group =www --with-http_stub_status_module --with-http_ssl_module |
9.测试网站是否能够通过https访问
1
|
https: //www.localhost.cn |
另外还可以加入如下代码实现80端口重定向到443
1
2
3
4
5
6
|
server { listen 80; server_name www.localhost.cn; #rewrite ^(.*) https://$server_name$1 permanent; rewrite ^(.*)$ https: //$host$1 permanent; } |
过以下配置,可以设置一个虚拟主机同时支持HTTP和HTTPS
1
2
|
listen 80; listen 443 default ssl; |
相关文章
- win10安装Anaconda,详细版本,避坑指南,报ssl错误或者超时错误等等
- pip is configured with locations that require TLS/SSL, however the ssl module in Python is not avail
- unable to connect to ssl://gateway.sandbox.push.apple.com:2195 错误
- Google Earth Engine(GEE)——缩放错误指南(聚合过多、超出内存、超出最大像素和超出内存限制)!
- 观点 | 7个测试自动化的错误及解决方案实例
- Oracle SQL Developer出现错误 【ora-28002:the password will expire within 7 days】的解决办法
- npm install 报 npm ERR! cb()never called!的错误
- 执行buildman --fetch-arch arm提示"urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)>"如何处理?
- Nessus提示API Disabled错误
- 如果nginx启动失败,错误解决
- 【Unity3D日常BUG】Unity3D中导入Obi插件出现的错误:DLLNotFouindException:libOni
- STM32引用“CmBacktrace”: ARM Cortex-M 系列 MCU 错误追踪库
- Hadoop搭建,上传文件时出现错误,没有到主机的路由
- esp-aliyun undefined reference to `pthread_create‘错误
- 【文件处理】——字典写入json文件或TXT文件,读取文件中的字典&TypeError: Object of type ‘ndarray‘ is not JSON serializable错误解决方法
- pchar,pwidechar,pansichar作为返回参数时内存访问错误
- 14. 成功解决:ssl_client_socket_impl.cc(992) handshake failed;returned -1,SSL error code 1,net_error -103