当前位置: 首页 > 编程笔记 >

使用Nginx实现根据 IP 匹配指定 URL

后星河
2023-03-14
本文向大家介绍使用Nginx实现根据 IP 匹配指定 URL,包括了使用Nginx实现根据 IP 匹配指定 URL的使用技巧和注意事项,需要的朋友参考一下

业务需求

业务和开发同事需要我这边做一条规则,所有访问 ip 为非上海、广州 office 外网 ip,url 为http://test.com/fuck/index.html 的请求都跳转到 http://test.com/index.html 。然后所有在上海和广州 office 的外网 IP 访问 http://test.com/fuck/index.html 依然还是 http://test.com/fuck/index.html。这样就可以在生产上做隔离,不影响其他用户的服务。

注:因为目前生产上的 Nginx 没有做 lua 支持,所以就无法通过使用 lua 来实现该需求,也没有安装 geoip ,所以也无法用模块来支持,只能原生的。

原始的 nginx 配置

upstream service_test {
     server 127.0.0.1:8080;
}
server
 {
  listen    80;
  server_name test.com;
  index index.html index.php;
  root /tmp/test.com;
  error_page 404 http://test.com/404.html;
  error_page 502 http://test.com/502.html;
  error_page 500 http://test.com/500.html;
  location ~* \.(gif|jpg|jpeg|png|css|js|ico|txt|svg|woff|ttf|eot)$
  {
    rewrite ^(.*)$ /static$1 break;
    root /tmp/test.com; # 
    expires 1d;
  }
  location ~* \.(html|htm)$
  {
    rewrite ^(.*)$ /static$1 break;
    roo /tmp/test.com; # 
    expires 900s;
  }
  location / {
     proxy_pass http://service_test;
     include /opt/conf/nginx/proxy.conf;
  }

修改后的 Nginx 配置

upstream service_test {
     server 127.0.0.1:8080;
}
server
 {
  listen    80;
  server_name test.com;
  index index.html index.php;
  root /tmp/test.com;
  error_page 404 http://test.com/404.html;
  error_page 502 http://test.com/502.html;
  error_page 500 http://test.com/500.html;
  location ~* \.(gif|jpg|jpeg|png|css|js|ico|txt|svg|woff|ttf|eot)$
  {
    rewrite ^(.*)$ /static$1 break;
    root /tmp/test.com; # 
    expires 1d;
  }
  location ~* \.(html|htm)$
  {
    rewrite ^(.*)$ /static$1 break;
    roo /tmp/test.com; # 
    expires 900s;
  }
  set $flag 0;
  if ($request_uri ~* "^/fuck/\w+\.html$") {
      set $flag "${flag}1";
  }
  if ($remote_addr !~* "192.168.0.50|192.168.0.51|192.168.0.56") {
    set $flag "${flag}2";
  }
  if ($flag = "012") {
    rewrite ^ /index.html permanent;
  }
  location / {
     proxy_pass http://service_test;
     include /opt/conf/nginx/proxy.conf;
  }

在实现需求的过程中出现的问题

把 if 指令 和 proxy_pass 都放在 location 下面的话,if 指令里面的内容不会执行,只会执行 proxy_pass。

location / {
   if ($remote_addr !~* "192.168.0.50|192.168.0.51|192.168.0.56") {
      rewrite ^ /index.html permanent;
   }
   proxy_pass http://service_test;
   include /opt/conf/nginx/proxy.conf;
}

if 指令下面使用 proxy_pass 指令问题

像下面这样使用会报错,错误的方式:

    if ($remote_addr ~* "192.168.0.50|192.168.0.51|192.168.0.56") {
      proxy_pass http://test.com/fuck;
    }

正确的方式:

    if ($remote_addr ~* "192.168.0.50|192.168.0.51|192.168.0.56") {
      proxy_pass http://test.com$request_uri;
    }

或是

    if ($remote_addr ~* "192.168.0.50|192.168.0.51|192.168.0.56") {
      proxy_pass http://test.com;
    }


如果你是直接另外启动一个 location 的话,比如启动如下 location :

  location /fund {
     if ($remote_addr !~* "192.168.0.50|192.168.0.51|192.168.0.56") {
        rewrite ^ /index.html permanent;
     }
  }

这样的方式也是不支持的,当用 IP 192.168.0.50 访问的时候,没有达到我们的业务需求,会报错 400

注:各位有其他好的建议,欢迎探讨。

 类似资料:
  • 本文向大家介绍Python实现ping指定IP的示例,包括了Python实现ping指定IP的示例的使用技巧和注意事项,需要的朋友参考一下 贴代码: ip=“192.168.1.11”的运行结果如下:ping不通 ip=“172.24.186.191”的运行结果如下:ping通 以上这篇Python实现ping指定IP的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持呐

  • 问题内容: 我正在使用以下代码来解析yaml并应将输出作为对象,并且该函数应更改数据结构并根据以下结构提供输出 这是我尝试过的方法,但是我不确定如何从yaml中获取的值 替换 函数运行器中 的硬代码值 与来自 这就是我尝试过的所有想法,该怎么做? 问题答案: 将runners对象的名称分配给名称的struct 字段,并使用与名称匹配的函数命令将命令列表附加到type字段: 操场上的工作代码

  • 问题内容: 整整一整天,我一直在编程领域投入时间来进行指纹匹配/识别算法/实现。尽管有点模糊,因为我似乎找不到任何真正相关的东西。 我基本上是在寻找两件事: 指纹识别:验证图像实际上是一个指纹,因此可以与另一个指纹匹配 指纹匹配:从项目中匹配两个指纹以查看实际是否相等 所有操作都将在图像上完成,因此我与硬件无关。我发现了一些东西,例如Java Fingerprint SDK等,并且在Stackov

  • 本文向大家介绍Nginx如何封禁IP和IP段的实现,包括了Nginx如何封禁IP和IP段的实现的使用技巧和注意事项,需要的朋友参考一下 写在前面 Nginx不仅仅只是一款反向代理和负载均衡服务器,它还能提供很多强大的功能,例如:限流、缓存、黑白名单和灰度发布等等。在之前的文章中,我们已经介绍了Nginx提供的这些功能。小伙伴们可以到【Nginx专题】进行查阅。今天,我们来介绍Nginx另一个强大的

  • 我试图创建一个与ip地址匹配的regex,或者一个带有网络掩码的ip地址:类似于8.8.8.8/24 第一部分匹配得很好,第二部分与netmask匹配得很好…有人知道为什么吗?

  • 本文向大家介绍Nginx 根据URL带的参数转发的实现,包括了Nginx 根据URL带的参数转发的实现的使用技巧和注意事项,需要的朋友参考一下 使用场景: 需要根据截取URL动态配置跳转路径,常见于访问内网不固定ip地址的文件图片, 请求地址:http://11.19.1.212:82/bimg4/32.52.62.42:222/DownLoadFile?filename=LOC:12/data/