使用Nginx实现根据 IP 匹配指定 URL
2015-04-11 00:00
537 查看
最近的一个项目,需要特定的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 配置
修改后的 Nginx 配置
在实现需求的过程中出现的问题
把 if 指令 和 proxy_pass 都放在 location 下面的话,if 指令里面的内容不会执行,只会执行 proxy_pass。
if 指令下面使用 proxy_pass 指令问题
像下面这样使用会报错,错误的方式:
正确的方式:
或是
如果你是直接另外启动一个 location 的话,比如启动如下 location :
这样的方式也是不支持的,当用 IP 192.168.0.50 访问的时候,没有达到我们的业务需求,会报错 400
注:各位有其他好的建议,欢迎探讨。
业务需求
业务和开发同事需要我这边做一条规则,所有访问 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实现根据 IP 匹配指定 URL
- 分享:使用搜索技术实现 URL 智能匹配
- Ionic2系列——使用DeepLinker实现指定页面URL
- java根据map指定的value值获取对应的key值使用Gson实现
- nginx中对指定的IP不使用密码对区域外IP使用密码访问
- 使用nginx的rewrite实现代理指定文件夹命令方法
- Ionic2系列之使用DeepLinker实现指定页面URL
- 使用nginx的rewrite实现代理指定目录命令方法
- C#中怎么使用系统默认浏览器打开指定的URL(代码实现)
- 使用nginx的rewrite实现代理指定目录命令方法
- 如何使用curl命令指定ip访问url
- c#中HttpWebRequest使用Proxy实现指定IP的域名请求
- c#中HttpWebRequest使用Proxy实现指定IP的域名请求
- Nginx服务器下使用rewrite重写url以实现伪静态的示例
- 使用RewritePath方法实现【不同路径+任意URL后缀重写到指定页面且URL地址不变】(附源码)_AX
- nginx 禁止访问配置,指定URL地址指定IP允许访问
- (不推荐使用)springMVC基本配置+继承MultiActionController来实现根据参数名指定要请求的方法
- 2、nginx中使用Lua根据url参数长度跳转到不同的upstream
- 使用nginx针对URL实现负载均衡或者说接口定向分发