一、限制訪問Nginx的方式
1、使用密鑰登錄,禁用密碼登錄
# 打開sshd_config文件 vim /etc/ssh/sshd_config #找到下面這2行 #PasswordAuthentication yes #PermitEmptyPasswords no #修改為 PasswordAuthentication no PermitEmptyPasswords no # 重啟ssh服務 service sshd restart
2、限制Nginx伺服器的訪問來源
# 打開nginx.conf配置文件 vim /etc/nginx/nginx.conf # 在http模塊下加入下面語句,限定訪問來源 http { # .. allow 127.0.0.1; allow 192.168.0.0/16; deny all; # .. } # 重載Nginx配置 nginx -s reload
3、配置SSL/TLS協議,以加密和保護HTTP通信
# 安裝SSL/TLS模塊 yum install openssl nginx-mod-http-openssl # 啟用SSL/TLS,允許HTTPS連接 server { listen 443 ssl; server_name example.com; ssl on; ssl_certificate /path/to/cert.crt; ssl_certificate_key /path/to/cert.key; # ... } # 重載Nginx配置 nginx -s reload
二、使用Nginx內置模塊加強Web應用防禦機制
1、使用HttpLimitReqModule,用於限制請求速率
# 配置rate=5r/s,在5秒內允許最多發起5個請求 http { #.. limit_req_zone $binary_remote_addr zone=mylimit:10m rate=5r/s; server { #.. limit_req zone=mylimit burst=10 nodelay; } }
2、使用HttpLimitConnModule,用於限制客戶端的並發連接數
# 配置客戶端可同時保持的連接數 events { worker_connections 1024; } # 給特定的IP地址配置限制連接數的功能 http { #.. limit_conn_zone $binary_remote_addr zone=addr:10m; server { #.. limit_conn addr 2; } }
3、使用HttpAccessModule,用於限制訪問IP、URL等
# 在http或server塊中使用deny和allow指令 http { #.. deny 192.168.1.1; allow 192.168.1.0/24; allow 10.0.0.0/8; deny all; }
三、Nginx安全性的其他建議
1、定期更新安全補丁,以確保Nginx可以抵禦新的漏洞攻擊
# 執行命令更新系統軟體包 yum update
2、關閉Nginx不需要的模塊,以減少攻擊面
# 打開nginx.conf vim /etc/nginx/nginx.conf # 取消注釋下面這條,關閉不需要的模塊 # include /etc/nginx/modules/*.conf; # 重載Nginx配置 nginx -s reload
3、啟用防火牆,以防止未授權的訪問
# 安裝firewalld,並啟用服務 yum install firewalld systemctl start firewalld systemctl enable firewalld # 禁止所有的入站網路流量,只允許指定的埠和來源IP,一路按照命令行提示操作 firewall-cmd --permanent --add-port=80/tcp firewall-cmd --permanent --add-port=443/tcp firewall-cmd --permanent --add-source=192.168.1.0/24 firewall-cmd --reload
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/271352.html