AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

    • 主页
    • 系统&网络
    • Ubuntu
    • Unix
    • DBA
    • Computer
    • Coding
    • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题

问题[nginx](server)

Martin Hope
Amr Abu Aza
Asked: 2025-04-28 22:29:01 +0800 CST

NGINX 将所有请求路由到后端服务,而不是分离后端和 API(PHP YII2)

  • 6

我在 Docker 中的同一个端口上运行了两个服务(后端和 API)。但是,每当我发送请求时,NGINX 都会将所有请求路由到后端服务,而我无法正常访问 API 服务。

我怀疑这是我的 NGINX 配置的问题。

这是我的文件:

docker-compose.yml

version: '3.8'

services:
  backend:
    build:
      context: .
      dockerfile: ./backend/Dockerfile.dev
    volumes:
      - ./backend:/var/www/html/backend
    environment:
      - APP_ENV=development
    networks:
      - bysooq-network
    expose:
      - 9000
    env_file:
      - .env

  api:
    build:
      context: .
      dockerfile: ./api/Dockerfile.dev
    volumes:
      - ./api:/var/www/html/api
    environment:
      - APP_ENV=development
    networks:
      - bysooq-network
    expose:
      - 9000
    env_file:
      - .env

  postgres:
    image: postgres:13
    restart: always
    volumes:
      - ~/bysooq-data/postgres:/var/lib/postgresql/data
    environment:
      POSTGRES_DB: xxx
      POSTGRES_USER: xx
      POSTGRES_PASSWORD: xxx
    networks:
      - bysooq-network

  redis:
    image: redis:latest
    ports:
      - "6380:6379"
    restart: always
    networks:
      - bysooq-network

  nginx:
    image: nginx:latest
    volumes:
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
      - ./api:/var/www/html/api
      - ./backend:/var/www/html/backend
    ports:
      - 80:80
    depends_on:
      - backend
      - api
    networks:
      - bysooq-network

networks:
  bysooq-network:
    driver: bridge

nginx 默认配置文件

nginx

server {
    listen 80;
    server_name localhost;
    client_max_body_size 100M;
    index index.php;

    # API Service - Must come first with strict matching
    location ~ ^/api(/.*)?$ {
        root /var/www/html/api/web;
        try_files $1 $1/ /index.php$is_args$args;

        location ~ \.php$ {
            fastcgi_pass api:9000;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root/index.php;
            fastcgi_param REQUEST_URI $1$is_args$args;
        }
    }

    # Backend Service
    location / {
        root /var/www/html/backend/web;
        try_files $uri $uri/ /index.php$is_args$args;

        location ~ \.php$ {
            fastcgi_pass backend:9000;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
    }
}

我的期望是:

对 /api/* 的请求应该转到 api 服务。

其他请求应该转到后端服务。

会发生什么:

所有请求(甚至 /api/...)都由后端处理。

问题:如何正确配置 NGINX 以将 /api/* 请求路由到 API 服务并将其他请求路由到后端服务?

提前致谢!

nginx
  • 1 个回答
  • 51 Views
Martin Hope
Jerome WAGNER
Asked: 2025-03-26 00:15:22 +0800 CST

处理机器人扫描数千个 URL 以查找安全配置错误

  • 5

我想减少机器人发送 GET URL 来测试网站“经典”安全漏洞所使用的流量和带宽。

类似 GET /.env GET /.git/config GET /phpinfo.php .. 的 URL

当我查看我的日志时,我发现有成千上万个这样的机器人。有些机器人会定期更改其 IP,有些机器人会不断更改其 User-Agent 字符串,……

因此,我正在考虑从我的日志中提取一个硬编码列表并向他们发送 403 Forbidden 或类似的内容,但我想让它足够快,以便 nginx 或 varnish 能够足够快地处理这个过滤器,以免影响它们的性能。

您是否尝试过使用这样的列表来处理这种持续的请求流,并且是否存在像 Nginx 或 Varnish 中的二叉树这样的特殊技巧,可以帮助配置大量路径列表以进行快速匹配?

nginx
  • 2 个回答
  • 74 Views
Martin Hope
natli
Asked: 2025-03-25 02:19:20 +0800 CST

即使正则表达式匹配,fail2ban 过滤器也不会触发

  • 5

因此,我正在运行一个带有 nginx 容器的 docker 服务器,该容器反向代理一堆其他容器。在主机上,我想使用 fail2ban 禁止试图闯入 wordpress 网站的 IP。非常简单,但我需要使用自定义操作,以便禁令在 DOCKER-USER 链中发生,否则 docker 转发(也使用 iptables)将覆盖禁令。为了实现这一点,我将 nginx 日志目录从主机安装到容器,以便 fail2ban 可以从主机监视它们。

一切似乎都运行良好,除了它没有禁止任何 IP。正则表达式在测试中匹配正确,手动禁止工作正常,fail2ban 日志没有显示任何异常;我被难住了。

以下是来自访问日志的模糊/匿名摘录;

132.321.555.777 - - [24/Mar/2025:17:12:07 +0000] "POST //xmlrpc.php HTTP/1.1" 200 423 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36"
132.321.555.777 - - [24/Mar/2025:17:12:07 +0000] "POST //xmlrpc.php HTTP/1.1" 200 423 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36"
132.321.555.777 - - [24/Mar/2025:17:12:07 +0000] "POST //xmlrpc.php HTTP/1.1" 200 423 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36"
132.321.555.777 - - [24/Mar/2025:17:12:08 +0000] "POST //xmlrpc.php HTTP/1.1" 200 423 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36"
132.321.555.777 - - [24/Mar/2025:17:12:08 +0000] "POST //xmlrpc.php HTTP/1.1" 200 423 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36"
84.29.333.555 - - [24/Mar/2025:17:12:08 +0000] "GET /wp-content/uploads/2024/06/xx-32x32.png HTTP/1.1" 200 926 "-" "NetworkingExtension/8620.2.4.10.8 Network/4277.82.1 iOS/18.3.2"
84.29.333.555 - - [24/Mar/2025:17:12:08 +0000] "GET /wp-content/uploads/2024/06/xx-180x180.png HTTP/1.1" 200 5567 "-" "NetworkingExtension/8620.2.4.10.8 Network/4277.82.1 iOS/18.3.2"
132.321.555.777 - - [24/Mar/2025:17:12:09 +0000] "POST //xmlrpc.php HTTP/1.1" 200 423 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36"
132.321.555.777 - - [24/Mar/2025:17:12:09 +0000] "POST //xmlrpc.php HTTP/1.1" 200 423 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36"
132.321.555.777 - - [24/Mar/2025:17:12:09 +0000] "POST //xmlrpc.php HTTP/1.1" 200 423 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36"
132.321.555.777 - - [24/Mar/2025:17:12:10 +0000] "POST //xmlrpc.php HTTP/1.1" 200 423 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36"
43.166.222.22 - - [24/Mar/2025:17:12:02 +0000] "GET / HTTP/1.1" 400 248 "-" "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1"
84.29.123.32 - - [24/Mar/2025:17:12:08 +0000] "GET /wp-content/uploads/2023/01/xx-300x225.png HTTP/1.1" 200 115438 "-" "NetworkingExtension/8620.2.4.10.8 Network/4277.82.1 iOS/18.3.2"
84.29.123.32 - - [24/Mar/2025:17:12:08 +0000] "GET /wp-content/uploads/2024/06/xx-192x192.png HTTP/1.1" 200 5994 "-" "NetworkingExtension/8620.2.4.10.8 Network/4277.82.1 iOS/18.3.2"
84.29.123.32 - - [24/Mar/2025:17:12:09 +0000] "GET /wp-content/uploads/2024/06/xx-32x32.png HTTP/1.1" 200 926 "-" "NetworkingExtension/8620.2.4.10.8 Network/4277.82.1 iOS/18.3.2"

/etc/fail2ban/jail.local

[nginx-xmlrpc]
enabled  = true
filter   = nginx-xmlrpc
logpath  = /var/dockervolumes/nginx/logs/access.log
maxretry = 1
findtime = 5
bantime  = 3600
action   = iptables-docker

我把 maxretry 和 findtime 设置得非常低以进行测试,但这没有帮助。

/etc/fail2ban/filter.d/nginx-xmlrpc.conf

[INCLUDES]

before = nginx-bad-request.conf

[Definition]
failregex = ^<HOST>.*] "POST (|.*)\/xmlrpc\.php.*
            ^<HOST>.*] "GET (|.*)\/xmlrpc\.php.*
            ^<HOST> .* "POST .*wp-login.php
ignoreregex = ^<HOST>.*] "POST (|.*)\/xmlrpc\.php\?for=jetpack.*

datepattern = ^[^\[]*\[({DATE})
              {^LN-BEG}

正如您所看到的,我已经尝试过弄乱日期模式,但没有帮助。

/etc/fail2ban/action.d/iptables-docker.conf

[Definition]
actionstart =
actionstop =
actioncheck = /usr/sbin/iptables -n -L DOCKER-USER | grep -q '[<ip>]'
actionban = /usr/sbin/iptables -I DOCKER-USER -s <ip> -j DROP
actionunban = /usr/sbin/iptables -D DOCKER-USER -s <ip> -j DROP

测试正则表达式;fail2ban-regex /var/dockervolumes/nginx/logs/access.log /etc/fail2ban/filter.d/nginx-xmlrpc.conf

Running tests
=============

Use   failregex filter file : nginx-xmlrpc, basedir: /etc/fail2ban
Use      datepattern : ^[^\[]*\[({DATE})
{^LN-BEG} : Default Detectors
Use         log file : /var/dockervolumes/nginx/logs/access.log
Use         encoding : UTF-8


Results
=======

Failregex: 13345 total
|-  #) [# of hits] regular expression
|   1) [13299] ^<HOST>.*] "POST (|.*)\/xmlrpc\.php.*
|   2) [20] ^<HOST>.*] "GET (|.*)\/xmlrpc\.php.*
|   3) [26] ^<HOST> .* "POST .*wp-login.php
`-

Ignoreregex: 0 total

Date template hits:
|- [# of hits] date format
|  [26486] ^[^\[]*\[(Day(?P<_sep>[-/])MON(?P=_sep)ExYear[ :]?24hour:Minute:Second(?:\.Microseconds)?(?: Zone offset)?)
`-

Lines: 26486 lines, 0 ignored, 13345 matched, 13141 missed
[processed in 2.39 sec]

手动测试禁令;fail2ban-client 设置 nginx-xmlrpc banip 1.2.5.9

产生日志:

2025-03-24 19:09:35,909 fail2ban.actions        [828254]: NOTICE  [nginx-xmlrpc] Ban 1.2.5.9

并正确列出被阻止的 IP;iptables -L DOCKER-USER

Chain DOCKER-USER (1 references)
target     prot opt source               destination
DROP       all  --  1.2.5.9              anywhere
DROP       all  --  1.2.4.9              anywhere
DROP       all  --  1.2.3.9              anywhere
DROP       all  --  1.2.3.8              anywhere
DROP       all  --  1.2.3.7              anywhere
DROP       all  --  1.2.3.6              anywhere
DROP       all  --  1.2.3.5              anywhere
RETURN     all  --  anywhere             anywhere

日志也没有显示任何异常;tail -f /var/log/fail2ban.log

2025-03-24 19:00:29,067 fail2ban.actions        [828254]: NOTICE  [nginx-xmlrpc] Restore Ban 1.2.3.5
2025-03-24 19:00:29,080 fail2ban.actions        [828254]: NOTICE  [nginx-xmlrpc] Restore Ban 1.2.3.6
2025-03-24 19:00:29,094 fail2ban.actions        [828254]: NOTICE  [nginx-xmlrpc] Restore Ban 1.2.3.7
2025-03-24 19:00:29,104 fail2ban.actions        [828254]: NOTICE  [nginx-xmlrpc] Restore Ban 1.2.3.8
2025-03-24 19:00:29,115 fail2ban.actions        [828254]: NOTICE  [nginx-xmlrpc] Restore Ban 1.2.3.9
2025-03-24 19:00:29,126 fail2ban.actions        [828254]: NOTICE  [nginx-xmlrpc] Restore Ban 1.2.4.9
2025-03-24 19:00:29,240 fail2ban.filtersystemd  [828254]: INFO    [nginx-xmlrpc] Jail is in operation now (process new journal entries)
2025-03-24 19:05:53,278 fail2ban.actions        [828254]: NOTICE  [nginx-xmlrpc] Unban 1.2.3.4
2025-03-24 19:09:33,024 fail2ban.actions        [828254]: WARNING [nginx-xmlrpc] 1.2.4.9 already banned
2025-03-24 19:09:35,909 fail2ban.actions        [828254]: NOTICE  [nginx-xmlrpc] Ban 1.2.5.9

更新:

~ fail2ban-客户端状态 nginx-xmlrpc

Status for the jail: nginx-xmlrpc
|- Filter
|  |- Currently failed: 0
|  |- Total failed:     0
|  `- Journal matches:  _SYSTEMD_UNIT=nginx.service + _COMM=nginx
`- Actions
   |- Currently banned: 0
   |- Total banned:     8
   `- Banned IP list:

这 8 次禁令都是我手动测试的。

~ iptables-保存

*filter
:INPUT ACCEPT [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [0:0]
:DOCKER - [0:0]
:DOCKER-ISOLATION-STAGE-1 - [0:0]
:DOCKER-ISOLATION-STAGE-2 - [0:0]
:DOCKER-USER - [0:0]
-A FORWARD -j DOCKER-USER
-A FORWARD -j DOCKER-ISOLATION-STAGE-1
-A FORWARD -o docker0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -o docker0 -j DOCKER
-A FORWARD -i docker0 ! -o docker0 -j ACCEPT
-A FORWARD -i docker0 -o docker0 -j ACCEPT
-A FORWARD -o br-51ec37449070 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -o br-51ec37449070 -j DOCKER
-A FORWARD -i br-51ec37449070 ! -o br-51ec37449070 -j ACCEPT
-A FORWARD -i br-51ec37449070 -o br-51ec37449070 -j ACCEPT
-A DOCKER -d 172.18.0.39/32 ! -i br-51ec37449070 -o br-51ec37449070 -p tcp -m tcp --dport 2020 -j ACCEPT
-A DOCKER -d 172.18.0.39/32 ! -i br-51ec37449070 -o br-51ec37449070 -p tcp -m tcp --dport 2019 -j ACCEPT
-A DOCKER -d 172.18.0.39/32 ! -i br-51ec37449070 -o br-51ec37449070 -p tcp -m tcp --dport 2018 -j ACCEPT
-A DOCKER -d 172.18.0.39/32 ! -i br-51ec37449070 -o br-51ec37449070 -p tcp -m tcp --dport 2017 -j ACCEPT
-A DOCKER -d 172.18.0.39/32 ! -i br-51ec37449070 -o br-51ec37449070 -p tcp -m tcp --dport 2016 -j ACCEPT
-A DOCKER -d 172.18.0.39/32 ! -i br-51ec37449070 -o br-51ec37449070 -p tcp -m tcp --dport 2015 -j ACCEPT
-A DOCKER -d 172.18.0.39/32 ! -i br-51ec37449070 -o br-51ec37449070 -p tcp -m tcp --dport 2014 -j ACCEPT
-A DOCKER -d 172.18.0.39/32 ! -i br-51ec37449070 -o br-51ec37449070 -p tcp -m tcp --dport 2013 -j ACCEPT
-A DOCKER -d 172.18.0.39/32 ! -i br-51ec37449070 -o br-51ec37449070 -p tcp -m tcp --dport 2012 -j ACCEPT
-A DOCKER -d 172.18.0.39/32 ! -i br-51ec37449070 -o br-51ec37449070 -p tcp -m tcp --dport 2011 -j ACCEPT
-A DOCKER -d 172.18.0.39/32 ! -i br-51ec37449070 -o br-51ec37449070 -p tcp -m tcp --dport 2010 -j ACCEPT
-A DOCKER -d 172.18.0.39/32 ! -i br-51ec37449070 -o br-51ec37449070 -p tcp -m tcp --dport 2009 -j ACCEPT
-A DOCKER -d 172.18.0.39/32 ! -i br-51ec37449070 -o br-51ec37449070 -p tcp -m tcp --dport 2008 -j ACCEPT
-A DOCKER -d 172.18.0.39/32 ! -i br-51ec37449070 -o br-51ec37449070 -p tcp -m tcp --dport 2007 -j ACCEPT
-A DOCKER -d 172.18.0.39/32 ! -i br-51ec37449070 -o br-51ec37449070 -p tcp -m tcp --dport 2006 -j ACCEPT
-A DOCKER -d 172.18.0.39/32 ! -i br-51ec37449070 -o br-51ec37449070 -p tcp -m tcp --dport 2005 -j ACCEPT
-A DOCKER -d 172.18.0.39/32 ! -i br-51ec37449070 -o br-51ec37449070 -p tcp -m tcp --dport 2004 -j ACCEPT
-A DOCKER -d 172.18.0.39/32 ! -i br-51ec37449070 -o br-51ec37449070 -p tcp -m tcp --dport 2003 -j ACCEPT
-A DOCKER -d 172.18.0.39/32 ! -i br-51ec37449070 -o br-51ec37449070 -p tcp -m tcp --dport 2002 -j ACCEPT
-A DOCKER -d 172.18.0.39/32 ! -i br-51ec37449070 -o br-51ec37449070 -p tcp -m tcp --dport 2001 -j ACCEPT
-A DOCKER -d 172.18.0.39/32 ! -i br-51ec37449070 -o br-51ec37449070 -p tcp -m tcp --dport 2000 -j ACCEPT
-A DOCKER -d 172.18.0.6/32 ! -i br-51ec37449070 -o br-51ec37449070 -p tcp -m tcp --dport 443 -j ACCEPT
-A DOCKER -d 172.18.0.6/32 ! -i br-51ec37449070 -o br-51ec37449070 -p tcp -m tcp --dport 80 -j ACCEPT
-A DOCKER-ISOLATION-STAGE-1 -i docker0 ! -o docker0 -j DOCKER-ISOLATION-STAGE-2
-A DOCKER-ISOLATION-STAGE-1 -i br-51ec37449070 ! -o br-51ec37449070 -j DOCKER-ISOLATION-STAGE-2
-A DOCKER-ISOLATION-STAGE-1 -j RETURN
-A DOCKER-ISOLATION-STAGE-2 -o docker0 -j DROP
-A DOCKER-ISOLATION-STAGE-2 -o br-51ec37449070 -j DROP
-A DOCKER-ISOLATION-STAGE-2 -j RETURN
-A DOCKER-USER -s 1.14.96.192/32 -j DROP
-A DOCKER-USER -s 18.219.14.219/32 -j DROP
-A DOCKER-USER -s 174.138.27.210/32 -j DROP
-A DOCKER-USER -s 128.199.244.121/32 -j DROP
-A DOCKER-USER -s 45.130.145.4/32 -j DROP
-A DOCKER-USER -s 128.199.244.121/32 -j DROP
-A DOCKER-USER -j RETURN
COMMIT
# Completed on Wed Mar 26 09:10:19 2025
# Generated by iptables-save v1.8.10 (nf_tables) on Wed Mar 26 09:10:19 2025
*nat
:PREROUTING ACCEPT [320032242:19224216945]
:INPUT ACCEPT [0:0]
:OUTPUT ACCEPT [396346224:30027564903]
:POSTROUTING ACCEPT [531692826:38140531338]
:DOCKER - [0:0]
-A PREROUTING -m addrtype --dst-type LOCAL -j DOCKER
-A OUTPUT ! -d 127.0.0.0/8 -m addrtype --dst-type LOCAL -j DOCKER
-A POSTROUTING -s 172.17.0.0/16 ! -o docker0 -j MASQUERADE
-A POSTROUTING -s 172.18.0.0/16 ! -o br-51ec37449070 -j MASQUERADE
-A POSTROUTING -s 172.18.0.39/32 -d 172.18.0.39/32 -p tcp -m tcp --dport 2020 -j MASQUERADE
-A POSTROUTING -s 172.18.0.39/32 -d 172.18.0.39/32 -p tcp -m tcp --dport 2019 -j MASQUERADE
-A POSTROUTING -s 172.18.0.39/32 -d 172.18.0.39/32 -p tcp -m tcp --dport 2018 -j MASQUERADE
-A POSTROUTING -s 172.18.0.39/32 -d 172.18.0.39/32 -p tcp -m tcp --dport 2017 -j MASQUERADE
-A POSTROUTING -s 172.18.0.39/32 -d 172.18.0.39/32 -p tcp -m tcp --dport 2016 -j MASQUERADE
-A POSTROUTING -s 172.18.0.39/32 -d 172.18.0.39/32 -p tcp -m tcp --dport 2015 -j MASQUERADE
-A POSTROUTING -s 172.18.0.39/32 -d 172.18.0.39/32 -p tcp -m tcp --dport 2014 -j MASQUERADE
-A POSTROUTING -s 172.18.0.39/32 -d 172.18.0.39/32 -p tcp -m tcp --dport 2013 -j MASQUERADE
-A POSTROUTING -s 172.18.0.39/32 -d 172.18.0.39/32 -p tcp -m tcp --dport 2012 -j MASQUERADE
-A POSTROUTING -s 172.18.0.39/32 -d 172.18.0.39/32 -p tcp -m tcp --dport 2011 -j MASQUERADE
-A POSTROUTING -s 172.18.0.39/32 -d 172.18.0.39/32 -p tcp -m tcp --dport 2010 -j MASQUERADE
-A POSTROUTING -s 172.18.0.39/32 -d 172.18.0.39/32 -p tcp -m tcp --dport 2009 -j MASQUERADE
-A POSTROUTING -s 172.18.0.39/32 -d 172.18.0.39/32 -p tcp -m tcp --dport 2008 -j MASQUERADE
-A POSTROUTING -s 172.18.0.39/32 -d 172.18.0.39/32 -p tcp -m tcp --dport 2007 -j MASQUERADE
-A POSTROUTING -s 172.18.0.39/32 -d 172.18.0.39/32 -p tcp -m tcp --dport 2006 -j MASQUERADE
-A POSTROUTING -s 172.18.0.39/32 -d 172.18.0.39/32 -p tcp -m tcp --dport 2005 -j MASQUERADE
-A POSTROUTING -s 172.18.0.39/32 -d 172.18.0.39/32 -p tcp -m tcp --dport 2004 -j MASQUERADE
-A POSTROUTING -s 172.18.0.39/32 -d 172.18.0.39/32 -p tcp -m tcp --dport 2003 -j MASQUERADE
-A POSTROUTING -s 172.18.0.39/32 -d 172.18.0.39/32 -p tcp -m tcp --dport 2002 -j MASQUERADE
-A POSTROUTING -s 172.18.0.39/32 -d 172.18.0.39/32 -p tcp -m tcp --dport 2001 -j MASQUERADE
-A POSTROUTING -s 172.18.0.39/32 -d 172.18.0.39/32 -p tcp -m tcp --dport 2000 -j MASQUERADE
-A POSTROUTING -s 172.18.0.6/32 -d 172.18.0.6/32 -p tcp -m tcp --dport 443 -j MASQUERADE
-A POSTROUTING -s 172.18.0.6/32 -d 172.18.0.6/32 -p tcp -m tcp --dport 80 -j MASQUERADE
-A DOCKER -i docker0 -j RETURN
-A DOCKER -i br-51ec37449070 -j RETURN
-A DOCKER ! -i br-51ec37449070 -p tcp -m tcp --dport 2020 -j DNAT --to-destination 172.18.0.39:2020
-A DOCKER ! -i br-51ec37449070 -p tcp -m tcp --dport 2019 -j DNAT --to-destination 172.18.0.39:2019
-A DOCKER ! -i br-51ec37449070 -p tcp -m tcp --dport 2018 -j DNAT --to-destination 172.18.0.39:2018
-A DOCKER ! -i br-51ec37449070 -p tcp -m tcp --dport 2017 -j DNAT --to-destination 172.18.0.39:2017
-A DOCKER ! -i br-51ec37449070 -p tcp -m tcp --dport 2016 -j DNAT --to-destination 172.18.0.39:2016
-A DOCKER ! -i br-51ec37449070 -p tcp -m tcp --dport 2015 -j DNAT --to-destination 172.18.0.39:2015
-A DOCKER ! -i br-51ec37449070 -p tcp -m tcp --dport 2014 -j DNAT --to-destination 172.18.0.39:2014
-A DOCKER ! -i br-51ec37449070 -p tcp -m tcp --dport 2013 -j DNAT --to-destination 172.18.0.39:2013
-A DOCKER ! -i br-51ec37449070 -p tcp -m tcp --dport 2012 -j DNAT --to-destination 172.18.0.39:2012
-A DOCKER ! -i br-51ec37449070 -p tcp -m tcp --dport 2011 -j DNAT --to-destination 172.18.0.39:2011
-A DOCKER ! -i br-51ec37449070 -p tcp -m tcp --dport 2010 -j DNAT --to-destination 172.18.0.39:2010
-A DOCKER ! -i br-51ec37449070 -p tcp -m tcp --dport 2009 -j DNAT --to-destination 172.18.0.39:2009
-A DOCKER ! -i br-51ec37449070 -p tcp -m tcp --dport 2008 -j DNAT --to-destination 172.18.0.39:2008
-A DOCKER ! -i br-51ec37449070 -p tcp -m tcp --dport 2007 -j DNAT --to-destination 172.18.0.39:2007
-A DOCKER ! -i br-51ec37449070 -p tcp -m tcp --dport 2006 -j DNAT --to-destination 172.18.0.39:2006
-A DOCKER ! -i br-51ec37449070 -p tcp -m tcp --dport 2005 -j DNAT --to-destination 172.18.0.39:2005
-A DOCKER ! -i br-51ec37449070 -p tcp -m tcp --dport 2004 -j DNAT --to-destination 172.18.0.39:2004
-A DOCKER ! -i br-51ec37449070 -p tcp -m tcp --dport 2003 -j DNAT --to-destination 172.18.0.39:2003
-A DOCKER ! -i br-51ec37449070 -p tcp -m tcp --dport 2002 -j DNAT --to-destination 172.18.0.39:2002
-A DOCKER ! -i br-51ec37449070 -p tcp -m tcp --dport 2001 -j DNAT --to-destination 172.18.0.39:2001
-A DOCKER ! -i br-51ec37449070 -p tcp -m tcp --dport 2000 -j DNAT --to-destination 172.18.0.39:2000
-A DOCKER ! -i br-51ec37449070 -p tcp -m tcp --dport 443 -j DNAT --to-destination 172.18.0.6:443
-A DOCKER ! -i br-51ec37449070 -p tcp -m tcp --dport 80 -j DNAT --to-destination 172.18.0.6:80
COMMIT
# Completed on Wed Mar 26 09:10:19 2025
nginx
  • 1 个回答
  • 80 Views
Martin Hope
Rekiel
Asked: 2025-03-22 23:10:50 +0800 CST

Nginx 代理管理器禁止 403

  • 5

当我尝试访问 Vaultwarden 的 /admin 页面时遇到 403 Forbidden 错误,该页面受通过 Nginx 代理管理器进行的 HTTP 基本身份验证保护。

问题摘要:

环境:Raspberry Pi 运行 Docker、Vaultwarden、Portainer 和 Nginx 代理管理器。问题:输入凭据后访问 /admin 页面会导致 403 Forbidden 错误。

详细信息和配置:

Nginx Configuration:

location /admin {
    auth_basic "Restricted Access";
    auth_basic_user_file /data/compose/1/data/.htpasswd;

    proxy_pass http://localhost:8080;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}

我尝试过的:

已验证 .htpasswd 文件已正确配置且可供 Nginx 访问。已检查文件权限以确保其设置正确。已查看 Nginx 错误日志,但它们未提供有关 403 错误原因的具体信息。已确认 Vaultwarden 在没有密码保护的情况下也能正常工作。

任何见解或建议都将不胜感激。如果需要更多信息,请告诉我!

谢谢你!

屏幕: 代理主机基本配置 SSL 配置 高级和自定义 Nginx 配置 403 禁止错误

nginx
  • 1 个回答
  • 57 Views
Martin Hope
Quinnell
Asked: 2025-03-22 03:20:45 +0800 CST

使用 Nginx 重定向特定子域路径而不覆盖 Synology 管理的路径

  • 5

我有一台使用 Nginx 反向代理进行 WAN 访问的 Synology NAS。由于子域cloud.domain.com路由到 Synology,因此所有子文件夹路径均由 Synology 应用程序管理。

Synology 界面的屏幕截图,其中/files分配了

要访问文件管理器,我将使用以下 URL:

https://cloud.example.org/files

我在文件管理器中有一个共享文件夹,它使用 tinyURL 样式的链接进行直接访问。我想构建一个重定向,使用自定义 URL 直接指向此共享文件夹,如下所示:

https://cloud.example.org/public_downloads

换句话说,我需要使用https://cloud.example.org/public_downloads来访问https://tiny.url/abcd123。有没有办法覆盖特定的子文件夹路径而不破坏 Synology 管理的路径,例如/files?

我查看了类似的问题nginx 重定向特定子域和路径,但在查看链接的文档后,我不知道如何为路径而不是子域配置它/。我怀疑关键在于操纵location我服务器的 Nginx 配置部分cloud.example.org。

我的 Synology Nginx 配置

## CLOUD HTTPS ##
server {
        listen [::]:443 ssl http2;
        listen 443 ssl http2;
        server_name cloud.domain.com;

                ssl_session_timeout 30m;
                ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
                ssl_certificate      /usr/share/nginx/ssl/example.org-chain.pem;
                ssl_certificate_key  /usr/share/nginx/ssl/example.org-key.pem;
                ssl_session_cache shared:SSL:10m;

                        add_header X-Xss-Protection "1; mode=block" always;
                        add_header X-Content-Type-Options "nosniff" always;
                        add_header Strict-Transport-Security "max-age=2592000; includeSubdomains" always;
                        add_header X-Frame-Options "SAMEORIGIN" always;
                        proxy_hide_header X-Powered-By;
                        add_header 'Referrer-Policy' 'no-referrer';
                        add_header Content-Security-Policy "frame-ancestors example.org app.example.org;";

                location / {
                proxy_pass http://10.1.2.3:5000;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-Host $server_name;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Ssl on;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_read_timeout  90;
                proxy_redirect http://synologyhostname:5000 https://$host/:5001;
        }
}
## END CLOUD HTTPS ##
nginx
  • 1 个回答
  • 29 Views
Martin Hope
Kevin Schulz
Asked: 2025-03-21 01:37:09 +0800 CST

如何通过服务器的 IP 地址将对我的 VPS 上的 Web 服务的访问限制为仅限具有 SSL 的域?

  • 5

我已经在服务器上设置了 Nginx 代理管理器,并使用 SSL(Let's Encrypt)为我的域配置了反向代理。一切正常,我可以通过 HTTPS 安全地通过我的域访问我的 Web 服务,但我遇到了一个问题:任何人都可以通过 http://IP:PORT 访问我的服务,我想完全阻止这种情况。

我想限制对我的服务器的访问,以便只能通过 https://DOMAIN 访问,而不能通过服务器的公共 IP 地址访问。

我已经尝试过以下事情:

  • 将 HTTP 重定向到 HTTPS 并限制仅对域进行访问。-> 无法将 IP 地址写入 NPM 中的域字段
  • 使用自定义 Nginx 代码完全阻止直接 IP 访问。-> 然后代理主机将获得“离线”状态
  • 启用 HSTS -> 仍然允许通过 IP 访问

有没有人遇到过类似的问题,或者可以建议在 Nginx 代理管理器中配置此问题的最佳方法?我非常感谢任何帮助或建议。

提前致谢!

nginx
  • 1 个回答
  • 189 Views
Martin Hope
ShadowEagle
Asked: 2025-03-14 22:16:25 +0800 CST

Nginx - 仅拦截来自后端的 404 并显示自定义错误页面

  • 5

我对想要实现的特定设置遇到了一些问题:

概括:

  • Nginx 充当反向代理
  • 后端运行于 http://localhost:8080/
  • Nginx 可能会返回 HTTP 403($block_access = true),在这种情况下,我想显示自定义错误页面(/errors/403.html)
  • 后端可能会返回 HTTP 404,在这种情况下,我还想显示自定义错误页面(/errors/404.html)

问题:

  • 自定义 HTTP 403 页面不起作用,而是使用默认的 Nginx 403 页面
  • 后端的自定义 HTTP 404 页面有效
  • 如果我从位置块中删除proxy_intercept_errors,自定义 403 页面适用于 Nginx,但代理的自定义 404 页面显然不再起作用

配置:

server {
    listen 443 ssl http2;
    server_name mydomain.com;

    # Reverse proxy configuration
    location / {
        # Let Nginx handle basic blocking behavior
        if ($block_access) {
            return 403;
        }

        # Proxy request to backend
        proxy_pass http://localhost:8080/;
        proxy_buffering off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Connection $http_connection;

        # Only intercept 404 errors from the backend
        proxy_intercept_errors on;
        error_page 404 /errors/404.html;

        # Serve custom 404 error page
        location = /errors/404.html {
            root /var/www/html;
            internal;
        }
    }

    # Serve custom 403 error page for blocked access ($block_access = true)
    error_page 403 /errors/403.html;
    location = /errors/403.html {
        root /var/www/html;
        internal;
    }

    location /errors/ {
        root /var/www/html;
        try_files $uri $uri/ =404;
    }
}

我或多或少在寻找解决方案时完全不知所措......

nginx
  • 1 个回答
  • 61 Views
Martin Hope
Lifeboy
Asked: 2025-03-12 02:35:32 +0800 CST

Nginx 和 acne.sh LE 证书:验证错误:来自 .well-known/acme-challenge 的响应无效

  • 6

我正在使用 acme.sh 为较旧的服务器创建证书,出于各种原因我需要运行该证书。但是,我缺少 .well-known/acme-challenge 部分的内容。

当我运行脚本时:

acme.sh --issue --nginx -d fish.pre.imb.co --test --debug 2 

我收到一个错误:

fish.pre.imb.co:Verify error:197.214.119.177: Invalid response from https://fish.pre.imb.co/.well-known/acme-challenge/0wDIf77g9i9U1fx75tGX9UEOK_d4-A3ZdORclyTd14k: 404

但是,如果我将一个具有上述查询名称的文件放入目录中,它就可以正常解析。

这是我的 nginx conf 文件:

upstream puma_fisheagle {
        server 192.168.abc.xyz:1234 max_fails=3 fail_timeout=30s;
}

# IMB external system api server setup.
server {
        resolver_timeout 10s;

        server_name fish.pre.imb.co;
        root /var/local/www/fisheagle/public;
        access_log /var/log/nginx/fisheagle.log;
        rewrite_log on;
        try_files $uri/index.html $uri @puma_fisheagle;

        location @puma_fisheagle {
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_read_timeout  1800s;
                proxy_redirect off;
                proxy_pass http://puma_fisheagle;
                proxy_connect_timeout   2;
        }
    
        location /.well-known/acme-challenge {
                default_type "text/plain";
                allow all;
                root /var/local/www/acme;
        }

    location /nginx_status {
        stub_status on;
        access_log off;
        allow 192.168.abc.0/24; 
    #   deny all;
    }
    
        error_page 404 /404.html;
        error_page 422 /422.html;
        error_page 500 503 504 /500.html;
        error_page 502 /502.html;
        client_max_body_size 10m;
        keepalive_timeout 10m;

    listen              443 ssl;

    ssl_certificate      /etc/acme.sh/certs/fish.pre.imb.co/fullchain.cer;
    ssl_certificate_key  /etc/acme.sh/certs/fish.pre.imb.co/fish.pre.imb.co.key;
        # intermediate configuration
        # old configuration
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/ssl/private/ffdhe4096.pem;
    ssl_ecdh_curve secp384r1;
    ssl_ciphers EECDH+AESGCM:EDH+AESGCM;
    ssl_session_timeout 60m;
    ssl_session_cache shared:SSL:50m;
}

server {
    if ($host = fish.pre.imb.co) {
        return 301 https://$host$request_uri;
    }

    server_name fish.pre.imb.co;

    listen 80;
    return 404;
}

因此,acme 似乎无法将文件写入目录?我的意思是,如果我手动写入,配置会读取它。

我错过了什么?

这是 acme.sh 调试文件:https://pastebin.com/6zHGAprA

nginx
  • 1 个回答
  • 120 Views
Martin Hope
RDK
Asked: 2025-03-02 00:22:18 +0800 CST

配置 Fail2ban jail 以捕获特定文件的 302 状态代码

  • 5

伙计们...我在 StackExchange 网站上问了这个问题,有人建议这里会更好...

我有一个 nginx 网站,该网站对不存在的文件“mydata.html”的点击次数很多。这些点击次数以 301(或 302)状态代码记录在网站的访问日志中。我想知道如何配置 Fail2ban jail,它将捕获这些点击次数,并在点击次数过多时禁止 IP。

以下是日志文件中的一个示例记录:

1.2.3.4 - - [02/Mar/2025:00:00:06 -0700] “GET /MyData.html HTTP/1.1” 301 185 “http://xxx.MySite.com/MyData.html” “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,如 Gecko) Chrome/125.0.0.0 Safari/537.36”

我尝试使用正则表达式来选择条目^.*MyData.*HTTP.* 301。是否可以修改该正则表达式以获取 302 或其他代码?

有人能提供一些指导吗?我对正则表达式的了解还不如初学者。谢谢……RDK

nginx
  • 1 个回答
  • 138 Views
Martin Hope
Dream59
Asked: 2025-02-24 22:11:26 +0800 CST

尽管安全组阻止了直接 IP 访问,机器人如何仍向我的 AWS ECS Fargate 服务发送请求?

  • 5

我已经使用AWS ECS Fargate部署了我的容器化应用程序。我的设置包括:

  • 在端口 8000上运行的FastAPI Web服务器。
  • 在端口 8080上运行的NGINX服务器。

出于安全原因,我阻止了通过安全组的直接访问- 这意味着没有人可以通过端口 80、443、8080 或 8000访问我的实例的公共 IP 。

相反,我使我的应用程序只能通过AWS 应用程序负载均衡器 (ALB)访问,该均衡器处理基于域的流量。我的假设是,由于无法访问公共 IP ,除非机器人知道我的域名,否则它们将无法发送自动请求。

然而,我仍然看到机器人请求不断涌入。

我的问题:

  1. 如果直接 IP 访问被阻止,机器人如何仍向我的服务发送请求?
  2. 有没有办法在不使用 AWS WAF 或其他 AWS 安全服务的情况下完全阻止机器人流量?
  3. 阻止直接公共 IP 访问是否意味着只有知道我的域的用户才能访问该服务?

希望大家能对发生这种情况的原因以及可能的解决方案有所了解。

nginx
  • 3 个回答
  • 78 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助
subwaysurfers
my femboy roommate

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve