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

 更新时间:2014年09月06日 08:42:26   投稿:hebedich  
最近的一个项目,需要特定的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 配置

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

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

相关文章

  • nginx反向代理webSocket配置详解

    nginx反向代理webSocket配置详解

    这篇文章主要介绍了nginx反向代理webSocket配置详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-10-10
  • nginx常用操作命令详解

    nginx常用操作命令详解

    这篇文章主要介绍了nginx常用操作命令,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-07-07
  • 利用nginx搭建RTMP视频点播、直播、HLS服务器

    利用nginx搭建RTMP视频点播、直播、HLS服务器

    本文主要介绍了利用nginx搭建RTMP视频点播、直播、HLS服务器,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • Nginx轻松搞定跨域问题完整过程

    Nginx轻松搞定跨域问题完整过程

    这篇文章主要给大家介绍了关于Nginx轻松搞定跨域问题的相关资料,大家在使用nginx进行服务器管理,经常会遇到跨域问题,文中将解决方法介绍的非常详细,需要的朋友可以参考下
    2023-08-08
  • Linux  安装nginx服务器详细介绍

    Linux 安装nginx服务器详细介绍

    这篇文章主要介绍了Linux 安装nginx服务器详细介绍的相关资料,需要的朋友可以参考下
    2016-10-10
  • nginx实现多geoserver服务的负载均衡的示例代码

    nginx实现多geoserver服务的负载均衡的示例代码

    本文主要介绍了nginx实现多geoserver服务的负载均衡的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-05-05
  • 在nginx中部署https服务的详细步骤

    在nginx中部署https服务的详细步骤

    Web服务器一般指网站服务器,可以处理浏览器等Web客户端的请求并返回相应响应,也可以放置网站文件,让全世界浏览;可以放置数据文件,让全世界下载,本文主要介绍nginx中部署https服务的具体流程,也是搭建web的开端与主要步骤之一
    2023-10-10
  • nginx rewrite参数解析

    nginx rewrite参数解析

    这篇文章主要介绍了nginx rewrite参数解析的相关资料,需要的朋友可以参考下
    2022-11-11
  • 利用Nginx处理Vue开发环境的跨域的方法

    利用Nginx处理Vue开发环境的跨域的方法

    这篇文章主要介绍了利用Nginx处理Vue开发环境的跨域的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-06-06
  • Nginx负载均衡配置简单配置方法

    Nginx负载均衡配置简单配置方法

    Nginx作为负载均衡服务器,用户请求先到达nginx,再由nginx根据负载配置将请求转发至不同的Web服务器。下面通过本文给大家分享Nginx负载均衡配置简单配置方法,需要的的朋友参考下吧
    2017-08-08

最新评论