当前位置:首页 > 编程开发

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

webgou9年前 (2017-11-13)编程开发97

 最近的一个项目,需要特定的IP访问某专题页面的时候跳转到网站首页,思考了下,直接使用NGINX实现,分享给大家。

业务需求

业务和开发同事需要我这边做一条规则,所有访问 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 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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。

1
2
3
4
5
6
7
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 指令问题

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

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

正确的方式:

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

或是

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


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

1
2
3
4
5
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

 

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

来源:http://www.jb51.net/article/54854.htm

扫描二维码推送至手机访问。

版权声明:本文由知了博客发布,如需转载请注明出处。

本文链接:https://www.webgou.info/?id=612

标签: ngix
分享给朋友:

“使用Nginx实现根据 IP 匹配指定 URL” 的相关文章

linux 常用命令

作者:北南南北来自:LinuxSir.Org摘要:Linux 命令行模式下来操作文件管理还是比较重要的,虽然目前在Linux桌面上能完成大部份的文件管理工作,但就效率来说,还是命令行模式更为强大。最重要的是在文本操作界面和图形界面通用。如果您是服务器管理人员,您能体会到命令行下的文件管理工具是何等重…

MyEclipse 中SVN 中的安装

MyEclipse 中SVN 中的安装 1. 这里介紹Eclipse的SVN Plugin,叫做Subclipse,官方网站的下载地址在 ...…

智能科普:VR、AR、MR的区别

 近日, 获得谷歌5亿美元融资的技术公司Magic Leap在WSJD展会中放出了一段实录视频,引起不小骚动。如今,也有媒体称他们为MR公司,那么VR、AR、MR之间到底有什么区别呢。VR、AR、MR定义:什么是虚拟现实?虚拟现实(Virtual Realit…

中国iOS开发者出走海外

7月6日,知名移动数据监测机构App Annie在移动互联网创新大会上公布的这组数据揭开了中国移动互联网产业的现实与无奈。 整个iOS市场中,中国区APP下载量排名全球第二,但收入只排到全球第八,中国平均每下载量收入仅有0.03美元,是美国用户0.28美元的十分之一,甚至只有越南的一半。 在iO…

cooliris应用

下面为cooliris用falsh嵌入Flickr图片,效果很好. 本来想找到cooliris源码,再继续分析,cooliris的实现过程,我猜想cooliris是基于GPL的认证,由于其实现采用B-S,所以要想获得其在服务器上的代码有点困难,尽管有人可以用反编译出其Actoinscript源码,反…

Android文件系统深入剖析

用过Linux的朋友可能对开源的系统有一个非常好的印象。那么在日益成熟的手机操作系统中,有没有一款像Linux一样的开源系统呢?谷歌公司就退出了一款基于Linux平台的Android 操作系统来满足人们的需求。在这里大家可以先来了解一下Android文件系统的相关内容。1、Android文件系统的结…

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。