Copyright 2025 HNCloud Limited.
香港联合通讯国际有限公司
美国高防云服务器部署Nginx的详细步骤
时间 : 2025-03-06 13:51:04
编辑 : 华纳云
阅读量 : 206
对于网站管理员或者开发人员来说,部署Nginx作为web服务器搭建一个安全的网站环境是非常重要的,不仅需要对Nginx有一定的了解,还要做好在美国高防云服务器环境下的优化和安全加固工作。下面是在美国高防云服务器部署Nginx的详细步骤,涉及到常规的安装和配置,还有专属优化措施。
步骤一、安装Nginx
更新系统并安装依赖
# Ubuntu/Debian sudo apt update && sudo apt upgrade -y sudo apt install curl gnupg2 ca-certificates lsb-release software-properties-common -y # CentOS/RHEL sudo yum update -y sudo yum install epel-release -y
安装 Nginx
# Ubuntu/Debian sudo apt install nginx -y # CentOS/RHEL sudo yum install nginx -y
启动并设置开机自启
sudo systemctl start nginx sudo systemctl enable nginx
步骤二、基础配置与安全加固
配置 Nginx 基础文件:
- 主配置文件路径:/etc/nginx/nginx.conf
- 站点配置文件路径:/etc/nginx/sites-available/(Ubuntu)或 /etc/nginx/conf.d/(CentOS)
限制敏感信息暴露:
修改 nginx.conf,隐藏版本号等敏感信息:
http { server_tokens off; # 隐藏 Nginx 版本号 ... }
配置访问控制:
在站点配置文件中限制 IP 访问(仅允许信任的 IP):
server { listen 80; server_name example.com; location /admin { allow 192.168.1.0/24; # 仅允许特定 IP 段 deny all; } }
启用 HTTPS(SSL/TLS):
# 安装 Certbot sudo apt install certbot python3-certbot-nginx -y # Ubuntu sudo yum install certbot python3-certbot-nginx -y # CentOS # 获取证书(自动配置 Nginx) sudo certbot --nginx -d example.com -d www.example.com
配置 HTTP 到 HTTPS 重定向:
server { listen 80; server_name example.com www.example.com; return 301 https://$server_name$request_uri; }
修改站点配置文件:
server { listen 80; server_name example.com www.example.com; return 301 https://$server_name$request_uri; }
步骤三、配置Nginx优化措施
1. 限制连接速率:
http { limit_req_zone $binary_remote_addr zone=one:10m rate=100r/s; server { location / { limit_req zone=one burst=200 nodelay; ... } } }
2. 限制并发连接数:
http { limit_conn_zone $binary_remote_addr zone=addr:10m; server { location / { limit_conn addr 50; # 每个 IP 最多 50 个并发连接 ... } } }
3. 优化 TCP/IP 协议栈:
修改 /etc/sysctl.conf 以抵御 SYN Flood 攻击:
net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_max_syn_backlog = 4096 net.ipv4.tcp_synack_retries = 2
应用配置:
sudo sysctl -p
4. 配置缓存
server { location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ { expires 30d; add_header Cache-Control "public, no-transform"; } }
5. 启用 HTTP/2:
server { listen 443 ssl http2; ... }
步骤四、配置日志与监控
配置访问日志和错误日志:
http { access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; }
使用Fail2Ban自动封禁恶意 IP:
安装并配置Fail2Ban:
sudo apt install fail2ban -y # Ubuntu sudo yum install fail2ban -y # CentOS
创建Nginx防护规则文件 /etc/fail2ban/jail.d/nginx.conf:
[nginx-http-auth] enabled = true filter = nginx-http-auth logpath = /var/log/nginx/error.log maxretry = 3 bantime = 3600
部署好Nginx以后,需要将安全加固和与美国高防云服务器结合,进行一系列必要的优化措施,这样不仅可以确保Nginx正常运行,还能充分利用其高防特性抵御网络攻击。特别提醒,在修改任何配置前务必要进行备份,最好是使用测试命令检查语法错误,逐步应用更改减少风险。