V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
cnit
V2EX  ›  NGINX

如何限制只允许内网访问

  •  
  •   cnit · 138 天前 · 2223 次点击
    这是一个创建于 138 天前的主题,其中的信息可能已经有所发展或是发生改变。

    使用 status.example.com 解析到 123.123.666.666 (公网 ip) 后添加如下配置

    nginx conf

    server {
        listen 80;
        server_name status.example.com;
        charset utf-8;
    	 
    
    
        location / {
            proxy_pass http://192.168.1.167:3001;
    		proxy_set_header   X-Real-IP $remote_addr;
    		proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
    		proxy_set_header   Host $host;
    		proxy_set_header   X-Forwarded-Host $host;
    		proxy_http_version 1.1;
    		proxy_set_header   Upgrade $http_upgrade;
    		proxy_set_header   Connection "upgrade";
        } 
    	
     
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root /usr/share/nginx/html;
        }
    }
    

    现在想要 限制只允许同内网的机器访问 ,尝试过添加

     # 优先允许内网中的特定 IP 地址
     allow 192.168.0.199; # 或者你想要允许的内网 IP 地址
     # 接着允许特定网段的 IP 范围访问
     allow 192.168.0.0/23;
     # 最后拒绝所有其他 IP 地址
     deny all;
    

    但是测试发现要么都是允许要么都是 403

    网络路径是 status.example.com > 123.123.666.666 > 深信服 > 内网机器(nginx)

    13 条回复    2023-12-12 10:17:02 +08:00
    InDom
        1
    InDom  
       138 天前
    但凡看一眼 log ...

    中间夹了一道,所以你获取的客户端 IP 是 深信服 的 IP 。

    不知道这个 深信服 有没有在流量中添加源 IP 。
    NessajCN
        2
    NessajCN  
       138 天前
    你直接只监听内网 ip 不就好了
    listen 80;
    改成
    listen <内网 ip>:80;
    9113946
        3
    9113946  
       138 天前
    端口没映射到外网,你是怎么做到外网能访问的?
    InDom
        4
    InDom  
       138 天前
    @InDom 还有一个问题,如果你引导用户使用 公网 IP 访问,那么你获取的应该也是客户端的公网 IP 才对?
    cnit
        5
    cnit  
    OP
       138 天前
    @InDom


    192.168.0.1 - [11/Dec/2023:17:43:13 +0800] "GET / HTTP/1.1" status.example.com 200 200 2012 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36" - - 192.168.1.167:3001 0.031 0.031

    日志是这样的

    @NessajCN

    这样的我试过,还说不能 reload 要完全退出在再启动,实际测试依然无效
    cnit
        6
    cnit  
    OP
       138 天前
    应该是 192.168.0.1 这个问题了 手机用 5G 流量访问也是这个
    NessajCN
        7
    NessajCN  
       138 天前
    @cnit 你配置没写对吧,是不是 sites-enabled 里面的配置把外面的覆盖了
    ondeay
        8
    ondeay  
       138 天前
    内网的机器是通过外网 ip 访问你这个网址,allow 应该配置的是公司的出口公网 ip ,而不是内网 ip
    cnit
        9
    cnit  
    OP
       138 天前
    @NessajCN 没有这个 sites-enabled 配置的

    @ondeay 这个我之前试过 ,无效


    现在看日志,不管是内部还是外部流量,access.log 都是 192.168.0.1 ,感觉是防火墙上转发一层
    coderzhangsan
        10
    coderzhangsan  
       138 天前
    限制只允许在内网访问,我理解的是服务器如果不在内网部署,就要出公网去连服务器,只需要限制公司公网 IP 访问就好;如果在内网部署,只需要在内网 dns 加域名解析就好,也不知道对不对。
    bing1178
        11
    bing1178  
       138 天前
    你防火墙 限制 3001 端口访问就可以了, 只允许你 nginx 这台机器访问 http://192.168.1.167:3001;
    Lax
        12
    Lax  
       138 天前
    做了限制,又相当于没限制
    cnit
        13
    cnit  
    OP
       137 天前
    各位 ,问题出在防火墙上,外部流量进来的时候被 192.168.0.1 给代理了,导致在应用侧收到的访问 ip 都是 192.168.0.1
    ```
    location /ip {
    default_type text/plain;
    return 200 $remote_addr;
    }
    ```


    让网关同事联系了深信服,处理方案类似于

    https://bbs.sangfor.com.cn/forum.php?mod=viewthread&tid=10516

    https://bbs.sangfor.com.cn/forum.php?mod=viewthread&tid=10516
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2922 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 14:32 · PVG 22:32 · LAX 07:32 · JFK 10:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.