Nginx是一個輕量級的,高性能的Web服務器以及反向代理和郵箱代理服務器。它運行在UNIX、GNU、linux、BSD、Mac OS X、Solaris和Windows各種版本。根據調查統計數據顯示,當前全球超過6%的網站使用Nginx Web服務器來管理Web網站應用。
為了保證基于Nginx的Web應用安全, 我結合之前在項目中遇到的相應問題進行總結和提煉,針對Nginx常用安全配置進行梳理。
一、基礎安全配置
-
隱藏版本信息:編輯 Nginx 源碼中的
/http/ngx_http_header_filter_module.c
文件,找到以下兩行并修改為自定義內容:static char ngx_http_server_string[] = “Server: Ninja Web Server” ; static char ngx_http_server_full_string[] = “Server: Ninja Web Server” ;
然后重新編譯安裝 Nginx。
-
限制模塊加載:在編譯 Nginx 時,禁用不必要的模塊,例如:
./configure -without-http_autoindex_module -without-http_ssi_module make make install
-
限制并發連接:在
nginx.conf
文件中,使用limit_zone
和limit_conn
指令限制每個 IP 的并發連接數:limit_zone slimits $binary_remote_addr 5m; limit_conn slimits 5;
-
限制訪問域名:在
nginx.conf
文件中,添加以下配置,只允許特定域名的請求:if ($host !~ ^(guoyu.city|www.guoyu.city|images.guoyu.city)$ ) {return 444; }
-
目錄訪問限制:
- 通過 IP 地址限制訪問:
location /docs/ {deny 192.168.1.1;allow 192.168.1.0/24;deny all; }
- 通過密碼保護目錄:
在mkdir /usr/local/nginx/conf/.htpasswd/ htpasswd -c /usr/local/nginx/conf/.htpasswd/passwd user
nginx.conf
中添加:location ~ /(personal-images/.|delta/.) {auth_basic “Restricted”;auth_basic_user_file /usr/local/nginx/conf/.htpasswd/passwd; }
- 通過 IP 地址限制訪問:
-
防止目錄遍歷:在
nginx.conf
文件中,設置autoindex off
來防止目錄遍歷攻擊:location / {autoindex off; }
-
過濾常見XSS攻擊: 過濾常見的 XSS 攻擊字符, XSS 攻擊常常涉及將
<script>
標簽或類似的特殊字符(如 <>)作為查詢參數的一部分。你可以通過正則表達式來阻止這些非法字符的訪問。server {listen 80;server_name your-domain.com;# 屏蔽包含 <script> 或類似的 XSS 攻擊字符的查詢字符串location / {if ($query_string ~* "<script|%3Cscript|%3E|%3C|alert|eval|javascript|<|>") {return 403; # 返回 HTTP 403 禁止訪問}# 其他配置...}# 其他配置... }
除了
<script>
,你還可以過濾其他可能用于 XSS 攻擊的字符,比如 JavaScript 關鍵字、URL 編碼后的字符(如 %3C 為 <,%3E 為 >),甚至是嘗試使用 eval() 或 alert() 等 JavaScript 函數。server {listen 80;server_name your-domain.com;# 屏蔽含有危險字符或關鍵字的請求location / {if ($query_string ~* "<script|%3Cscript|%3E|%3C|alert|eval|javascript|<|>") {return 403; # 返回 HTTP 403 禁止訪問}# 其他配置...}# 其他配置... }
-
設置HTTP頭: 雖然 X-XSS-Protection 響應頭在現代瀏覽器中已被棄用,但它仍然可以在一些舊版本瀏覽器中提供一定的保護。可在location規則下添加相應的配置信息。
add_header X-XSS-Protection "1; mode=block";
-
設置 Content-Type Options: 阻止瀏覽器從執行由服務器推斷出的 MIME 類型,而非服務器聲明的內容類型。可在location規則下添加相應的配置信息。
add_header X-Content-Type-Options nosniff;
-
限制請求大小 :通過限制請求的大小,可以減少大型惡意載荷的風險。可在location規則下添加相應的配置信息。
# 限制請求大小為10KB client_max_body_size 10K;
-
實施內容安全策略(CSP): CSP 是一個額外的安全層,可以幫助檢測和減輕某些類型的攻擊,包括 XSS 和數據注入攻擊。可在location規則下添加相應的配置信息。
add_header Content-Security-Policy "default-src 'self'; script-src 'self' https://apis.guoyu.city";
二、SSL/TLS 配置
-
生成 SSL 證書:
cd /usr/local/nginx/conf openssl genrsa -des3 -out server.key 1024 openssl req -new -key server.key -out server.csr cp server.key server.key.org openssl rsa -in server.key.org -out server.key openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
-
配置 SSL:在
nginx.conf
文件中,添加以下配置:server {server_name project.guoyu.city;listen 443;ssl on;ssl_certificate /usr/local/nginx/conf/server.crt;ssl_certificate_key /usr/local/nginx/conf/server.key;access_log /usr/local/nginx/logs/ssl.access.log;error_log /usr/local/nginx/logs/ssl.error.log; }
-
配置 HTTPS 重定向:在
nginx.conf
文件中,添加以下配置,將所有 HTTP 請求重定向到 HTTPS:server {listen 80;server_name example.com;return 301 https://$host$request_uri; }
-
配置 SSL 參數:在
nginx.conf
文件中,添加以下配置,提高 SSL 安全性:ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3; ssl_prefer_server_ciphers on;
-
使用 Let’s Encrypt 自動續期證書:
- 安裝 Certbot 和 Nginx 插件:
sudo dnf install certbot python3-certbot-nginx -y
- 使用 Certbot 申請 SSL 證書并自動配置 Nginx:
sudo certbot --nginx -d your_domain.com -d www.your_domain.com
- 配置定時任務,每天凌晨 2 點自動檢查并續期證書:
sudo crontab -e 0 2 * * * /usr/bin/certbot renew --quiet --post-hook "systemctl reload nginx"
- 安裝 Certbot 和 Nginx 插件:
三、其他安全配置
-
安裝 SELinux 策略:安裝和編譯 SELinux 策略以強化 Nginx Web 服務器:
yum -y install selinux-policy-targeted selinux-policy-devel cd /opt wget http://downloads.sourceforge.net/project/selinuxnginx/se-ngix_1_0_10.tar.gz?use_mirror=nchc&rsquo tar -zxvf se-ngix_1_0_10.tar.gz cd se-ngix_1_0_10/nginx make /usr/sbin/semodule -i nginx.pp
-
限制 Nginx 連接傳出:使用 Iptables 限制 Nginx 用戶的傳出連接:
/sbin/iptables -A OUTPUT -o eth0 -m owner -uid-owner nginx -p tcp -dport 80 -m state -state NEW,ESTABLISHED -j ACCEPT
-
配置操作系統保護:正確設置
/nginx
文檔根目錄的權限,確保 Nginx 以非 root 用戶運行:find /nginx -user nginx find /usr/local/nginx/html -user nginx ls -l /usr/local/nginx/html/
-
限制每個 IP 的連接數:在防火墻級別限制每個 IP 的連接數:
/sbin/iptables -A INPUT -p tcp -dport 80 -i eth0 -m state -state NEW -m recent -set /sbin/iptables -A INPUT -p tcp -dport 80 -i eth0 -m state -state NEW -m recent -update -seconds 60 -hitcount 15 -j DROP service iptables save
通過以上的安全配置,可以大大提高 Nginx 服務器的安全性,減少網站的安全威脅。