最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Nginx?Proxy?Manager配置Web?WAF應(yīng)用防火墻

 更新時間:2025年02月12日 09:41:21   作者:碼手Lion  
Nginx?Proxy?Manager是一款功能強大的開源軟件,配置Web應(yīng)用防火墻,可以防止常見的web攻擊,本文就來介紹一下Nginx?Proxy?Manager配置Web?WAF應(yīng)用防火墻,感興趣的可以了解一下

Nginx Proxy Manager (NPM) 是一款功能強大的開源軟件,它提供了一個用戶友好的界面,讓用戶可以輕松地管理 Nginx 反向代理配置。通過 NPM,你可以快速搭建高性能、安全的反向代理服務(wù)器,實現(xiàn)負(fù)載均衡、SSL 證書自動申請、自定義配置,配置 Web 應(yīng)用防火墻,防止常見的 Web 攻擊等功能。

一、安裝

首先,確保已經(jīng)安裝了 Docker 和 Docker Compose,然后通過以下步驟來啟動 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、啟動服務(wù)

你可以通過以下命令啟動服務(wù):

docker-compose up -d

這樣,nginx-proxy-manager 就會在后臺啟動,默認(rèn)通過 admin@example.com 和 changeme 作為登錄憑證。

3、中文鏡像

  • 英文鏡像jc21/nginx-proxy-manager
  • 中文鏡像chishin/nginx-proxy-manager-zh

你可以根據(jù)需求切換鏡像,中文鏡像為中文界面,適合中文用戶。

二、配置

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      # 包含在代理服務(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)的外部站點直接引用你的圖像資源,可以在 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。
  • 代理轉(zhuǎn)發(fā):符合規(guī)則的請求將被轉(zhuǎn)發(fā)到 https://blog.mslion.top 進(jìn)行處理。

3、防止惡意查詢參數(shù)

為了增強安全性,以下是防止惡意查詢參數(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ū)域

為了限制特定國家/地區(qū)的訪問,你可以使用 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ù)庫(數(shù)據(jù)庫得自已下載,并上傳到對應(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 限制訪問(例如僅允許中國 IP):
if ($geoip2_data_country_code != "CN") {
    return 403;
}
  • 也可在 Proxy Host 面板的 Advanced 配置中,添加類似的限制:
if ($geoip2_data_country_code != "CN") {
    return 403;
}

測試輸出

為了確保 GeoIP2 模塊和其他防護措施正常工作,可以通過配置一個測試 URL 來驗證是否按預(yù)期限制了訪問。以下是輸出測試的示例配置:

  • 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ù)庫:
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,你應(yīng)該能夠看到類似如下的輸出(如果你的請求來自中國):
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 安全頭來增強網(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、限制請求速率(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 的基礎(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;

    # 強制 HTTPS 重定向
    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    }

    # 其他配置...
}

四、總結(jié)

通過以上的配置和步驟,你可以有效地配置和優(yōu)化你的 nginx-proxy-manager,增強系統(tǒng)的安全性和性能。關(guān)鍵步驟包括:

  • GeoIP2 限制:通過加載 GeoIP2 數(shù)據(jù)庫來限制訪問來源,僅允許來自特定國家的請求訪問。
  • 惡意查詢防護:使用 Nginx 的 map 和 if 指令,防止惡意的查詢參數(shù)(如 XSS、SQL 注入等)。
  • 防盜鏈:配置圖床防盜鏈,確保只有合法來源的請求能夠訪問你的靜態(tài)資源。
  • SSL/TLS 加密:啟用 HTTPS 加密流量,確保數(shù)據(jù)安全。
  • HTTP 安全頭:配置 HTTP 安全頭,防止常見的 Web 攻擊。
  • 請求速率限制:防止暴 力破 解和 DDoS 攻擊。

通過這些優(yōu)化措施,你能夠提升你的反向代理服務(wù)器的安全性,保護你的網(wǎng)絡(luò)資產(chǎn)免受惡意攻擊,同時提升訪問性能。

到此這篇關(guān)于Nginx Proxy Manager配置Web WAF應(yīng)用防火墻的文章就介紹到這了,更多相關(guān)Nginx Proxy Manager配置防火墻內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • keepalived?+?nginx?實現(xiàn)高可用方案

    keepalived?+?nginx?實現(xiàn)高可用方案

    這篇文章主要介紹了keepalived?+?nginx?實現(xiàn)高可用方案的相關(guān)資料,需要的朋友可以參考下
    2022-12-12
  • Nginx Location配置全過程

    Nginx Location配置全過程

    介紹了Nginx的Location語法結(jié)構(gòu),涵蓋標(biāo)準(zhǔn)/正則uri匹配、匹配標(biāo)識優(yōu)先級(精確>正則>通用),root與alias指令的區(qū)別(保留/替換前綴),以及通過錯誤日志調(diào)試Location配置的方法
    2025-07-07
  • Nginx打包RPM過程(FPM工具)

    Nginx打包RPM過程(FPM工具)

    文章介紹了在Linux系統(tǒng)中安裝Nginx、Ruby2.6及FPM工具的完整流程,包括源碼編譯、依賴安裝、RPM打包配置及錯誤處理方法,重點解決rpmbuild缺失問題
    2025-07-07
  • Nginx在linux和windows下代理靜態(tài)文件夾方式

    Nginx在linux和windows下代理靜態(tài)文件夾方式

    Windows下配置Nginx代理E盤data文件夾,開發(fā)端口7766;Linux下配置Nginx代理home/mydata文件夾,開發(fā)端口7766,重啟Nginx方法:Windows直接nginx -s reload;Linux根據(jù)安裝位置使用find命令找到sbin目錄后,使用/usr/sbin/nginx -s reload重啟
    2025-11-11
  • nginx源碼分析configure腳本詳解

    nginx源碼分析configure腳本詳解

    這篇文章主要介紹了nginx源碼分析configure腳本詳解的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • windows安裝nginx部署步驟圖解(反向代理與負(fù)載均衡)

    windows安裝nginx部署步驟圖解(反向代理與負(fù)載均衡)

    這篇文章主要介紹了windows安裝nginx部署步驟,設(shè)置反向代理與負(fù)載均衡的使用方法,需要的朋友可以參考下
    2014-02-02
  • 詳解nginx請求頭數(shù)據(jù)讀取流程

    詳解nginx請求頭數(shù)據(jù)讀取流程

    這篇文章主要介紹了詳解nginx請求頭數(shù)據(jù)讀取流程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • nginx中使用lua腳本的方法

    nginx中使用lua腳本的方法

    這篇文章主要介紹了nginx中使用lua腳本的方法,本文介紹通過第三方模塊lua-nginx-module實現(xiàn)lua腳本在nginx的調(diào)用,并附一個配置例子,需要的朋友可以參考下
    2014-07-07
  • nginx訪問控制的兩種方法

    nginx訪問控制的兩種方法

    這篇文章主要介紹了關(guān)于nginx訪問控制的兩種方法,一種是基于Basic Auth認(rèn)證,另一種是基于IP的訪問控制,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-03-03
  • Nginx為已安裝nginx動態(tài)添加模塊

    Nginx為已安裝nginx動態(tài)添加模塊

    本篇文章主要介紹了Nginx之為已安裝nginx動態(tài)添加模塊的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04

最新評論

晋州市| 璧山县| 鹤岗市| 武邑县| 寻甸| 兖州市| 新竹市| 铁岭县| 山阴县| 成安县| 疏附县| 乳源| 芷江| 正定县| 谢通门县| 青浦区| 房山区| 金堂县| 古蔺县| 柘城县| 鱼台县| 牙克石市| 米脂县| 南部县| 富阳市| 卓资县| 石门县| 遵义市| 济源市| 报价| 呈贡县| 松阳县| 康保县| 光泽县| 商都县| 福建省| 龙川县| 西乌| 五莲县| 保靖县| 马山县|