nginx如何配置部署ssl证书的方法
发布时间:2023-10-20 点击数:598
nginx如何配置部署ssl证书的方法
保护 NGINX Web 服务器的安全对于保护当今数字环境中的在线资源至关重要。 保护您的网站将提高您网站的可信度并确保用户信息的隐私。 SSL 是一种安全套接字层协议,可保护您的数据并确保用户的隐私。 它在您的 Web 服务器和用户浏览器之间创建安全且加密的连接,以保护敏感信息,例如登录凭据和个人详细信息。
在本指南中,我们将引导您完成在 NGINX Web 服务器上设置 SSL 的过程。 在这篇文章中,我们将解释在 NGINX 上实现 SSL 的两种不同方法。
先决条件
在我们开始之前,请确保您具备以下条件:
正在运行的 NGINX Web 服务器。
指向您的服务器 IP 地址的域或子域。
root 用户或具有 sudo 权限的用户。
使用自签名 SSL 保护 NGINX
自签名证书不是由证书颁发机构签名的。 它是为内部网络和开发环境而设计的。 不适合生产环境。
以下是在 NGINX 服务器上实施自签名 SSL 的步骤。
步骤 1 - 使用以下命令创建私钥和证书签名请求 (CSR)。
openssl req -nodes -newkey rsa:2048 -keyout /etc/ssl/my-private.key -out /etc/ssl/my-request.csr
提供您的证书信息,如下所示:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
Country Name (2 letter code) \[AU]:US
State or Province Name (full name) \[Some-State]:FL
Locality Name (eg, city) \[]:newyork
Organization Name (eg, company) \[Internet Widgits Pty Ltd]:alibaba
Organizational Unit Name (eg, section) \[]:IT
Common Name (e.g. server FQDN) \[]:domain.com
Email Address \[]:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password \[]:
An optional company name \[]:
步骤 2 - 使用上述证书生成 SSL 证书。
openssl x509 -in /etc/ssl/my-request.csr -out /etc/ssl/certificate.crt -req -signkey /etc/ssl/my-private.key -days 365
您将看到以下输出。
Certificate request self-signature ok
subject=C = US, ST = FL, L = newyork, O = alibaba, OU = IT, CN = domain.com
第 3 步 - 接下来,您需要编辑 NGINX 虚拟主机配置文件并将其配置为使用生成的 SSL。
nano /etc/nginx/conf.d/your-website.conf
添加以下配置:
server {
listen 443 ssl;
server_name your-domain.com;
ssl_certificate /etc/ssl/certificate.crt;
ssl_certificate_key /etc/ssl/my-private.key;
ssl_protocols TLSv1.2;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
ssl_prefer_server_ciphers on;
\# ... other server configuration ...
}
保存并关闭文件,然后重新加载 NGINX 以应用 SSL 配置:
systemctl reload nginx
最后,打开 Web 浏览器并访问您的网站以测试 SSL。
使用商业 SSL 保护 NGINX
在此方法中,您需要 从任何受信任的证书颁发机构 (CA) 或任何 SSL 证书的授权经销商处购买 SSL 证书 。
以下是有关如何使用商业 SSL 证书保护 Nginx 的分步指南:
步骤 1 - 从任何受信任的 CA 购买 SSL 证书。 您需要在购买过程中提供有关您的域和组织的信息。
步骤 2 - 完成购买过程后,CA 将通过电子邮件向您发送 SSL 证书和中间证书。
步骤 3 - 获得所有证书后,您需要使用任何安全文件传输方法将所有文件上传到您的服务器。 将所有文件放置在默认位置 /etc/nginx/ssl/ 中。
步骤 4 - 编辑您网站的 NGINX 虚拟主机配置文件。
nano /etc/nginx/conf.d/your-website.conf
添加以下配置来定义 SSL 证书的路径。
server {
listen 443 ssl;
server_name your-domain.com;
ssl_certificate /etc/nginx/ssl/your_domain.crt;
ssl_certificate_key /etc/nginx/ssl/your_domain.key;
ssl_trusted_certificate /etc/nginx/ssl/intermediate.crt;
\# ... other server configuration ...
}
完成后保存并关闭文件,然后重新启动 NGINX 服务以应用更改。
systemctl restart nginx
保护 NGINX Web 服务器的安全对于保护当今数字环境中的在线资源至关重要。 保护您的网站将提高您网站的可信度并确保用户信息的隐私。 SSL 是一种安全套接字层协议,可保护您的数据并确保用户的隐私。 它在您的 Web 服务器和用户浏览器之间创建安全且加密的连接,以保护敏感信息,例如登录凭据和个人详细信息。
在本指南中,我们将引导您完成在 NGINX Web 服务器上设置 SSL 的过程。 在这篇文章中,我们将解释在 NGINX 上实现 SSL 的两种不同方法。
先决条件
在我们开始之前,请确保您具备以下条件:
正在运行的 NGINX Web 服务器。
指向您的服务器 IP 地址的域或子域。
root 用户或具有 sudo 权限的用户。
使用自签名 SSL 保护 NGINX
自签名证书不是由证书颁发机构签名的。 它是为内部网络和开发环境而设计的。 不适合生产环境。
以下是在 NGINX 服务器上实施自签名 SSL 的步骤。
步骤 1 - 使用以下命令创建私钥和证书签名请求 (CSR)。
openssl req -nodes -newkey rsa:2048 -keyout /etc/ssl/my-private.key -out /etc/ssl/my-request.csr
提供您的证书信息,如下所示:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
Country Name (2 letter code) \[AU]:US
State or Province Name (full name) \[Some-State]:FL
Locality Name (eg, city) \[]:newyork
Organization Name (eg, company) \[Internet Widgits Pty Ltd]:alibaba
Organizational Unit Name (eg, section) \[]:IT
Common Name (e.g. server FQDN) \[]:domain.com
Email Address \[]:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password \[]:
An optional company name \[]:
步骤 2 - 使用上述证书生成 SSL 证书。
openssl x509 -in /etc/ssl/my-request.csr -out /etc/ssl/certificate.crt -req -signkey /etc/ssl/my-private.key -days 365
您将看到以下输出。
Certificate request self-signature ok
subject=C = US, ST = FL, L = newyork, O = alibaba, OU = IT, CN = domain.com
第 3 步 - 接下来,您需要编辑 NGINX 虚拟主机配置文件并将其配置为使用生成的 SSL。
nano /etc/nginx/conf.d/your-website.conf
添加以下配置:
server {
listen 443 ssl;
server_name your-domain.com;
ssl_certificate /etc/ssl/certificate.crt;
ssl_certificate_key /etc/ssl/my-private.key;
ssl_protocols TLSv1.2;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
ssl_prefer_server_ciphers on;
\# ... other server configuration ...
}
保存并关闭文件,然后重新加载 NGINX 以应用 SSL 配置:
systemctl reload nginx
最后,打开 Web 浏览器并访问您的网站以测试 SSL。
使用商业 SSL 保护 NGINX
在此方法中,您需要 从任何受信任的证书颁发机构 (CA) 或任何 SSL 证书的授权经销商处购买 SSL 证书 。
以下是有关如何使用商业 SSL 证书保护 Nginx 的分步指南:
步骤 1 - 从任何受信任的 CA 购买 SSL 证书。 您需要在购买过程中提供有关您的域和组织的信息。
步骤 2 - 完成购买过程后,CA 将通过电子邮件向您发送 SSL 证书和中间证书。
步骤 3 - 获得所有证书后,您需要使用任何安全文件传输方法将所有文件上传到您的服务器。 将所有文件放置在默认位置 /etc/nginx/ssl/ 中。
步骤 4 - 编辑您网站的 NGINX 虚拟主机配置文件。
nano /etc/nginx/conf.d/your-website.conf
添加以下配置来定义 SSL 证书的路径。
server {
listen 443 ssl;
server_name your-domain.com;
ssl_certificate /etc/nginx/ssl/your_domain.crt;
ssl_certificate_key /etc/nginx/ssl/your_domain.key;
ssl_trusted_certificate /etc/nginx/ssl/intermediate.crt;
\# ... other server configuration ...
}
完成后保存并关闭文件,然后重新启动 NGINX 服务以应用更改。
systemctl restart nginx
第 5 步 - 打开 Web 浏览器并使用 URL https://your-domain.com 安全地访问您的网站。 您还可以使用 SSL Lab 等在线 SSL 测试工具来验证您的 SSL。
我们是阿里云15年合作伙伴北京优胜智连,通过我们购买阿里云产品享受折扣优惠,北京专线:132-6161-6125、全国市话:400-150-1900 ,联系客服获取优惠价格,请访问阿里云代理https://www.zhiy.com.cn/product/aliyun/23.html享更多优惠!