Nginx?Proxy?Manager配置Web?WAF應(yīng)用防火墻
Nginx Proxy Manager (NPM) 是一款功能強(qiáng)大的開源軟件,它提供了一個(gè)用戶友好的界面,讓用戶可以輕松地管理 Nginx 反向代理配置。通過(guò) NPM,你可以快速搭建高性能、安全的反向代理服務(wù)器,實(shí)現(xiàn)負(fù)載均衡、SSL 證書自動(dòng)申請(qǐng)、自定義配置,配置 Web 應(yīng)用防火墻,防止常見的 Web 攻擊等功能。
一、安裝
首先,確保已經(jīng)安裝了 Docker 和 Docker Compose,然后通過(guò)以下步驟來(lái)啟動(dòng) nginx-proxy-manager
服務(wù)。
1、創(chuàng)建文件
# 創(chuàng)建所需的文件夾 mkdir -p /home/docker/npm # 進(jìn)入安裝目錄 cd /home/docker/npm # 創(chuàng)建 docker-compose.yml 文件 vim docker-compose.yml
在文件中,添加以下配置:
# 默認(rèn)登陸名和密碼 # 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
進(jìn)入編輯模式,按下 Esc
退出編輯模式,輸入 :wq
保存并退出)。
2、啟動(dòng)服務(wù)
你可以通過(guò)以下命令啟動(dòng)服務(wù):
docker-compose up -d
這樣,nginx-proxy-manager
就會(huì)在后臺(tái)啟動(dòng),默認(rèn)通過(guò) admin@example.com
和 changeme
作為登錄憑證。
3、中文鏡像
- 英文鏡像:
jc21/nginx-proxy-manager
- 中文鏡像:
chishin/nginx-proxy-manager-zh
你可以根據(jù)需求切換鏡像,中文鏡像為中文界面,適合中文用戶。
二、配置
Nginx Proxy Manager 允許你通過(guò)掛載自定義配置文件來(lái)定制 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 # 包含在代理服務(wù)器塊的末尾 /data/nginx/custom/server_redirect.conf # 包含在重定向服務(wù)器塊的末尾 /data/nginx/custom/server_stream.conf # 包含在流服務(wù)器塊的末尾 /data/nginx/custom/server_stream_tcp.conf # 包含在 TCP 流服務(wù)器塊的末尾 /data/nginx/custom/server_stream_udp.conf # 包含在 UDP 流服務(wù)器塊的末尾
- 配置文件示例
# Nginx 主配置文件 # ========================== # 1. 全局配置:root_top.conf # ========================== # 包含全局的基礎(chǔ)配置(如模塊加載、日志路徑等) 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; # 服務(wù)器配置 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、圖床配置防盜鏈
為了防止未經(jīng)授權(quán)的外部站點(diǎn)直接引用你的圖像資源,可以在 Nginx 配置中啟用防盜鏈。以下是一個(gè)簡(jiǎn)單的圖床防盜鏈配置示例:
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ī)則:此配置會(huì)匹配所有
.gif
,.jpg
,.png
和.bmp
文件。 - 合法引用者:指定哪些來(lái)源是合法的,非法來(lái)源會(huì)返回
403 Forbidden
。 - 代理轉(zhuǎn)發(fā):符合規(guī)則的請(qǐng)求將被轉(zhuǎn)發(fā)到
https://blog.mslion.top
進(jìn)行處理。
3、防止惡意查詢參數(shù)
為了增強(qiáng)安全性,以下是防止惡意查詢參數(shù)的配置示例:
- 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í)行關(guān)鍵字防御 "~*(rm\s-rf|chmod\s777|chown\s|chgrp\s)" 1; }
- server_proxy.conf 文件中添加:
if ($blocked) { return 405; }
4、限制區(qū)域
為了限制特定國(guó)家/地區(qū)的訪問(wèn),你可以使用 GeoIP2 模塊進(jìn)行地理位置限制。以下是配置示例:
- 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 數(shù)據(jù)庫(kù)(數(shù)據(jù)庫(kù)得自已下載,并上傳到對(duì)應(yīng)的目錄):
geoip2 /data/nginx/custom/GeoLite2-Country.mmdb { $geoip2_data_country_code country iso_code; $geoip2_data_country_name country names en; }
- server_proxy.conf 文件中根據(jù) IP 限制訪問(wèn)(例如僅允許中國(guó) IP):
if ($geoip2_data_country_code != "CN") { return 403; }
- 也可在 Proxy Host 面板的 Advanced 配置中,添加類似的限制:
if ($geoip2_data_country_code != "CN") { return 403; }
測(cè)試輸出
為了確保 GeoIP2 模塊和其他防護(hù)措施正常工作,可以通過(guò)配置一個(gè)測(cè)試 URL 來(lái)驗(yàn)證是否按預(yù)期限制了訪問(wèn)。以下是輸出測(cè)試的示例配置:
- root_top.conf:?jiǎn)⒂?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 數(shù)據(jù)庫(kù):
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:添加一個(gè)
/show-geoip
路由,用于輸出地理位置信息,僅允許來(lái)自中國(guó)的請(qǐng)求訪問(wèn):
location /show-geoip { # 僅允許中國(guó)的 IP 地址訪問(wèn) if ($geoip2_data_country_code != "CN") { return 403; # 如果不是中國(guó) IP,返回 403 禁止訪問(wèn) } default_type text/plain; echo "Country Code: $geoip2_data_country_code"; echo "Country Name: $geoip2_data_country_name"; }
- 通過(guò)訪問(wèn)
http://your-server-ip/show-geoip
,你應(yīng)該能夠看到類似如下的輸出(如果你的請(qǐng)求來(lái)自中國(guó)):
Country Code: CN Country Name: China
如果請(qǐng)求來(lái)自其他國(guó)家,Nginx 會(huì)返回 403 Forbidden
錯(cuò)誤,確保只有符合地理位置要求的用戶能夠訪問(wèn)該頁(yè)面。
三、其它
除了上面的 GeoIP2 地理位置限制和惡意查詢防護(hù),Nginx 還可以通過(guò)其他措施來(lái)增強(qiáng)安全性。以下是一些額外的防護(hù)和優(yōu)化建議:
1、禁用不必要的 HTTP 方法
server { listen 80; server_name example.com; if ($request_method !~ ^(GET|POST|HEAD|OPTIONS)$) { return 405; } # 其他配置... }
2、啟用 HTTP 安全頭
配置一些 HTTP 安全頭來(lái)增強(qiáng)網(wǎng)站的安全性,防止 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; # 禁止緩存敏感內(nèi)容 add_header Cache-Control "no-store, no-cache, must-revalidate, max-age=0" always; # 啟用嚴(yán)格傳輸安全 (HSTS) add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always; # 其他配置... }
3、限制請(qǐng)求速率(Rate Limiting)
為了防止暴 力破 解和 DDoS 攻擊,可以配置請(qǐng)求速率限制:
http { # 定義一個(gè)限制規(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; # 其他配置... } }
這將限制每個(gè) IP 地址每分鐘最多能發(fā)起 30 次請(qǐng)求,超過(guò)此限制的請(qǐng)求將被拒絕。
4、啟用 SSL/TLS 加密
確保你的 Nginx 配置啟用了 SSL/TLS 加密,以確保所有流量是加密的,防止中間人攻擊(MITM)。以下是啟用 SSL 的基礎(chǔ)配置:
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; # 強(qiáng)制 HTTPS 重定向 if ($scheme != "https") { return 301 https://$host$request_uri; } # 其他配置... }
四、總結(jié)
通過(guò)以上的配置和步驟,你可以有效地配置和優(yōu)化你的 nginx-proxy-manager
,增強(qiáng)系統(tǒng)的安全性和性能。關(guān)鍵步驟包括:
- GeoIP2 限制:通過(guò)加載 GeoIP2 數(shù)據(jù)庫(kù)來(lái)限制訪問(wèn)來(lái)源,僅允許來(lái)自特定國(guó)家的請(qǐng)求訪問(wèn)。
- 惡意查詢防護(hù):使用 Nginx 的
map
和if
指令,防止惡意的查詢參數(shù)(如 XSS、SQL 注入等)。 - 防盜鏈:配置圖床防盜鏈,確保只有合法來(lái)源的請(qǐng)求能夠訪問(wèn)你的靜態(tài)資源。
- SSL/TLS 加密:?jiǎn)⒂?HTTPS 加密流量,確保數(shù)據(jù)安全。
- HTTP 安全頭:配置 HTTP 安全頭,防止常見的 Web 攻擊。
- 請(qǐng)求速率限制:防止暴 力破 解和 DDoS 攻擊。
通過(guò)這些優(yōu)化措施,你能夠提升你的反向代理服務(wù)器的安全性,保護(hù)你的網(wǎng)絡(luò)資產(chǎn)免受惡意攻擊,同時(shí)提升訪問(wèn)性能。
到此這篇關(guān)于Nginx Proxy Manager配置Web WAF應(yīng)用防火墻的文章就介紹到這了,更多相關(guān)Nginx Proxy Manager配置防火墻內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
服務(wù)器的負(fù)載均衡nginx+tomcat實(shí)現(xiàn)動(dòng)靜分離
這篇文章主要為大家介紹了服務(wù)器的負(fù)載均衡nginx+tomcat實(shí)現(xiàn)動(dòng)靜分離的案例實(shí)施步驟以及環(huán)境詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03關(guān)于nginx沒有跳轉(zhuǎn)到upstream地址的解決
這篇文章主要介紹了關(guān)于nginx沒有跳轉(zhuǎn)到upstream地址的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09阿里云部署Ubuntu 1.4 Flask + WSGI + Nginx 詳解
本文解決的是 Flask 最后一公里的問(wèn)題:Linux 部署,需要的朋友可以參考下2017-12-12HAProxy和Nginx搭建負(fù)載均衡器的實(shí)現(xiàn)
負(fù)載均衡器是一個(gè)常用于分布式計(jì)算和網(wǎng)絡(luò)應(yīng)用中的系統(tǒng)組件,主要用于將客戶端的請(qǐng)求分發(fā)到多個(gè)后端服務(wù)器上,以實(shí)現(xiàn)高可用性、高性能和可擴(kuò)展性,本文主要介紹了HAProxy和Nginx搭建負(fù)載均衡器的實(shí)現(xiàn),感興趣的可以了解一下,感興趣的可以了解一下2023-11-11Nginx如何配置Http、Https、WS、WSS的方法步驟
這篇文章主要介紹了Nginx如何配置Http、Https、WS、WSS的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05在Nginx中配置image filter模塊來(lái)實(shí)現(xiàn)動(dòng)態(tài)生成縮略圖
這篇文章主要介紹了在Nginx中配置image filter模塊來(lái)實(shí)現(xiàn)動(dòng)態(tài)生成縮略圖的方法,包括縮略圖尺寸的設(shè)置等方面的介紹,需要的朋友可以參考下2015-12-12