# 3. 搭建HTTPS 文件服务器

AI协助（ChatGpt）提问：

基于 RHEL 9.5 + Nginx + Let's Encrypt 的 HTTPS 文件服务器配置，文件路径为 `/home/fs/sysidc/files`，并通过端口 8888 提供文件下载服务，并且实现 安全加固

### 1. 安装nginx

1. 更新系统并安装 Nginx：

```bash
dnf update -y
dnf install -y nginx
```

2. 启动 Nginx 并设置为开机启动：

```bash
systemctl enable --now nginx
```

3. 检查 Nginx 状态，确认是否正常运行：

```bash
systemctl status nginx
```

### 2. 配置 Let's Encrypt SSL 证书

1. 安装 Certbot

```bash
dnf install -y certbot python3-certbot-nginx
```

2. 申请证书

* 使用 DNS 验证方式申请证书，按照提示在 DNS 配置中添加 TXT 记录：

```
certbot certonly --manual --preferred-challenges dns -d fs.sysidc.com
```

* 生成证书后，证书和私钥将存储在以下路径：

证书：`/etc/letsencrypt/live/fs.sysidc.com/fullchain.pem`

私钥：`/etc/letsencrypt/live/fs.sysidc.com/privkey.pem`

### 3.配置 Nginx 作为文件服务器

1. 创建存放文件的目录

```bash
mkdir -p /home/fs/sysidc/files
chmod -R 755 /home/fs/sysidc/files
chown -R nginx:nginx /home/fs/sysidc/files
```

2. 配置 Nginx

编辑 `/etc/nginx/conf.d/fs_sysidc.conf` 文件，确保（**开启目录浏览**）配置正确：

```bash
server {
    listen 8888 ssl;
    server_name fs.sysidc.com;

    ssl_certificate /etc/letsencrypt/live/fs.sysidc.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/fs.sysidc.com/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;

    root /home/fs/sysidc/files;
    index index.html;
    
    autoindex on;
    autoindex_exact_size off;
    autoindex_format html;
    autoindex_localtime on;

    location / {
        allow all;
        limit_except GET {
            deny all;
        }
        try_files $uri $uri/ =404;
    }

    # 禁止执行脚本，防止安全风险
    location ~* \.(php|pl|py|cgi|sh)$ {
        return 403;
    }

    # 限制 .htaccess, .git, .svn, .DS_Store 文件访问
    location ~ /\. {
        deny all;
    }

    error_log /var/log/nginx/fs_sysidc_error.log;
    access_log /var/log/nginx/fs_sysidc_access.log;
}

```

&#x20;安全起见，**禁止目录浏览**如下：（可选）

```bash
server {
    listen 8888 ssl http2;
    server_name fs.sysidc.com;

    ssl_certificate /etc/letsencrypt/live/fs.sysidc.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/fs.sysidc.com/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;

    root /home/fs/sysidc/files;
    index index.html index.htm;

    location / {
        autoindex off;
        try_files $uri $uri/ =404;
    }

    # 禁止执行脚本，防止安全风险
    location ~* \.(php|pl|py|cgi|sh)$ {
        return 403;
    }

    # 限制 .htaccess, .git, .svn, .DS_Store 文件访问
    location ~ /\. {
        deny all;
    }

    error_log /var/log/nginx/fs_sysidc_error.log;
    access_log /var/log/nginx/fs_sysidc_access.log;
}

```

3. 检查 Nginx 配置并重启

* 检查 Nginx 配置是否正确：

```
nginx -t
```

* 重启 Nginx 使配置生效：

```
systemctl restart nginx
```

### 4. 配置防火墙

启用并配置 firewalld

1. 启动 firewalld 并使其开机启动：

```
systemctl enable --now firewalld
```

2. 开放 8888 端口并重新加载防火墙配置：

```
firewall-cmd --add-port=8888/tcp --permanent
firewall-cmd --reload
```

3. 禁止 ICMP 请求（即禁止 ping）：

```bash
firewall-cmd --add-icmp-block=echo-request --permanent
firewall-cmd --reload
```

4. 检查防火墙状态和开放的端口：

```bash
firewall-cmd --list-all
```

5. 开放 Ping（如需）

```bash
firewall-cmd --remove-icmp-block=echo-request --permanent
firewall-cmd --reload
```

### 5. 配置 SELinux

1. 确保 SELinux 处于 enforcing 模式，检查 SELinux 状态：

```
sestatus
```

若输出是 `SELinux status: disabled`，则需要编辑 `/etc/selinux/config` 文件，修改为：

```
SELINUX=enforcing
```

然后重启服务器：

```
reboot
```

2. 允许 Nginx 访问文件目录

```bash
semanage fcontext -a -t httpd_sys_content_t "/home/fs/sysidc/files(/.*)?"
restorecon -Rv /home/fs/sysidc/files/
```

3. 允许 Nginx 监听 8888 端口

```bash
semanage port -a -t http_port_t -p tcp 8888
systemctl restart nginx
```

### 6. 安全加固（可选项，配置复杂未配置成功）

安装并配置 fail2ban 防止暴力破解

1. 安装 fail2ban：

```bash
dnf install -y fail2ban
```

2. 启动并设置为开机启动：

```bash
systemctl enable --now fail2ban
```

3. 配置 Nginx 相关的 fail2ban 规则：

```bash
tee /etc/fail2ban/jail.d/nginx.conf <<EOF
```

```
[nginx] enabled = true filter = nginx logpath = /var/log/nginx/access.log maxretry = 5 bantime = 600 EOF
```

4. 重启 fail2ban：

```bash
systemctl restart fail2ban
```

5. 使用 nmap 检测端口，确认服务器只开放了 8888 端口：

```
nmap -p 1-65535 113.108.111.218
```

6. 使用 curl 测试 HTTPS 访问

```bash
curl -kI https://fs.sysidc.com:8888
```

返回示例：

```python
HTTP/1.1 200 OK
Server: nginx
...
```

### 7. 配置自动更新证书

1. 创建自动续期脚本编辑 `/root/renew_cert.sh`：

```bash
#!/bin/bash

DOMAIN="fs.sysidc.com"
LOG_FILE="/var/log/letsencrypt/renew.log"

echo "$(date) - 开始 SSL 证书续期" >> "$LOG_FILE"

# 交互式方式，手动更新 DNS 记录
certbot certonly --manual --preferred-challenges dns -d "$DOMAIN" --force-renewal

# 检查 certbot 执行状态
if [ $? -eq 0 ]; then
    echo "$(date) - SSL 证书续期成功，检查 Nginx 配置..." >> "$LOG_FILE"

    # 检查 Nginx 配置是否正确
    nginx -t
    if [ $? -eq 0 ]; then
        echo "$(date) - Nginx 配置正确，重启 Nginx" >> "$LOG_FILE"
        systemctl restart nginx
    else
        echo "$(date) - Nginx 配置错误，请手动检查！" >> "$LOG_FILE"
    fi
else
    echo "$(date) - SSL 证书续期失败，请检查 TXT 记录并手动更新" >> "$LOG_FILE"
fi

```

或

```bash
#!/bin/bash
certbot certonly --manual --preferred-challenges dns -d fs.sysidc.com --force-renewal
systemctl restart nginx
```

2. 赋予脚本执行权限

```bash
chmod +x /root/renew_cert.sh
```

3. 设置定时任务

```bash
crontab -e
```

添加：

```bash
0 2 1 * * /root/renew_cert.sh >> /var/log/letsencrypt/renew.log 2>&1
```

### 8. 总结

1. **Nginx 文件服务器** 在 `8888` 端口运行，文件存放路径为 `/home/fs/sysidc/files/`。
2. **HTTPS 证书** 使用 Let's Encrypt 提供，需手动续签。
3. **防火墙** 只开放 8888 端口，禁止 Ping。
4. **SELinux** 配置允许 Nginx 访问文件目录并监听 8888 端口。
5. **fail2ban** 防止暴力破解，`nmap` 检查端口确保服务器安全。

***

这个配置将使 **HTTPS 文件服务器** 安全且高效地运行！
