nginx 配置 ssl 后无法访问 nginx: [emerg] cannot load certificate “*”: BIO
作者:mmseoamin日期:2023-12-20

nginx: [emerg] cannot load certificate “/etc/nginx/ssl/a.pem”: BIO_new_file() failed (SSL: error:0200100D:system library:fopen:Permission denied:fopen(‘/etc/nginx/ssl/a.pem’,‘r’) error:2006D002:BIO routines:BIO_new_file:system lib)

场景

nginx 配置 ssl 后无法访问

1. 确认443端口已开放
2. ssl 证书有效
3. nginx -t 检测正常

nginx -s reload 无异常,其他域名也可以正常访问

使用 systemctl status nginx 查看状态,发现异常

Sep 01 10:42:55 instance-1 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Sep 01 10:42:55 instance-1 nginx[5131]: nginx: [emerg] cannot load certificate "/etc/nginx/ssl/a.pem": BIO_new_file() failed (SSL: error:0200100D:system library:fopen:Permission deni...file:system lib)
Sep 01 10:42:55 instance-1 nginx[5131]: nginx: configuration file /etc/nginx/nginx.conf test failed
Sep 01 10:42:55 instance-1 systemd[1]: nginx.service: control process exited, code=exited status=1
Sep 01 10:42:55 instance-1 systemd[1]: Failed to start The nginx HTTP and reverse proxy server.
Sep 01 10:42:55 instance-1 systemd[1]: Unit nginx.service entered failed state.
Sep 01 10:42:55 instance-1 systemd[1]: nginx.service failed.

解决

可能是 SELinux 打开造成无权限读取证书

# 查看是否打开
sestatus -v
SELinux status:                 enabled

如果状态为disabled是关闭,没必要往下看了

# 查看
ls -lrtZ /etc/nginx/ssl/a.pem
# 恢复正确的 SELinux type
restorecon -v -R /etc/nginx/ssl/a.pem

重启nginx,应该就可以了

可能出现的其他问题

nginx -s reload 无效,可能是以下原因,建议直接使用systemctl restart nginx重启

1. nginx版本不支持 reload 命令。
2. nginx配置过程中产生了错误。
3. nginx配置文件或其他相关文件的权限不正确。