nginx.conf 配置ssl
时间: 2023-10-18 17:59:58 浏览: 57
在 Nginx 的配置文件(通常是 `/etc/nginx/nginx.conf` 或 `/etc/nginx/conf.d/default.conf`)中,你需要进行以下配置来启用 SSL:
1. 找到 `http` 块的开头,并添加以下行来加载 SSL 模块:
```
http {
...
# 加载 SSL 模块
include /etc/nginx/modules-enabled/*.conf;
...
}
```
2. 在 `server` 块中,添加以下行来配置 SSL:
```
server {
...
# 监听 443 端口,启用 SSL
listen 443 ssl;
# 填写你的域名
server_name your_domain.com;
# SSL 证书和私钥的路径
ssl_certificate /path/to/your_certificate.pem;
ssl_certificate_key /path/to/your_private_key.pem;
# 可选:配置其他 SSL 参数
# ssl_protocols TLSv1.2 TLSv1.3;
# ssl_ciphers HIGH:!aNULL:!MD5;
...
}
```
确保将 `your_domain.com` 替换为你的域名,并将证书文件的路径替换为实际的路径。
3. 可选:如果你希望将 HTTP 请求自动重定向到 HTTPS,可以在 `server` 块中添加以下行:
```
server {
...
# HTTP 请求自动重定向到 HTTPS
if ($scheme != "https") {
return 301 https://$host$request_uri;
}
...
}
```
4. 保存并退出配置文件。
5. 检查配置文件语法:使用 `nginx -t` 命令检查配置文件语法是否正确,如果没有错误提示,则继续下一步。
6. 重新加载 Nginx 配置:使用 `sudo systemctl reload nginx` 命令重新加载 Nginx 配置。
现在,你的 Nginx 配置已经启用了 SSL,可以通过 HTTPS 访问你的网站。
相关推荐
![conf](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)