AI协助(ChatGpt)提问:
基于 RHEL 9.5 + Nginx + Let's Encrypt 的 HTTPS 文件服务器配置,文件路径为 /home/fs/sysidc/files
,并通过端口 8888 提供文件下载服务,并且实现 安全加固
1. 安装nginx
dnf update -y
dnf install -y nginx
systemctl enable --now nginx
2. 配置 Let's Encrypt SSL 证书
dnf install -y certbot python3-certbot-nginx
使用 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 作为文件服务器
mkdir -p /home/fs/sysidc/files
chmod -R 755 /home/fs/sysidc/files
chown -R nginx:nginx /home/fs/sysidc/files
编辑 /etc/nginx/conf.d/fs_sysidc.conf
文件,确保(开启目录浏览)配置正确:
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;
}
安全起见,禁止目录浏览如下:(可选)
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;
}
systemctl restart nginx
4. 配置防火墙
启用并配置 firewalld
systemctl enable --now firewalld
firewall-cmd --add-port=8888/tcp --permanent
firewall-cmd --reload
firewall-cmd --add-icmp-block=echo-request --permanent
firewall-cmd --reload
firewall-cmd --list-all
firewall-cmd --remove-icmp-block=echo-request --permanent
firewall-cmd --reload
5. 配置 SELinux
确保 SELinux 处于 enforcing 模式,检查 SELinux 状态:
若输出是 SELinux status: disabled
,则需要编辑 /etc/selinux/config
文件,修改为:
然后重启服务器:
semanage fcontext -a -t httpd_sys_content_t "/home/fs/sysidc/files(/.*)?"
restorecon -Rv /home/fs/sysidc/files/
semanage port -a -t http_port_t -p tcp 8888
systemctl restart nginx
6. 安全加固(可选项,配置复杂未配置成功)
安装并配置 fail2ban 防止暴力破解
dnf install -y fail2ban
systemctl enable --now fail2ban
配置 Nginx 相关的 fail2ban 规则:
tee /etc/fail2ban/jail.d/nginx.conf <<EOF
[nginx] enabled = true filter = nginx logpath = /var/log/nginx/access.log maxretry = 5 bantime = 600 EOF
systemctl restart fail2ban
使用 nmap 检测端口,确认服务器只开放了 8888 端口:
nmap -p 1-65535 113.108.111.218
curl -kI https://fs.sysidc.com:8888
返回示例:
HTTP/1.1 200 OK
Server: nginx
...
7. 配置自动更新证书
创建自动续期脚本编辑 /root/renew_cert.sh
:
#!/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
或
#!/bin/bash
certbot certonly --manual --preferred-challenges dns -d fs.sysidc.com --force-renewal
systemctl restart nginx
chmod +x /root/renew_cert.sh
添加:
0 2 1 * * /root/renew_cert.sh >> /var/log/letsencrypt/renew.log 2>&1
8. 总结
Nginx 文件服务器 在 8888
端口运行,文件存放路径为 /home/fs/sysidc/files/
。
HTTPS 证书 使用 Let's Encrypt 提供,需手动续签。
SELinux 配置允许 Nginx 访问文件目录并监听 8888 端口。
fail2ban 防止暴力破解,nmap
检查端口确保服务器安全。
这个配置将使 HTTPS 文件服务器 安全且高效地运行!