在nginx里面把http配置成https
时间: 2023-10-03 10:03:57 浏览: 50
在 Nginx 中将 HTTP 配置为 HTTPS 需要进行以下步骤:
1. 安装 SSL 证书
首先,你需要在服务器上安装 SSL 证书。你可以从证书颁发机构(CA)获得 SSL 证书,或者你可以生成自签名证书。
2. 配置 HTTPS 服务器
在 Nginx 配置文件中添加 HTTPS 服务器块。以下是一个简单的 HTTPS 服务器块配置:
```
server {
listen 443;
server_name example.com;
ssl on;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
location / {
# 配置 HTTPS 服务器的其他选项
}
}
```
- `listen 443;` 指定 HTTPS 服务器监听的端口号为 443。
- `server_name example.com;` 指定域名或 IP 地址。
- `ssl on;` 启用 SSL。
- `ssl_certificate` 和 `ssl_certificate_key` 分别指定 SSL 证书和私钥的路径。
3. 配置 HTTP 服务器
在 Nginx 配置文件中添加 HTTP 服务器块,并将其重定向到 HTTPS 服务器。以下是一个简单的 HTTP 服务器块配置:
```
server {
listen 80;
server_name example.com;
return 301 https://$server_name$request_uri;
}
```
- `listen 80;` 指定 HTTP 服务器监听的端口号为 80。
- `server_name example.com;` 指定域名或 IP 地址。
- `return 301 https://$server_name$request_uri;` 将所有 HTTP 请求重定向到 HTTPS 服务器。
4. 重新加载 Nginx
在完成以上配置后,使用以下命令重新加载 Nginx:
```
sudo systemctl reload nginx
```
现在,你的 Nginx 已经配置为使用 HTTPS 了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)