Nginx?Proxy?Manager配置Web?WAF應用防火墻
Nginx Proxy Manager (NPM) 是一款功能強大的開源軟件,它提供了一個用戶友好的界面,讓用戶可以輕松地管理 Nginx 反向代理配置。通過 NPM,你可以快速搭建高性能、安全的反向代理服務器,實現負載均衡、SSL 證書自動申請、自定義配置,配置 Web 應用防火墻,防止常見的 Web 攻擊等功能。
一、安裝
首先,確保已經安裝了 Docker 和 Docker Compose,然后通過以下步驟來啟動 nginx-proxy-manager
服務。
1、創(chuàng)建文件
# 創(chuàng)建所需的文件夾 mkdir -p /home/docker/npm # 進入安裝目錄 cd /home/docker/npm # 創(chuàng)建 docker-compose.yml 文件 vim docker-compose.yml
在文件中,添加以下配置:
# 默認登陸名和密碼 # Email: admin@example.com # Password: changeme services: app: image: 'docker.io/jc21/nginx-proxy-manager:latest' restart: unless-stopped ports: - '80:80' - '81:81' - '443:443' volumes: - /home/docker/npm/data:/data - /home/docker/npm/letsencrypt:/etc/letsencrypt
配置完成后,保存并退出編輯器(按下 i
進入編輯模式,按下 Esc
退出編輯模式,輸入 :wq
保存并退出)。
2、啟動服務
你可以通過以下命令啟動服務:
docker-compose up -d
這樣,nginx-proxy-manager
就會在后臺啟動,默認通過 admin@example.com
和 changeme
作為登錄憑證。
3、中文鏡像
- 英文鏡像:
jc21/nginx-proxy-manager
- 中文鏡像:
chishin/nginx-proxy-manager-zh
你可以根據需求切換鏡像,中文鏡像為中文界面,適合中文用戶。
二、配置
Nginx Proxy Manager 允許你通過掛載自定義配置文件來定制 Nginx 配置。以下是一些常見的自定義配置文件路徑和使用方法:
1、配置路徑
在 Nginx Proxy Manager 中,你可以在 /data/nginx/custom
文件夾中添加自定義配置文件,按需引入到主配置文件中:
/data/nginx/custom/root_top.conf # 包含在 nginx.conf 的頂部 /data/nginx/custom/root.conf # 包含在 nginx.conf 的最末尾 /data/nginx/custom/http_top.conf # 包含在 http 塊的頂部 /data/nginx/custom/http.conf # 包含在 http 塊的末尾 /data/nginx/custom/events.conf # 包含在 events 塊的末尾 /data/nginx/custom/stream.conf # 包含在 stream 塊的末尾 /data/nginx/custom/server_proxy.conf # 包含在代理服務器塊的末尾 /data/nginx/custom/server_redirect.conf # 包含在重定向服務器塊的末尾 /data/nginx/custom/server_stream.conf # 包含在流服務器塊的末尾 /data/nginx/custom/server_stream_tcp.conf # 包含在 TCP 流服務器塊的末尾 /data/nginx/custom/server_stream_udp.conf # 包含在 UDP 流服務器塊的末尾
- 配置文件示例
# Nginx 主配置文件 # ========================== # 1. 全局配置:root_top.conf # ========================== # 包含全局的基礎配置(如模塊加載、日志路徑等) include /data/nginx/custom/root_top.conf; # ========================== # 2. 事件配置:events.conf # ========================== events { include /data/nginx/custom/events.conf; } # ========================== # 3. 主 HTTP 配置塊:http_top.conf 和 http.conf # ========================== http { include /data/nginx/custom/http_top.conf; server_tokens off; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; error_log /var/log/nginx/error.log warn; # 服務器配置 server { listen 80; server_name proxy.example.com; include /data/nginx/custom/server_proxy.conf; } server { listen 80; server_name redirect.example.com; include /data/nginx/custom/server_redirect.conf; return 301 https://$host$request_uri; } include /data/nginx/custom/http.conf; } # ========================== # 5. 主流配置:stream.conf # ========================== stream { include /data/nginx/custom/stream.conf; server { listen 3306; proxy_pass backend_tcp_servers; include /data/nginx/custom/server_stream.conf; } server { listen 3307; proxy_pass backend_tcp_servers; include /data/nginx/custom/server_stream_tcp.conf; } server { listen 53 udp; proxy_pass backend_udp_servers; include /data/nginx/custom/server_stream_udp.conf; } } # ========================== # 9. 全局配置末尾:root.conf # ========================== include /data/nginx/custom/root.conf;
2、圖床配置防盜鏈
為了防止未經授權的外部站點直接引用你的圖像資源,可以在 Nginx 配置中啟用防盜鏈。以下是一個簡單的圖床防盜鏈配置示例:
location ~* \.(gif|jpg|png|bmp)$ { valid_referers none blocked mslion.top *.mslion.top ~\.google\. ~\.bing\. ~\.baidu\.; if ($invalid_referer) { return 403; } proxy_pass https://blog.mslion.top; }
- 匹配規(guī)則:此配置會匹配所有
.gif
,.jpg
,.png
和.bmp
文件。 - 合法引用者:指定哪些來源是合法的,非法來源會返回
403 Forbidden
。 - 代理轉發(fā):符合規(guī)則的請求將被轉發(fā)到
https://blog.mslion.top
進行處理。
3、防止惡意查詢參數
為了增強安全性,以下是防止惡意查詢參數的配置示例:
- http_top.conf 文件中添加:
# 定義惡意查詢模式 map $query_string $blocked { default 0; # XSS 防御 "~*(alert\(|<script>|</script>|on\w+=|javascript:|<img\s+src=|<svg\s+οnlοad=)" 1; # SQL 注入防御 "~*(--|or\s1=1|union\sselect|select\s.*from|drop\s+table|insert\s+into|update\s+set|delete\s+from|;--|#|0x|char\(|unhex\()" 1; # 文件包含攻擊防御 "~*(/etc/passwd|/proc/self/environ|php://input|php://filter|file\://|ftp://|http://)" 1; # 命令注入防御 "~*(\|&|\&\||;|`|exec\(|system\(|passthru\(|shell_exec\(|popen\()" 1; # Webshell 特征防御 "~*(base64_encode\(|eval\(|gzinflate\(|gzuncompress\(|assert\(|create_function\(|function_exists\()" 1; # 禁止惡意 User-Agent 或 Referer "~*(bot|spider|crawl|wget|curl|nmap|nikto|sqlmap|libwww|httrack)" 1; # 命令執(zhí)行關鍵字防御 "~*(rm\s-rf|chmod\s777|chown\s|chgrp\s)" 1; }
- server_proxy.conf 文件中添加:
if ($blocked) { return 405; }
4、限制區(qū)域
為了限制特定國家/地區(qū)的訪問,你可以使用 GeoIP2 模塊進行地理位置限制。以下是配置示例:
- root_top.conf 文件中啟用 GeoIP2 模塊:
load_module /usr/lib/nginx/modules/ngx_http_geoip2_module.so; load_module /usr/lib/nginx/modules/ngx_stream_geoip2_module.so;
- http_top.conf 文件中加載 GeoLite2 數據庫(數據庫得自已下載,并上傳到對應的目錄):
geoip2 /data/nginx/custom/GeoLite2-Country.mmdb { $geoip2_data_country_code country iso_code; $geoip2_data_country_name country names en; }
- server_proxy.conf 文件中根據 IP 限制訪問(例如僅允許中國 IP):
if ($geoip2_data_country_code != "CN") { return 403; }
- 也可在 Proxy Host 面板的 Advanced 配置中,添加類似的限制:
if ($geoip2_data_country_code != "CN") { return 403; }
測試輸出
為了確保 GeoIP2 模塊和其他防護措施正常工作,可以通過配置一個測試 URL 來驗證是否按預期限制了訪問。以下是輸出測試的示例配置:
- root_top.conf:啟用 GeoIP2 模塊(已在前面配置):
load_module /usr/lib/nginx/modules/ngx_http_geoip2_module.so; load_module /usr/lib/nginx/modules/ngx_stream_geoip2_module.so;
- http_top.conf:配置日志格式并加載 GeoLite2 數據庫:
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$geoip2_data_country_code" "$geoip2_data_country_name"'; geoip2 /data/nginx/custom/GeoLite2-Country.mmdb { $geoip2_data_country_code country iso_code; $geoip2_data_country_name country names en; }
- server_proxy.conf:添加一個
/show-geoip
路由,用于輸出地理位置信息,僅允許來自中國的請求訪問:
location /show-geoip { # 僅允許中國的 IP 地址訪問 if ($geoip2_data_country_code != "CN") { return 403; # 如果不是中國 IP,返回 403 禁止訪問 } default_type text/plain; echo "Country Code: $geoip2_data_country_code"; echo "Country Name: $geoip2_data_country_name"; }
- 通過訪問
http://your-server-ip/show-geoip
,你應該能夠看到類似如下的輸出(如果你的請求來自中國):
Country Code: CN Country Name: China
如果請求來自其他國家,Nginx 會返回 403 Forbidden
錯誤,確保只有符合地理位置要求的用戶能夠訪問該頁面。
三、其它
除了上面的 GeoIP2 地理位置限制和惡意查詢防護,Nginx 還可以通過其他措施來增強安全性。以下是一些額外的防護和優(yōu)化建議:
1、禁用不必要的 HTTP 方法
server { listen 80; server_name example.com; if ($request_method !~ ^(GET|POST|HEAD|OPTIONS)$) { return 405; } # 其他配置... }
2、啟用 HTTP 安全頭
配置一些 HTTP 安全頭來增強網站的安全性,防止 XSS、Clickjacking 等攻擊:
server { listen 443 ssl; server_name example.com; # 防止 Clickjacking add_header X-Frame-Options "SAMEORIGIN" always; # 防止 XSS 攻擊 add_header X-XSS-Protection "1; mode=block" always; # 防止 MIME 類型嗅探 add_header X-Content-Type-Options "nosniff" always; # 禁止緩存敏感內容 add_header Cache-Control "no-store, no-cache, must-revalidate, max-age=0" always; # 啟用嚴格傳輸安全 (HSTS) add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always; # 其他配置... }
3、限制請求速率(Rate Limiting)
為了防止暴 力破 解和 DDoS 攻擊,可以配置請求速率限制:
http { # 定義一個限制規(guī)則 limit_req_zone $binary_remote_addr zone=one:10m rate=30r/m; server { listen 80; server_name example.com; # 啟用速率限制 limit_req zone=one burst=10 nodelay; # 其他配置... } }
這將限制每個 IP 地址每分鐘最多能發(fā)起 30 次請求,超過此限制的請求將被拒絕。
4、啟用 SSL/TLS 加密
確保你的 Nginx 配置啟用了 SSL/TLS 加密,以確保所有流量是加密的,防止中間人攻擊(MITM)。以下是啟用 SSL 的基礎配置:
server { listen 443 ssl; server_name example.com; ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers 'TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256'; ssl_prefer_server_ciphers on; # 強制 HTTPS 重定向 if ($scheme != "https") { return 301 https://$host$request_uri; } # 其他配置... }
四、總結
通過以上的配置和步驟,你可以有效地配置和優(yōu)化你的 nginx-proxy-manager
,增強系統的安全性和性能。關鍵步驟包括:
- GeoIP2 限制:通過加載 GeoIP2 數據庫來限制訪問來源,僅允許來自特定國家的請求訪問。
- 惡意查詢防護:使用 Nginx 的
map
和if
指令,防止惡意的查詢參數(如 XSS、SQL 注入等)。 - 防盜鏈:配置圖床防盜鏈,確保只有合法來源的請求能夠訪問你的靜態(tài)資源。
- SSL/TLS 加密:啟用 HTTPS 加密流量,確保數據安全。
- HTTP 安全頭:配置 HTTP 安全頭,防止常見的 Web 攻擊。
- 請求速率限制:防止暴 力破 解和 DDoS 攻擊。
通過這些優(yōu)化措施,你能夠提升你的反向代理服務器的安全性,保護你的網絡資產免受惡意攻擊,同時提升訪問性能。
到此這篇關于Nginx Proxy Manager配置Web WAF應用防火墻的文章就介紹到這了,更多相關Nginx Proxy Manager配置防火墻內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
阿里云部署Ubuntu 1.4 Flask + WSGI + Nginx 詳解
本文解決的是 Flask 最后一公里的問題:Linux 部署,需要的朋友可以參考下2017-12-12Nginx如何配置Http、Https、WS、WSS的方法步驟
這篇文章主要介紹了Nginx如何配置Http、Https、WS、WSS的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-05-05在Nginx中配置image filter模塊來實現動態(tài)生成縮略圖
這篇文章主要介紹了在Nginx中配置image filter模塊來實現動態(tài)生成縮略圖的方法,包括縮略圖尺寸的設置等方面的介紹,需要的朋友可以參考下2015-12-12