检查Ubuntu服务器上Nginx的SSL状态,需从证书有效性、配置正确性、连接状态、服务运行情况等多维度验证。以下是具体方法:
通过查看Nginx配置文件,确认SSL证书路径、监听端口等关键指令是否配置无误。
# 查找Nginx配置文件(通常位于/etc/nginx/sites-available/或/etc/nginx/conf.d/)
ls /etc/nginx/sites-available/
# 检查指定域名的配置文件(如yourdomain.com)
sudo nano /etc/nginx/sites-available/yourdomain.com
重点确认以下配置是否存在且路径正确:
server {
listen 443 ssl; # 必须监听443端口
server_name yourdomain.com www.yourdomain.com;
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem; # 证书路径
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem; # 私钥路径
# 其他SSL配置(如ssl_protocols、ssl_ciphers等)
}
保存后测试配置语法是否正确:
sudo nginx -t
若输出configuration file /etc/nginx/nginx.conf syntax is ok,则配置无误。
使用openssl命令获取证书的颁发者、有效期、加密套件等信息,确保证书未过期且有效。
# 查看证书有效期(关键信息:notBefore、notAfter)
sudo openssl s_client -connect yourdomain.com:443 -servername yourdomain.com </dev/null 2>/dev/null | openssl x509 -noout -dates
# 查看证书完整详情(包括颁发者、主题、加密算法等)
sudo openssl s_client -connect yourdomain.com:443 -servername yourdomain.com </dev/null 2>/dev/null | openssl x509 -noout -text
notAfter日期早于当前日期,需及时续期证书(如使用Certbot续期);Verify return code不为0,则表示证书无效。通过curl命令模拟浏览器访问,检查HTTPS连接是否正常、TLS协议版本是否兼容。
# 检查HTTP到HTTPS的重定向(若配置了301跳转)
curl -I http://yourdomain.com
# 期望输出包含:HTTP/1.1 301 Moved Permanently 和 Location: https://yourdomain.com/
# 检查HTTPS连接详情(包括TLS版本、加密套件、证书链)
curl -v https://yourdomain.com
SSL handshake成功的信息(如TLSv1.3或TLSv1.2);SSL_ERROR_SYSCALL或certificate verify failed,需检查证书配置或网络连接。确认Nginx是否编译了ngx_http_ssl_module模块(默认编译),若未启用则无法支持SSL。
# 查看Nginx编译选项
nginx -V 2>&1 | grep -o with-http_ssl_module
with-http_ssl_module,则表示模块已启用;若启用了ngx_http_stub_status_module模块,可通过HTTP接口查看SSL连接数(需配置访问权限)。
# 编辑Nginx配置文件(如/etc/nginx/nginx.conf)
sudo nano /etc/nginx/nginx.conf
在server块中添加以下内容(仅允许本地访问):
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
重启Nginx使配置生效:
sudo systemctl restart nginx
通过浏览器或curl访问状态页面:
curl http://127.0.0.1/nginx_status
输出中的Writing列即为当前正在处理的SSL连接数(需配合stub_status模块使用)。
通过第三方工具(如SSL Labs的SSL Server Test)获取SSL配置的详细评分和建议,覆盖证书、协议、加密套件等全方位检查。
Submit;通过以上方法,可全面检查Ubuntu Nginx的SSL状态,确保SSL配置正确、证书有效、连接安全。