简体   繁体   English

nginx 拒绝基于 $http_x_forwarded_for

[英]nginx deny based on $http_x_forwarded_for

I have an nginx container in openshift.我在 openshift 中有一个 nginx 容器。 I am trying to limit the access from external IPs, more specifically, anything not in the 10.XXX range.我试图限制来自外部 IP 的访问,更具体地说,限制在 10.XXX 范围之外的任何内容。

This is my config file这是我的配置文件

http {
    ....
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    ....

    }
    server {

        listen 8080;
        server_name app.okd.company.com;
        allow 10.0.0.0/8;
        deny all;
        location / {
        proxy_pass http://app/;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_redirect off;
        }

        location /static/ {
        autoindex on;
        alias /app/static/;
        }
    }

The connection is allowed whether private or external.无论是私人的还是外部的,连接都是允许的。 Here are some logs.这是一些日志。

10.129.2.1 - - [16/Nov/2021:19:28:57 +0000] "POST /graphql/ HTTP/1.1" 200 53384 "https://app.okd.company.com/ " "Mozilla/5.0 (X11; Linux x86_64; rv:94.0) Gecko/20100101 Firefox/94.0" "10.3.16.158" 10.129.2.1 - - [16/Nov/2021:19:28:57 +0000] "POST /graphql/ HTTP/1.1" 200 53384 "https://app.okd.company.com/" "Mozilla/5.0 ( X11; Linux x86_64; rv:94.0) Gecko/20100101 Firefox/94.0" "10.3.16.158"

10.131.2.1 - - [16/Nov/2021:19:42:56 +0000] "POST /graphql/ HTTP/1.1" 200 53384 "https://app.okd.company.com/ " "Mozilla/5.0 (X11; Linux x86_64; rv:94.0) Gecko/20100101 Firefox/94.0" "73.177.XXX.XXX" 10.131.2.1 - - [16/Nov/2021:19:42:56 +0000] "POST /graphql/ HTTP/1.1" 200 53384 "https://app.okd.company.com/" "Mozilla/5.0 ( X11; Linux x86_64; rv:94.0) Gecko/20100101 Firefox/94.0" "73.177.XXX.XXX"

The first log seems to be allowing private IP connections, which is expected, but the second one is still being allowed.第一个日志似乎允许私有 IP 连接,这是预期的,但第二个日志仍然被允许。 I'm not sure why it isn't blocking.我不确定为什么它没有阻塞。

EDIT:编辑:

I realize the remote_addr is in the private IP range.我意识到 remote_addr 在私有 IP 范围内。 I don't care which proxy it used access the nginx I have control over.我不在乎它使用哪个代理访问我可以控制的 nginx。 I just care about the origin/http_x_forwarded_for.我只关心 origin/http_x_forwarded_for。 Is there a way I can allow or deny based off of that有没有一种方法可以基于此允许或拒绝

To use the http_x_forwarded_for as the real IP, you should set that in the server config.要将http_x_forwarded_for用作真实 IP,您应该在服务器配置中进行设置。

...
    server {
        set_real_ip_from 10.0.0.0/8;
        real_ip_header X-Forwarded-For;
...

set_real_ip_from1 is not optional. set_real_ip_from1不是可选的。 It needs to contain all addresses that could be the forwarding proxy它需要包含所有可能是转发代理的地址

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果$ http_x_forwarded_for存在,则有条件的Nginx日志记录? - Conditional nginx logging if $http_x_forwarded_for exists? Nginx limit_req_zone 使用 http_x_forwarded_for 对我不起作用 - Nginx limit_req_zone using http_x_forwarded_for is not working for me set_real_ip_from仍包含在HTTP_X_FORWARDED_FOR中 - set_real_ip_from still included in HTTP_X_FORWARDED_FOR 为什么我不能使用 ufw 拒绝来自 xxxx 的“Nginx HTTP”? - Why i cant use ufw deny to "Nginx HTTP" from x.x.x.x? 使用$ http_x_forwarded_proto强制使用NGINX上的www和https重定向作为Google云负载均衡器的后端 - Forcing www and https redirect on NGINX as backend of Google cloud Load Balancer using $http_x_forwarded_proto “ERR_TOO_MANY_REDIRECTS”nginx-ingress controller 不会覆盖 X-Forwarded-Proto:http,X-Forwarded-Scheme:http - "ERR_TOO_MANY_REDIRECTS" nginx-ingress controller does not overwrite X-Forwarded-Proto: http, X-Forwarded-Scheme: http Nginx:什么是X-Forwarded-For WebSockets的替代品? - Nginx: What is the X-Forwarded-For alternative for WebSockets? Django 落后于 NGINX 反向代理和 AWS Application Load Balancer 未在 HTTP_X_FORWARDED_PROTO 中从客户端转发 HTTPS - Django behind NGINX reverse proxy and AWS Application Load Balancer doesn't get HTTPS forwarded from client in HTTP_X_FORWARDED_PROTO NGinx $proxy_add_x_forwarded_for 和 real_ip_header - NGinx $proxy_add_x_forwarded_for and real_ip_header Nginx 入口控制器未设置 X-Forwarded-Host - Nginx Ingress Controller does not set X-Forwarded-Host
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM