首页 > 解决方案 > NGINX - proxy_pass root 只允许特定 IP 访问

问题描述

我只想允许来自 10.10.10.94 的流量

如果我浏览

http://IP/api:5006(从允许以外的任何机器)我被拒绝访问,它应该可以正常工作

http://IP:5007我得到网页(虽然 WEB 页面页面中没有显示数据,但某些图表应该只来自允许的主机)

问题:

对于/api位置访问工作正常,它只允许从允许的 IP,但/没有限制,可以从任何 IP 访问。

server {
    listen 80;
    listen [::]:80;
    server_name site.com;
    client_max_body_size 16M;
    return 301 https://$host$request_uri;
}

server {
  listen 443 http2 ssl;
  listen [::]:443 http2 ssl;

  server_name site.com;
  client_max_body_size 16M;
  ssl_certificate /etc/ssl/certs/star.pem;
  ssl_certificate_key /etc/ssl/private/star.key;

  location / {

      allow 10.10.10.94;
      deny 10.10.0.0/16;
      proxy_pass http://127.0.0.1:5007/;
  }
  location /api/ {

       allow 10.10.10.94;
      deny 10.10.0.0/16;
      proxy_pass http://127.0.0.1:5006/;
  }
}

标签: nginxnginx-location

解决方案


试试这个位置:

  location / {
      allow 10.10.10.94/32;
      deny all;
      proxy_pass http://127.0.0.1:5007/;
  }
  location /api/ {
      allow 10.10.10.94/32;
      deny all;
      proxy_pass http://127.0.0.1:5006/;
  }

推荐阅读